yusufbrima

Euler method Generalisation

Oct 22nd, 2020
1,113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.integrate import odeint
  4.  
  5. %matplotlib inline
  6.  
  7.  
  8. def F(t,k):
  9.     x,y,z = k
  10.    
  11.     Q = np.empty(3)
  12.     Q[0] = x  + 3
  13.    
  14.     Q[1] = x  + 3 *y +z
  15.    
  16.     Q[2] = x  + y**2
  17.    
  18.     return Q
  19.  
  20. def EulerMethod(a,b,c,N,F):
  21.     H = (b-a)/N
  22.    
  23.     t = np.linspace(a,b,N+1)
  24.    
  25.     Z = np.zeros((N+1,len(c)))
  26.    
  27.     Z[0,:] = c
  28.     for k in range(0,N):
  29.         Z[k+1] = Z[k] +  H*F(t[k],Z[k])
  30.     return t,Z
  31.  
  32.  
  33.  
  34. a = 0
  35. b = 1
  36. N = 100
  37. c = np.array([1,2,1])
  38.  
  39.  
  40. t,result = EulerMethod(a,b,c,N,F)
  41. result
RAW Paste Data