使用RK4解决Lorenz系统的动画和动画

时间:2018-02-19 14:43:43

标签: python animation matplotlib simulation chaos

我正在研究一个围绕混沌振荡器的项目。 现在,我刚刚使用四阶Runge-Kutta在Python中编写了一个Lorenz Attractor:

'''
Created on 19 feb. 2018

@author: judvuyst
'''
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.animation as animation

#Differential equations of a Lorenz System
def X(x, y, s):
    return s * (y - x)

def Y(x, y, z, r):
    return (-x) * z + r * x - y

def Z(x, y, z, b):
    return x * y - b * z

#RK4 for the differential equations
def RK4(x, y, z, s, r, b, h):
    k_1 = X(x, y, s)
    l_1 = Y(x, y, z, r)
    m_1 = Z(x, y, z, b)

    k_2 = X((x + k_1 * h * 0.5), (y + l_1 * h * 0.5), s)
    l_2 = Y((x + k_1 * h * 0.5), (y + l_1 * h * 0.5), (z + m_1 * h * 0.5), r)
    m_2 = Z((x + k_1 * h * 0.5), (y + l_1 * h * 0.5), (z + m_1 * h * 0.5), b)

    k_3 = X((x + k_2 * h * 0.5), (y + l_2 * h * 0.5), s)
    l_3 = Y((x + k_2 * h * 0.5), (y + l_2 * h * 0.5), (z + m_2 * h * 0.5), r)
    m_3 = Z((x + k_2 * h * 0.5), (y + l_2 * h * 0.5), (z + m_2 * h * 0.5), b)

    k_4 = X((x + k_3 * h), (y + l_3 * h), s)
    l_4 = Y((x + k_3 * h), (y + l_3 * h), (z + m_3 * h), r)
    m_4 = Z((x + k_3 * h), (y + l_3 * h), (z + m_3 * h), b)

    x += (k_1 + 2 * k_2 + 2 * k_3 + k_4) * h * (1/6)
    y += (l_1 + 2 * l_2 + 2 * l_3 + l_4) * h * (1/6)
    z += (m_1 + 2 * m_2 + 2 * m_3 + m_4) * h * (1/6)

    return (x, y, z)

#Initial values and Parameters
x_0, y_0, z_0 = 1, 1, 1
s, r, b = 10, 28, 8/3

#RK4 iteration
x_list = [x_0]
y_list = [y_0]
z_list = [z_0]

h = 0
i = 0

while h < 0.15:
    x = x_list[i]
    y = y_list[i]
    z = z_list[i]

    position = RK4(x, y, z, s, r, b, h)

    x_list.append(position[0])
    y_list.append(position[1])
    z_list.append(position[2])

    h += 0.0000005
    i += 1

#plotten
fig = plt.figure()
ax = fig.gca(projection = '3d')

x_array = np.array(x_list)
y_array = np.array(y_list)
z_array = np.array(z_list)

ax.plot(x_array, y_array, z_array, ',')

plt.show()

我的问题是:我可以使用此代码对吸引子进行模拟吗?某种点和线遵循这些方程并留下一条尾迹,最后是完整的吸引子图像。 我对matplotlib的动画模块并不熟悉。

提前谢谢!

亲切的问候

儒略

0 个答案:

没有答案
相关问题