Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a68d1a

Browse files
committed
Added a Lorenz Attractor example for mplot3d.
Why? Because butterflies are awesome!
1 parent 9fe78b6 commit 3a68d1a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

examples/mplot3d/lorenz_attractor.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Plot of the Lorenz Attractor based on Edward Lorenz's 1963 "Deterministic
2+
# Nonperiodic Flow" publication.
3+
# http://journals.ametsoc.org/doi/abs/10.1175/1520-0469%281963%29020%3C0130%3ADNF%3E2.0.CO%3B2
4+
#
5+
# Note: Because this is a simple non-linear ODE, it would be more easily
6+
# done using SciPy's ode solver, but this approach depends only
7+
# upon NumPy.
8+
9+
import numpy as np
10+
import matplotlib.pyplot as plt
11+
from mpl_toolkits.mplot3d import Axes3D
12+
13+
14+
def lorenz(x, y, z, s=10, r=28, b=2.667) :
15+
x_dot = s*(y - x)
16+
y_dot = r*x - y - x*z
17+
z_dot = x*y - b*z
18+
return x_dot, y_dot, z_dot
19+
20+
21+
dt = 0.01
22+
stepCnt = 10000
23+
24+
# Need one more for the initial values
25+
xs = np.empty((stepCnt + 1,))
26+
ys = np.empty((stepCnt + 1,))
27+
zs = np.empty((stepCnt + 1,))
28+
29+
# Setting initial values
30+
xs[0], ys[0], zs[0] = (0., 1., 1.05)
31+
32+
# Stepping through "time".
33+
for i in xrange(stepCnt) :
34+
# Derivatives of the X, Y, Z state
35+
x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
36+
xs[i + 1] = xs[i] + (x_dot * dt)
37+
ys[i + 1] = ys[i] + (y_dot * dt)
38+
zs[i + 1] = zs[i] + (z_dot * dt)
39+
40+
fig = plt.figure()
41+
ax = fig.gca(projection='3d')
42+
43+
ax.plot(xs, ys, zs)
44+
ax.set_xlabel("X Axis")
45+
ax.set_ylabel("Y Axis")
46+
ax.set_zlabel("Z Axis")
47+
48+
plt.show()
49+

0 commit comments

Comments
 (0)