Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. # Numerical solution of a differential equation
  2. import numpy as np
  3. import math
  4. import matplotlib.pyplot as plt
  5.  
  6. dx=0.01
  7. tau=0.5
  8. x_end=10
  9.  
  10. N=int(x_end/dx)
  11. Numerical_Solution=np.zeros(N)
  12. x=np.zeros(N)
  13. Analytic_Solution=np.zeros(N)
  14.  
  15. Numerical_Solution[0]=1
  16. x[0]=0
  17. Analytic_Solution[0]=1
  18.  
  19. for i in range (1,N):
  20. Numerical_Solution[i]=Numerical_Solution[i-1]+dx*(tau)*Numerical_Solution[i-1]
  21. x[i]=x[i-1]+dx
  22. Analytic_Solution[i]=math.exp(tau*x[i])
  23.  
  24.  
  25. fig = plt.figure(figsize=(8,4))
  26. # --- left hand plot
  27. ax = fig.add_subplot(1,3,1)
  28. plt.plot(x,Numerical_Solution,color='red')
  29. #ax.legend(loc='best')
  30. plt.title('Numerical Solution')
  31.  
  32. # --- right hand plot
  33. ax = fig.add_subplot(1,3,2)
  34. plt.plot(x,Analytic_Solution,color='blue')
  35. plt.title('Analytic Solution')
  36.  
  37. #ax.legend(loc='best')
  38. ax = fig.add_subplot(1,3,3)
  39. plt.plot(x,Analytic_Solution-Numerical_Solution,color='blue')
  40. plt.title('Error')
  41.  
  42. # --- title, explanatory text and save
  43. fig.suptitle('Exponential Growth Solution', fontsize=20)
  44. plt.tight_layout()
  45. plt.subplots_adjust(top=0.85)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement