La fonction euler prend en paramètres un nombre x, un nombre y0 et retourne la valeur y(x) pour la condition initiale y(0)=y0.
def euler(x,y0):
y = y0
n = 10**6
p = 1.0*x/n
for i in range(0,n):
y = y*(1.0+p)
return y
print euler(1,1)
print euler(2,1)
Équation y′=f
La fonction euler prend en paramètres une fonction f, un nombre x et la condition initiale (x0;y0). Elle retourne la valeur de y(x).
def f(x):
return x*x
def t(x):
return 1.0/x
def euler(f,x,x0,y0):
n = 10**5
p = 1.0*x/n
y = y0
x = x0
for i in range(0,n):
y = p*f(x)+y
x = x + p
return y
print euler(f,2,0,0)
print euler(t,10,1,0)
Équation y′=ay+b
La fonction euler prend en paramètres un nombre x, les nombres a et b (de y′=ay+b) et un nombre y0. Elle retourne la valeur y(x) pour la condition initiale y(0)=y0.
def euler(x,a,b,y0):
n = 10**5
p = 1.0*x/n
y = y0
for i in range(0,n):
y = (a*y+b)*p+y
return y
print euler(1,1,1,1)