CoCalc Shared Files2.2 / Physics 255 / Euler's Method.ipynbOpen in CoCalc with one click!
Author: Peter Francis
Views : 31
Euler's Method
In [4]:
import numpy as np from numba import njit import matplotlib.pyplot as plt from matplotlib import collections as mc
In [2]:
@njit def fprime(x): return np.cos(x)*x**(1/2) @njit def EMethod(xstart, xend, deltax, ystart): outputX = np.arange(xstart,xend+deltax,deltax) outputY = np.array([ystart]*len(outputX), dtype = np.float64) for i in range(1,len(outputX)): outputY[i] = outputY[i-1] + fprime(outputX[i])*deltax return (outputX,outputY)
In [5]:
fig, ax = plt.subplots(figsize=(10,10)) X_1,Y_1 = EMethod(0,10,.5,4) X_2,Y_2 = EMethod(0,10,.1,4) X_3,Y_3 = EMethod(0,10,.001,4) ax.plot(X_1,Y_1,'.', ms=5); ax.plot(X_2,Y_2,'.', ms=3); ax.plot(X_3,Y_3,'.', ms=1);