|
18 | 18 | import matplotlib.pyplot as plt |
19 | 19 |
|
20 | 20 |
|
21 | | -def lorenz(x, y, z, s=10, r=28, b=2.667): |
| 21 | +def lorenz(xyz, *, s=10, r=28, b=2.667): |
22 | 22 | """ |
23 | | - Given: |
24 | | - x, y, z: a point of interest in three dimensional space |
25 | | - s, r, b: parameters defining the lorenz attractor |
26 | | - Returns: |
27 | | - x_dot, y_dot, z_dot: values of the lorenz attractor's partial |
28 | | - derivatives at the point x, y, z |
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + xyz : array-like, shape (3,) |
| 26 | + Point of interest in three dimensional space. |
| 27 | + s, r, b : float |
| 28 | + Parameters defining the Lorenz attractor. |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + xyz_dot : array, shape (3,) |
| 33 | + Values of the Lorenz attractor's partial derivatives at *xyz*. |
29 | 34 | """ |
| 35 | + x, y, z = xyz |
30 | 36 | x_dot = s*(y - x) |
31 | 37 | y_dot = r*x - y - x*z |
32 | 38 | z_dot = x*y - b*z |
33 | | - return x_dot, y_dot, z_dot |
| 39 | + return np.array([x_dot, y_dot, z_dot]) |
34 | 40 |
|
35 | 41 |
|
36 | 42 | dt = 0.01 |
37 | 43 | num_steps = 10000 |
38 | 44 |
|
39 | | -# Need one more for the initial values |
40 | | -xs = np.empty(num_steps + 1) |
41 | | -ys = np.empty(num_steps + 1) |
42 | | -zs = np.empty(num_steps + 1) |
43 | | - |
44 | | -# Set initial values |
45 | | -xs[0], ys[0], zs[0] = (0., 1., 1.05) |
46 | | - |
| 45 | +xyzs = np.empty((num_steps + 1, 3)) # Need one more for the initial values |
| 46 | +xyzs[0] = (0., 1., 1.05) # Set initial values |
47 | 47 | # Step through "time", calculating the partial derivatives at the current point |
48 | 48 | # and using them to estimate the next point |
49 | 49 | for i in range(num_steps): |
50 | | - x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i]) |
51 | | - xs[i + 1] = xs[i] + (x_dot * dt) |
52 | | - ys[i + 1] = ys[i] + (y_dot * dt) |
53 | | - zs[i + 1] = zs[i] + (z_dot * dt) |
54 | | - |
| 50 | + xyzs[i + 1] = xyzs[i] + lorenz(xyzs[i]) * dt |
55 | 51 |
|
56 | 52 | # Plot |
57 | 53 | ax = plt.figure().add_subplot(projection='3d') |
58 | 54 |
|
59 | | -ax.plot(xs, ys, zs, lw=0.5) |
| 55 | +ax.plot(*xyzs.T, lw=0.5) |
60 | 56 | ax.set_xlabel("X Axis") |
61 | 57 | ax.set_ylabel("Y Axis") |
62 | 58 | ax.set_zlabel("Z Axis") |
|
0 commit comments