Shift+Entrée
Ctrl+Shift+p
%xmode Plain
import matplotlib.pyplot as plt
import numpy as np
import math
%matplotlib inline
import matplotlib as mpl
mpl.style.use('fivethirtyeight')
from scipy.integrate import odeint
def euler(F, y, temps):
y = np.array(y)
res = [y]
for k in range(len(temps)-1):
t, h = temps[k], temps[k+1]-temps[k]
y = y + h * np.array(F(y, t))
res.append(y)
return np.array(res)
EquaDiff : $y'=y$
def F(y, t):
""" Équation différentielle y'=y """
return y
x = np.linspace(0, 1, 10)
y_exp = np.exp(x)
plt.plot(x, y_exp, label="exp")
y_euler = euler(F, 1, x)
plt.plot(x, y_euler, '+', label='euler')
plt.legend()
def F(V, t):
""" Équation différentielle y''=-sin(y) """
theta, theta_prime = V
return (theta_prime, -math.sin(theta))
x = np.linspace(0, 10, 100)
res = euler(F, (0, 1), x)
plt.plot(x, res, '+', label='euler')
plt.legend()
x = np.linspace(0, 10, 100)
res = odeint(F, (0, 1), x)
plt.plot(x, res, '+', label='euler')
plt.legend()
x = np.linspace(0, 10, 100)
res = euler(F, (0, 1), x)
theta, theta_prime = res[:, 0], res[:, 1]
plt.plot(theta, theta_prime)
x = np.linspace(0, 10, 100)
res = odeint(F, (0, 1), x)
theta, theta_prime = res[:, 0], res[:, 1]
plt.plot(theta, theta_prime)
def euler_pendule(theta, theta_prime, temps):
res = [(theta, theta_prime)]
for k in range(len(temps)-1):
t, h = temps[k], temps[k+1]-temps[k]
theta = theta + h * theta_prime
theta_prime = theta_prime + h * (-sin(theta)) # ATTENTION : pas la bonne formule
res.append((theta, theta_prime))
return np.array(res)
def euler_pendule(theta, theta_prime, temps):
res = [(theta, theta_prime)]
for k in range(len(temps)-1):
t, h = temps[k], temps[k+1]-temps[k]
ancien_theta = theta
theta = theta + h * theta_prime
theta_prime = theta_prime + h * (-sin(ancien_theta))
res.append((theta, theta_prime))
return np.array(res)
def euler_pendule(theta, theta_prime, temps):
res = [(theta, theta_prime)]
for k in range(len(temps)-1):
t, h = temps[k], temps[k+1]-temps[k]
theta = res[k][0] + h * res[k][1]
theta_prime = res[k][1] + h * (-sin(res[k][0]))
res.append((theta, theta_prime))
return np.array(res)