CoCalc Public FilesNew Projects and Problems / Euler's Method.ipynbOpen in with one click!
Author: Peter Francis
Euler's Method
In [1]:
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) @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 [3]:
fig, ax = plt.subplots(figsize=(20,20)) X_0,Y_0 = EMethod(0,10,1,0) X_1,Y_1 = EMethod(0,10,.5,0) X_2,Y_2 = EMethod(0,10,.1,0) X_3,Y_3 = EMethod(0,10,.00001,0) ax.plot(X_0,Y_0,'.', ms=10); ax.plot(X_1,Y_1,'.', ms=5); ax.plot(X_2,Y_2,'.', ms=3); ax.plot(X_3,Y_3,'.', ms=1); X = np.arange(0,10,.001) Y = np.array([np.sin(x) for x in X]) ax.plot(X,Y);
In [ ]: