In mathematics and computational science, the Euler method (also called forward Euler method) is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value. It is the most basic explicit method for numerical integration of ordinary differential equations and is the simplest Runge–Kutta method. The Euler method is named after Leonhard Euler, who treated it in his book Institutionum calculi integralis (published 1768–1870).
import matplotlib.pyplot as plt
import numpy as np
import math
from math import tan
def Euler(x,x1,y,n):
h=(x1-x)/n
for i in range(1,n+1):
F1=F(x,y)
y+=F1*h
x+=h
Xlist.append(float(x))
Ylist.append(y)
print(i," ",x," ",i," ",y," ",(tan(x)-x))
#simple example for checking, how this method work
def F(X,Y):
#return X**2 - Y*2#
return Y + (math.e**X)/X
Xlist =[]
Ylist =[]
x=1.
x1=2.
n=10
y=1
print("Euler method")
Euler(x,x1,y,n)
Euler method
1 1.1 1 1.3718281828459045 0.8647596572486522
2 1.2000000000000002 2 1.7821170033074434 1.37215162212632
3 1.3000000000000003 3 2.2370051138662332 2.3021024479679815
4 1.4000000000000004 4 2.7429592150697215 4.397883715482902
5 1.5000000000000004 5 3.306912277065599 12.601419947171808
6 1.6000000000000005 6 3.93638277612803 -35.832532735556796
7 1.7000000000000006 7 4.6395855802655275 -9.396602139459121
8 1.8000000000000007 8 5.425541043687798 -6.086261674628051
9 1.9000000000000008 9 6.304186673857298 -4.8270975146777655
10 2.000000000000001 10 7.28649452241562 -4.185039863261515