1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import numpy as np
import matplotlib.pyplot as plt
a=-1
b=2
c=1
d=-1
def U(x):
return np.sqrt(a*x**2+b*x+c)+d
def fun(t,u):
return (1-t)/(1+u)
# parametres
t0,tf,y0,n=0,2.4,0,100
temps=np.linspace(t0,tf,n+1)
#euler
def euler(fct,ti,tf,nt,yi):
y=np.zeros(nt+1)
h=(tf-ti)/nt
t=np.linspace(ti,tf,nt+1)
y[0]=yi
for i in np.arange(nt):
y[i+1]=y[i]+h*fct(t[i],y[i])
return t,y
# appel de la fonction
t,y1=euler(fun,t0,tf,n,y0)
plt.plot(t,y1)
plt.title ('Intégration Euler Explicite de f(t,u) ')
plt.xlabel('t')
x=np.linspace(0,2.4)
y=U(x)
plt.plot(x,y, label='U(t)')
plt.plot(t,y1, label='Euler')
plt.title ('Comparaison')
plt.legend()
plt.xlabel('t') |