1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import numpy as np
import matplotlib.pyplot as plt
def P(n,x) :
if n not in p_dict:
# appels récursifs :
p_dict[n] = ((2*n-1)/n)*x*P(n-1,x) - P(n-2,x)*(n-1)/n
return p_dict[n]
def P_leg(n,x):
global p_dict
if n==0:
return 1
# initialisation du dico : 2 première valeurs 1 et x
p_dict = {0: 1, 1: x}
# appel de la fonction récursive Pn(n,x) et renvoi de son résultat
return P(n,x)
n=3
a=-1
b=1
x = np.linspace(a, b, 1000)
y = [P_leg(n,xi) for xi in x]
plt.plot(x, y, label="P_leg(n,x)")
plt.legend()
plt.show() |
Partager