-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlorenz_attractor.py
73 lines (48 loc) · 2.09 KB
/
lorenz_attractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ------- Importing Libraries ------- #
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.integrate import odeint
# ------- Defining the Constants of the system ------- #
sigma = 10
beta = 8/3
rho = 28
# ------- Solving the System Numerically ------- #
def system_of_odes(vector, t, sigma, beta, rho):
x, y, z = vector
d_vector = [
sigma*(y - x),
x*(rho - z) - y,
x*y - beta*z
]
return d_vector
position_0_1 = [0.0, 1.0, 1.0] # Represents the 1st position at t = 0
position_0_2 = [20.0, 6.1, 21.0] # Represents the 2nd position at t = 0
time_points = np.linspace(0, 40, 1001)
# Solving for the 1st system
positions_1 = odeint(system_of_odes, position_0_1, time_points, args=(sigma, beta, rho))
x_sol_1, y_sol_1, z_sol_1 = positions_1[:, 0], positions_1[:, 1], positions_1[:, 2]
# Solving for the 2nd system
positions_2 = odeint(system_of_odes, position_0_2, time_points, args=(sigma, beta, rho))
x_sol_2, y_sol_2, z_sol_2 = positions_2[:, 0], positions_2[:, 1], positions_2[:, 2]
# ------- Plotting the solutions ------- #
fig, ax = plt.subplots(subplot_kw={'projection':'3d'})
lorenz_plt_1, = ax.plot(x_sol_1, y_sol_1, z_sol_1, 'red', label=f'Position 0 of 1st: {position_0_1}')
lorenz_plt_2, = ax.plot(x_sol_2, y_sol_2, z_sol_2, 'blue',label=f'Position 0 of 2nd: {position_0_2}')
plt.legend()
# ------- Animating the solutions ------- #
def update(frame):
lower_lim = max(0, frame - 100)
x_current_1 = x_sol_1[lower_lim:frame+1]
y_current_1 = y_sol_1[lower_lim:frame+1]
z_current_1 = z_sol_1[lower_lim:frame+1]
x_current_2 = x_sol_2[lower_lim:frame+1]
y_current_2 = y_sol_2[lower_lim:frame+1]
z_current_2 = z_sol_2[lower_lim:frame+1]
lorenz_plt_1.set_data(x_current_1, y_current_1)
lorenz_plt_1.set_3d_properties(z_current_1)
lorenz_plt_2.set_data(x_current_2, y_current_2)
lorenz_plt_2.set_3d_properties(z_current_2)
return lorenz_plt_1, lorenz_plt_2
animation = FuncAnimation(fig, update, frames=len(time_points), interval=25, blit=False)
plt.show()