ϕ(x)=e−x22
on pose
Pn(x)=(−1)nn!ex22ϕ(n)(x)
pour calculer facilement ces polynômes, si l'on n'a pas l'expression de leurs coefficients, on peut dériver plusieurs fois formellement, et sympy s'impose:
from sympy import symbols, diff, exp, factorial, simplify, expand
import sympy as sp
def phi(x):
return(exp(-x**2/2))
X = sp.symbols('X')
def hermite(n):
P = (-1)**n/factorial(n)*exp(X**2/2)*diff(phi(X),X,n)
return(expand(simplify(P)))
on obtient par exemple:
print([(n,hermite(n)) for n in range(5)])
>>> [(0, 1), (1, X), (2, X**2/2 - 1/2), (3, X**3/6 - X/2), (4, X**4/24 - X**2/4 + 1/8)]
d'où
P4=X424−X24+18
Pour le tracé, utilisons matplotlib, plus souple (*).
Le code python est ici: polynomes_hermite.py
Le tracé:
Note: si on veut faire cohabiter sans problème sympy et matplotlib, il faut les importer avec précautions, pas tout d'un bloc:
from sympy import symbols, diff, exp, factorial, simplify, expand
import sympy as sp
import numpy as numpy
import matplotlib.pyplot as pyplot
Aucun commentaire:
Enregistrer un commentaire