In [4]:
import numpy as np

METHODE D'EULER EXPLICITE¶

In [10]:
def f(t, y):
    return -y + t + 1

# Conditions initiales
t0 = 0
y0 = 1
# Pas de temps
h = 0.1

# Nombre d'itérations
N = 5

# Tableaux pour stocker les résultats
t = np.zeros(N+1)
y = np.zeros(N+1)

# Initialisation
t[0] = t0
y[0] = y0
# Boucle de calcul
for i in range(N):
    y[i+1] = y[i] + h*f(t[i], y[i])
    t[i+1] = t[i] + h
    #Calcul de la solution exacte et de l'erreur
# Affichage des résultats
print("   t         y")
for i in range(N+1):# Sans N+1, le resultat à t=0.5 ne s'affiche pas!
    print("{:.1f}    {:.5f}".format(t[i], y[i]))
   t         y
0.0    1.00000
0.1    1.00000
0.2    1.01000
0.3    1.02900
0.4    1.05610
0.5    1.09049

methode d'Euler avec le calcul des erreurs¶

In [2]:
import numpy as np
import math

def f(t, y):
    return -y + t + 1

h = 0.1
t0, y0 = 0, 1
n = 5

t = np.zeros(n+1)
y = np.zeros(n+1)
e = np.zeros(n+1)

t[0] = t0
y[0] = y0

print("Iteration \t t \t\t y \t\t Error")

# boucle pour le calcul des valeurs

for i in range(n):
    y[i+1] = y[i] + h * f(t[i], y[i])
    t[i+1] = t[i] + h
    e[i+1] = abs(y[i+1] - (math.exp(-t[i+1]) + t[i+1]))
    print("%d \t\t %.1f \t\t %.6f \t %.6f" % (i+1, t[i+1], y[i+1], e[i+1]))
   
Iteration 	 t 		 y 		 Error
1 		 0.1 		 1.000000 	 0.004837
2 		 0.2 		 1.010000 	 0.008731
3 		 0.3 		 1.029000 	 0.011818
4 		 0.4 		 1.056100 	 0.014220
5 		 0.5 		 1.090490 	 0.016041

METHODE DE TAYLOR¶

In [19]:
import numpy as np
import math

def fonction(t, y):
    return -y + 1 + t

# Condition initiale
t0 = 0
y0 = 1

# Pas de temps
h = 0.1

# Nombre d'itérations
n = 5

# Initialisation des tableaux pour stocker les résultats et les erreurs
t_array = np.zeros(n+1)
y_array = np.zeros(n+1)
erreur_array = np.zeros(n+1)

# Première valeur
t_array[0] = t0
y_array[0] = y0
erreur_array[0] = 0

# Boucle pour les itérations suivantes
for i in range(n):
    t = t_array[i]
    y = y_array[i]
    
    # Calcul des dérivées nécessaires
    y_p = fonction(t, y)
    y_pp = -y_p + 1
    
    # Calcul de la nouvelle valeur de y
    y_n = y + h*y_p + (h**2)/2*y_pp
    
    # Calcul de la nouvelle valeur de t
    t_n = t + h
    
    # Calcul de l'erreur commise
    erreur = np.abs(y_n - (math.exp(-t_n) + t_n))
    
    # Stockage des résultats dans les tableaux
    t_array[i+1] = t_n
    y_array[i+1] = y_n
    erreur_array[i+1] = erreur

# Affichage des résultats
print("t\t y(t) \t\t erreur")
for i in range(n+1):
    print("{:.1f}\t {:.6f}\t {:.8f}".format(t_array[i], y_array[i], erreur_array[i]))
t	 y(t) 		 erreur
0.0	 1.000000	 0.00000000
0.1	 1.005000	 0.00016258
0.2	 1.019025	 0.00029425
0.3	 1.041218	 0.00039940
0.4	 1.070802	 0.00048190
0.5	 1.107076	 0.00054511

METHODE DE RUNGE-KUTTA¶

In [18]:
import numpy as np
import math
# Fonction pour calculer la dérivée y'(t)
def f(t, y):
    return -y + 1 + t

# Méthode de Runge-Kutta d'ordre 4
def runge_kutta(t, y, h):
    k1 = h * f(t, y)
    k2 = h * f(t + h/2, y + k1/2)
    k3 = h * f(t + h/2, y + k2/2)
    k4 = h * f(t + h, y + k3)
    return y + (k1 + 2*k2 + 2*k3 + k4)/6

# Paramètres de l'équation différentielle
y0 = 1  # condition initiale y(0) = 1
h = 0.1  # pas de temps
t0=0    # temps initial
# Tableaux pour stocker les résultats et les erreurs
t_array = np.zeros(n+1)
y_array = np.zeros(n+1)
erreur_array = np.zeros(n+1)

# Conditions initiales
t_array[0] = t0
y_array[0] = y0
# Boucle pour calculer les cinq premières itérations
for i in range(n):
    t_array[i+1] = t_array[i] + h
    y_array[i+1] = runge_kutta(t_array[i], y_array[i], h)
    erreur_array[i+1] = abs(y_array[i+1] - (math.exp(-t_array[i+1]) + t_array[i+1]))

# Afficher les résultats et les erreurs

print("t\t y(t) \t\t erreur")
for i in range(n):
    print("{:.1f}\t {:.6f}\t {:.15f}".format( t_array[i+1],y_array[i+1],erreur_array[i+1]))
t	 y(t) 		 erreur
0.1	 1.004838	 0.000000081964040
0.2	 1.018731	 0.000000148328268
0.3	 1.040818	 0.000000201319460
0.4	 1.070320	 0.000000242881851
0.5	 1.106531	 0.000000274710747
In [ ]: