Académique Documents
Professionnel Documents
Culture Documents
1)b)
x = [-2, 0, 2]
t_values = np.linspace(-2, 2, 5)
w0_values = BaseNevton(t_values, 0, x)
w1_values = BaseNevton(t_values, 1, x)
w2_values = BaseNevton(t_values, 2, x)
2)a)
def diffdiv(x, y):
n = len(x)
diffs = [y[i] for i in range(n)]
for j in range(1, n):
for i in range(n - 1, j - 1, -1):
diffs[i] = (diffs[i] - diffs[i - 1]) / (x[i] - x[i - j])
return diffs
2)b)
x = [-2, 0, 2]
y = [4, 3, 6]
coefficients = diffdiv(x, y)
B0, B1, B2 = coefficients[0], coefficients[1], coefficients[2]
print("Les coefficients de Newton sont :")
print("B0 =", B0)
print("B1 =", B1)
print("B2 =", B2)
3)a)
def InterpolationNewton(t, x, y):
beta = diffdiv(x, y)
for i in range(len(x)):
term = beta[i]
for j in range(i):
term *= (t - x[j])
P += term
return P
3)b)
x = [-2, 0, 2]
y = [4, 3, 6]
t = np.linspace(-2, 2, 5)
P = InterpolationNewton(t, x, y)
print(P)
3)c)
x = [-2, 0, 2]
y = [4, 3, 6]
t = np.linspace(-2, 2, 100)
P = InterpolationNewton(t, x, y)
Q = 0.5 * t**2 + 0.5 * t + 3
plt.plot(t, P, 'mo', t, Q, 'b')
plt.plot(, label='Polynôme Q')
plt.grid(True)
plt.legend(('Polynôme d\'interpolation P', 'Polynôme Q'))
Exe 2
1)
def methode1(f, x):
N = len(x) - 1
I = 0.0
for k in range(N):
xk = x[k]
xk1 = x[k+1]
width = xk1 - xk
midpoint = (xk + xk1) / 2.0
I += f(midpoint) * width
return I
2)
def methode2(f, x):
N = len(x) - 1
I = 0.0
for k in range(N):
xk = x[k]
xk1 = x[k+1]
width = xk1 - xk
midpoint = (xk + xk1) / 2.0
I += (f(xk) + f(xk1)) / 2.0 * width
return I
3)a)
x = np.linspace(0, 5, 5)
def f(t):
return 1 / (1 + (t + np.pi)**2)
I1 = methode1(f, x)
I2 = methode2(f, x)
3)c)
I_approx_methode1 = methode1(f, x)
I_approx_methode2 = methode2(f, x)
erreur_methode1 = abs(I_approx_methode1 - I_exact)
erreur_methode2 = abs(I_approx_methode2 - I_exact)
print("Erreur d'intégration en utilisant la méthode 1:", erreur_methode1)
print("Erreur d'intégration en utilisant la méthode 2:", erreur_methode2)
3)d)
N_values = [30, 40, 50, 60]
for i in range(len(N_values)):
N = N_values[i]
error_methode1 = abs(methode1(f, x) - I_exact)
error_methode2 = abs(methode2(f, x) - I_exact)
print("Erreurs d'intégration pour N =", N)
print("Méthode 1 :", error_methode1)
print("Méthode 2 :", error_methode2)
print()
e)
for i in range(len(N_values)):
N = N_values[i]
error_methode1 = abs(methode1(f, x) - I_exact)
error_methode2 = abs(methode2(f, x) - I_exact)