Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Numerical solution of a differential equation
- import numpy as np
- import math
- import matplotlib.pyplot as plt
- dx=0.01
- tau=0.5
- x_end=10
- N=int(x_end/dx)
- Numerical_Solution=np.zeros(N)
- x=np.zeros(N)
- Analytic_Solution=np.zeros(N)
- Numerical_Solution[0]=1
- x[0]=0
- Analytic_Solution[0]=1
- for i in range (1,N):
- Numerical_Solution[i]=Numerical_Solution[i-1]+dx*(tau)*Numerical_Solution[i-1]
- x[i]=x[i-1]+dx
- Analytic_Solution[i]=math.exp(tau*x[i])
- fig = plt.figure(figsize=(8,4))
- # --- left hand plot
- ax = fig.add_subplot(1,3,1)
- plt.plot(x,Numerical_Solution,color='red')
- #ax.legend(loc='best')
- plt.title('Numerical Solution')
- # --- right hand plot
- ax = fig.add_subplot(1,3,2)
- plt.plot(x,Analytic_Solution,color='blue')
- plt.title('Analytic Solution')
- #ax.legend(loc='best')
- ax = fig.add_subplot(1,3,3)
- plt.plot(x,Analytic_Solution-Numerical_Solution,color='blue')
- plt.title('Error')
- # --- title, explanatory text and save
- fig.suptitle('Exponential Growth Solution', fontsize=20)
- plt.tight_layout()
- plt.subplots_adjust(top=0.85)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement