---
# System prepended metadata

title: Codes for practice №14
tags: [mathbio]

---

# Codes for practice №14

###### tags: `mathbio`

```code=python
%matplotlib notebook
def lorenz(x, y, z, s=10, r=14.82, b=2.667):
    '''
    Given:
       x, y, z: a point of interest in three dimensional space
       s, r, b: parameters defining the lorenz attractor
    Returns:
       x_dot, y_dot, z_dot: values of the lorenz attractor's partial
           derivatives at the point x, y, z
    '''
    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


dt = 0.01
num_steps = 100000

# Need one more for the initial values
xs = np.empty(num_steps + 1)
ys = np.empty(num_steps + 1)
zs = np.empty(num_steps + 1)


# Plot
fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')

ax.set_xlim(-15,15)
ax.set_ylim(-15,15)
ax.set_zlim(0,20)


xs[0], ys[0], zs[0] = (0., 1., 1.05)
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)
ax.plot(xs.copy(), ys.copy(), zs.copy(), '-k', lw=0.5)

xs *= 0; ys *= 0; zs *= 0;
xs[0], ys[0], zs[0] = (-10., 1., 1.05)
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)
ax.plot(xs.copy(), ys.copy(), zs.copy(), '-r', lw=0.5)

ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
ax.set_title("Lorenz Attractor")

plt.show()
```