Examples Lynch book
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
554 B

  1. # Program 02e: Numerical and truncated series solution
  2. # figure 2.6
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. from scipy.integrate import odeint
  6. def ode(X, t):
  7. x = X[0]
  8. y = X[1]
  9. dxdt = y
  10. dydt = x-t**2*y
  11. return [dxdt, dydt]
  12. X0 = [1, 0]
  13. t = np.linspace(0, 10, 1000)
  14. sol = odeint(ode, X0, t)
  15. x = sol[:, 0]
  16. y = sol[:, 1]
  17. fig, ax = plt.subplots()
  18. ax.plot(t, x, label='Numerical')
  19. ax.plot(t, 1+t**2/2+t**4/24, 'r-', label='Truncated series')
  20. plt.xlabel('t')
  21. plt.ylabel('x')
  22. plt.xlim(0, 4)
  23. plt.ylim(0, 4)
  24. ax.legend()
  25. plt.show()