Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- def euler(h, yz, y0, t0, dt, steps):
- ys = np.empty(int(steps/dt))
- ts = np.empty(int(steps/dt))
- ys[0] = y0
- ts[0] = t0
- u1 = yz + 1
- u2 = yz - 1
- u = u1
- for i in range(2, steps):
- ts[i] = ts[i - 1] + dt
- if (yz - (h / 2) - ys[i - 1]) > 1:
- u = u1
- if (yz - (h / 2) - ys[i - 1]) < 0:
- u = u2
- ys[i] = ys[i - 1] + dt * (3 * u - 3 * ys[i - 1])
- y = ys
- t = ts
- return y, t
- y, t = euler(1, 20, 0, 0, 0.1, 50)
- plt.plot(t, y)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement