Advertisement
Guest User

dydx

a guest
Nov 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. from numpy import *
  2. import scipy as sp
  3. import matplotlib.pyplot as plt
  4. from scipy._lib.six import xrange
  5.  
  6.  
  7. def f(x, y):
  8.   return y
  9.  
  10.  
  11. y0 = f(1, 1)
  12. xmin = 0.
  13. xmax = 10.
  14. npoints = 150
  15.  
  16. dx = (xmax - xmin) / float(npoints)
  17.  
  18. x = sp.linspace(xmin, xmax, npoints)
  19. forwardSol = x * 0.
  20. forwardSol[0] = y0
  21.  
  22. for i in xrange(1, npoints):
  23.   forwardSol[i] = forwardSol[i-1] + dx * f(forwardSol[i-1], forwardSol[i-1])
  24.  
  25. sol_exact = exp(x)
  26.  
  27. plt.figure()
  28. plt.plot(x, forwardSol, '--r')
  29. plt.plot(x, sol_exact, 'g')
  30.  
  31. plt.ylabel("forward")
  32. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement