Académique Documents
Professionnel Documents
Culture Documents
ipynb - Colaboratory
# Importation de la libretie
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
QUESTION 1
Fonction Python qui utilise la méthode d’Euler pour résoudre une équation différentielle de la forme y′=f(t,y) avec y(a)=y0.
# Affichage de la solution
for t, y in solution:
print(f"t = {t}, y = {y}")
https://colab.research.google.com/drive/1VlkHzhW19b2Vmacx5wZ4RIc-SbrPLJhL#printMode=true 1/8
22/01/2024 21:29 TP1HYSNUM.ipynb - Colaboratory
t = 1.6767676767676767, y = 1.0443569149464353
t = 1.696969696969697, y = 1.057132889932723
t = 1.7171717171717171, y = 1.0700588860344802
t = 1.7373737373737375, y = 1.083131872522101
t = 1.7575757575757576, y = 1.0963488798928411
t = 1.7777777777777777, y = 1.10970699863391
t = 1.797979797979798, y = 1.123203378010554
t = 1.8181818181818181, y = 1.1368352248786195
t = 1.8383838383838385, y = 1.1505998025211084
t = 1.8585858585858586, y = 1.1644944295082342
t = 1.878787878787879, y = 1.1785164785805096
t = 1.898989898989899, y = 1.1926633755543958
t = 1.9191919191919191, y = 1.2069325982500625
t = 1.9393939393939394, y = 1.2213216754408072
t = 1.9595959595959596, y = 1.2358281858236988
t = 1.97979797979798, y = 1.2504497570110171
t = 2.0, y = 1.2651840645420669
Programme Python qui utilise la fonction euler pour résoudre l’équation différentielle y′+y=t sur l’intervalle [0,2] avec la condition initiale y(0)=1.
https://colab.research.google.com/drive/1VlkHzhW19b2Vmacx5wZ4RIc-SbrPLJhL#printMode=true 2/8
22/01/2024 21:29 TP1HYSNUM.ipynb - Colaboratory
# Calcul de l'erreur relative
error = np.abs((y_euler - y_analytical) / y_analytical)
max_error = np.max(error)
print(f'Erreur maximale : {max_error:.2%}')
QUESTION 2
Ce script utilise les fonctions rk2 et rk4 que nous avons écrites précédemment, ainsi que la fonction odeint de la bibliothèque scipy.integrate,
pour résoudre l’équation différentielle y′+y=t avec la condition initiale y(0)=1.
def analytical_solution(t):
return t - 1 + 2 * np.exp(-t)
a, b = 0, 2
y0 = 1
N = 10
t, y_rk2 = rk2(f, a, b, y0, N)
t, y_rk4 = rk4(f, a, b, y0, N)
y_odeint = odeint(f, y0, t).flatten()
y_analytical = analytical_solution(t)
output
Zoom 1
a, b = 1, 2
y0 = 1
N = 100
t, y_rk2 = rk2(f, a, b, y0, N)
t, y_rk4 = rk4(f, a, b, y0, N)
y_odeint = odeint(f, y0, t).flatten()
y_analytical = analytical_solution(t)
https://colab.research.google.com/drive/1VlkHzhW19b2Vmacx5wZ4RIc-SbrPLJhL#printMode=true 4/8
22/01/2024 21:29 TP1HYSNUM.ipynb - Colaboratory
Zoom 2
a, b = 0.6,0.9
y0 = 1
N = 10
t, y_rk2 = rk2(f, a, b, y0, N)
t, y_rk4 = rk4(f, a, b, y0, N)
y_odeint = odeint(f, y0, t).flatten()
y_analytical = analytical_solution(t)
Zoom 3
https://colab.research.google.com/drive/1VlkHzhW19b2Vmacx5wZ4RIc-SbrPLJhL#printMode=true 5/8
22/01/2024 21:29 TP1HYSNUM.ipynb - Colaboratory
a, b = 0.6,0.9
y0 = 1
N = 10
t, y_rk2 = rk2(f, a, b, y0, N)
t, y_rk4 = rk4(f, a, b, y0, N)
y_odeint = odeint(f, y0, t).flatten()
y_analytical = analytical_solution(t)
QUESTION 3
https://colab.research.google.com/drive/1VlkHzhW19b2Vmacx5wZ4RIc-SbrPLJhL#printMode=true 6/8
22/01/2024 21:29 TP1HYSNUM.ipynb - Colaboratory
# Définition des fonctions
def f1(y1, y2):
return y2
# Paramètres
g = 9.81 # accélération due à la gravité en m/s^2
l = 9.81 / 100 # longueur du pendule en m
y1_0 = np.radians(10) # angle initial en radians
y2_0 = 0 # vitesse angulaire initiale en rad/s
t = np.linspace(0, 10, 1000) # intervalle de temps
# Solution numérique
y1, y2 = runge_kutta_4(f1, f2, y1_0, y2_0, t)
Quzstion 4
https://colab.research.google.com/drive/1VlkHzhW19b2Vmacx5wZ4RIc-SbrPLJhL#printMode=true 7/8
22/01/2024 21:29 TP1HYSNUM.ipynb - Colaboratory
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad
# Paramètres
g = 9.81 # accélération due à la gravité en m/s^2
l = 9.81 / 100 # longueur du pendule en m
theta0_values = np.linspace(0.01, np.pi, 100) # valeurs de theta0
https://colab.research.google.com/drive/1VlkHzhW19b2Vmacx5wZ4RIc-SbrPLJhL#printMode=true 8/8