Guest User

Untitled

a guest
Jan 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. (3x-1)y''-(3x+2)y'+(6x-8)y=0, y(0)=2, y'(0)=3
  2.  
  3. def g(y,x):
  4. y0 = y[0]
  5. y1 = y[1]
  6. y2 = (6*x-8)*y0/(3*x-1)+(3*x+2)*y1/(3*x-1)
  7. return [y1,y2]
  8.  
  9. init = [2.0, 3.0]
  10. x=np.linspace(-2,2,100)
  11. sol=spi.odeint(g,init,x)
  12. plt.plot(x,sol[:,0])
  13. plt.show()
  14.  
  15. import numpy as np
  16. import scipy as sp
  17. from scipy.integrate import odeint
  18. import matplotlib.pyplot as plt
  19.  
  20. def g(y, x):
  21. y0 = y[0]
  22. y1 = y[1]
  23. y2 = ((3*x+2)*y1 + (6*x-8)*y0)/(3*x-1)
  24. return y1, y2
  25.  
  26. # Initial conditions on y, y' at x=0
  27. init = 2.0, 3.0
  28. # First integrate from 0 to 2
  29. x = np.linspace(0,2,100)
  30. sol=odeint(g, init, x)
  31. # Then integrate from 0 to -2
  32. plt.plot(x, sol[:,0], color='b')
  33. x = np.linspace(0,-2,100)
  34. sol=odeint(g, init, x)
  35. plt.plot(x, sol[:,0], color='b')
  36.  
  37. # The analytical answer in red dots
  38. exact_x = np.linspace(-2,2,10)
  39. exact_y = 2*np.exp(2*exact_x)-exact_x*np.exp(-exact_x)
  40. plt.plot(exact_x,exact_y, 'o', color='r', label='exact')
  41. plt.legend()
  42.  
  43. plt.show()
Add Comment
Please, Sign In to add comment