forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlorenz_attractor.py
67 lines (51 loc) · 1.58 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
"""
Lorenz Attractor
The Lorenz attractor is a set of chaotic solutions to the Lorenz system,
which are differential equations originally designed to model atmospheric
convection. It exhibits chaotic behavior and is sensitive to initial conditions.
References:
https://en.wikipedia.org/wiki/Lorenz_system
Requirements:
- matplotlib
- numpy
"""
import matplotlib.pyplot as plt
import numpy as np
def lorenz(x, y, z, s=10, r=28, b=2.667):
"""
Calculate the next step of the Lorenz system.
"""
x_dot = s * (y - x)
y_dot = r * x - y - x * z
z_dot = x * y - b * z
return x_dot, y_dot, z_dot
def generate_lorenz_attractor(
num_steps=10000, dt=0.01, initial_values=(0.0, 1.0, 1.05)
):
"""
Generates the points for the Lorenz attractor based on initial conditions.
>>> len(generate_lorenz_attractor(1000, 0.01))
1000
"""
xs = np.empty(num_steps + 1)
ys = np.empty(num_steps + 1)
zs = np.empty(num_steps + 1)
xs[0], ys[0], zs[0] = initial_values
for i in range(num_steps):
x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
xs[i + 1] = xs[i] + (x_dot * dt)
ys[i + 1] = ys[i] + (y_dot * dt)
zs[i + 1] = zs[i] + (z_dot * dt)
return xs, ys, zs
def plot_lorenz(xs, ys, zs):
"""
Plot the Lorenz attractor using matplotlib in 3D space.
"""
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.plot(xs, ys, zs, lw=0.5)
ax.set_title("Lorenz Attractor")
plt.show()
if __name__ == "__main__":
xs, ys, zs = generate_lorenz_attractor()
plot_lorenz(xs, ys, zs)