Feat: Ajoute les fonctions de calcul d'equa diff

This commit is contained in:
Namu
2026-01-19 10:33:51 +01:00
parent f6c69efb16
commit 0db8d58d8f

87
tp8.py
View File

@@ -1,24 +1,93 @@
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
def euler(f, n: int, delta_x: float, x_0: float, y_0: float) -> list[float]: def euler(f, n: int, h: float, x_0: float, y_0: float) -> tuple[list[float], list[float]]:
results = [] """
:param f: la fonction f (ou y)
:param n: jusqu'où va le calcul (le nombre d'itérations)
:param h: le pas de chaque itération
:param x_0: dans la condition initiale, c'est la valeur passée en entrée de f
:param y_0: dans la condition initiale, c'est la valeur de sortie de f.
:return:
"""
results_x = [x_0]
results_y = [y_0]
# h c'est delta_x
x = x_0 x = x_0
y = y_0 y = y_0
for i in range(n): for i in range(n):
x = x + delta_x if i < 4:
y = y + delta_x * f(x, y) x = x + h
results.append(y) y = y + h * f(x, y)
else:
h = .3
x = x + h
y = y + h * f(x, y)
results_x.append(x)
results_y.append(y)
return results return results_x, results_y
def runge_kutta(f, n: int, h: float, x_0: float, y_0: float) -> tuple[list[float], list[float]]:
results_x = [x_0]
results_y = [y_0]
x = x_0
y = y_0
for i in range(n):
if i < 4:
k_1 = f(x, y)
k_2 = f(x + (h / 2), y + (h / 2) * k_1)
x = x + h
y = y + h * k_2
else:
h = .3
k_1 = f(x, y)
k_2 = f(x + (h / 2), y + (h / 2) * k_1)
x = x + h
y = y + h * k_2
results_x.append(x)
results_y.append(y)
return results_x, results_y
def taylor(f, f_derive_x, f_derive_y, n: int, h: float, x_0: float, y_0: float) -> tuple[list[float], list[float]]:
results_x = [x_0]
results_y = [y_0]
x = x_0
y = y_0
for i in range(n):
if i < 4:
x = x + h
y = y + h * f(x, y) + (h ** 2) / 2 * (f_derive_x(x, y) + f_derive_y(x, y) * f(x, y))
else:
h = .3
x = x + h
y = y + h * f(x, y) + (h ** 2) / 2 * (f_derive_x(x, y) + f_derive_y(x, y) * f(x, y))
results_x.append(x)
results_y.append(y)
return results_x, results_y
def exercice1() -> None: def exercice1() -> None:
f = lambda x, y: -2 * x * y + 1 f = lambda x, y: -2 * x * y + 1
results = euler(f, n=20, x_0=0, y_0=1, delta_x=.2) f_derive_x = lambda x, y: -2 * y + 1
plt.plot(results) f_derive_y = lambda x, y: -2 * x + 1
plt.title('Euler')
results_x_euler, results_y_euler = euler(f, n=5, x_0=0, y_0=1, h=.2)
results_x_runge, results_y_runge = runge_kutta(f, n=5, x_0=0, y_0=1, h=.2)
results_x_taylor, results_y_taylor = taylor(f, f_derive_x, f_derive_y, 5, x_0=0, y_0=1, h=.2)
plt.plot(results_x_euler, results_y_euler, color='green')
plt.plot(results_x_runge, results_y_runge, color='red')
plt.plot(results_x_taylor, results_y_taylor, color='blue')
plt.title('La giga fonction pour les equations différentielles. (mieux que le sex garanti 3 ans)')
plt.show() plt.show()