Skip to content
Snippets Groups Projects
Commit 10532367 authored by iliya.saroukha's avatar iliya.saroukha :first_quarter_moon:
Browse files

feat: plotting errors on interpolation polynomial

parent 1544b9f4
No related branches found
No related tags found
No related merge requests found
...@@ -139,6 +139,8 @@ print() ...@@ -139,6 +139,8 @@ print()
print(f"valeur de la fonction en x = {SD.Taylor_points} : { print(f"valeur de la fonction en x = {SD.Taylor_points} : {
[SD.f(x) for x in SD.Taylor_points]}") [SD.f(x) for x in SD.Taylor_points]}")
print(f"valeur de la fonction en a et b: {SD.f(SD.a), SD.f(SD.b)}") print(f"valeur de la fonction en a et b: {SD.f(SD.a), SD.f(SD.b)}")
# print("===============================")
# print(SD.Maximal_derivatives_values)
# Exemple de graphe de la fonction f. # Exemple de graphe de la fonction f.
# t = np.linspace(SD.a, SD.b, Nmbre_pts) # t = np.linspace(SD.a, SD.b, Nmbre_pts)
...@@ -205,9 +207,11 @@ def ex2_taylor_poly(): ...@@ -205,9 +207,11 @@ def ex2_taylor_poly():
def ex3_lagrange_interpolation_poly(): def ex3_lagrange_interpolation_poly():
nb_points = np.linspace(3, 19, 6, dtype=np.uint64) nb_points = np.linspace(1, 13, 6, dtype=np.uint64)
fig, axes = plt.subplots(2, 3, figsize=(20, 12)) fig, axes = plt.subplots(2, 3, figsize=(20, 12))
t = np.linspace(SD.a, SD.b, Nmbre_pts)
for i, ax in enumerate(axes.flat): for i, ax in enumerate(axes.flat):
chebyshev_points = np.cos( chebyshev_points = np.cos(
(2 * np.arange(nb_points[i]) + 1) / (2 * nb_points[i]) * np.pi) (2 * np.arange(nb_points[i]) + 1) / (2 * nb_points[i]) * np.pi)
...@@ -221,13 +225,15 @@ def ex3_lagrange_interpolation_poly(): ...@@ -221,13 +225,15 @@ def ex3_lagrange_interpolation_poly():
l_poly_chebyshev_pts = lagrange( l_poly_chebyshev_pts = lagrange(
chebyshev_points_mapped, SD.f(chebyshev_points_mapped)) chebyshev_points_mapped, SD.f(chebyshev_points_mapped))
t = np.linspace(SD.a, SD.b, Nmbre_pts)
ax.plot(t, SD.f(t), color='black', label='f') ax.plot(t, SD.f(t), color='black', label='f')
ax.plot(t, l_poly_uniform(t), color='red', ax.plot(t, l_poly_uniform(t), color='red',
label='$L_{f}$, intervalle équidistants') label='$L_{f}$, intervalle équidistants')
ax.plot(t, np.abs(SD.f(t) - l_poly_uniform(t)), '--', color='red',
label='$L_{f}$, intervalle équidistants, erreur')
ax.plot(t, l_poly_chebyshev_pts(t), color='blue', ax.plot(t, l_poly_chebyshev_pts(t), color='blue',
label='$L_{f}$, points de Chebyshev') label='$L_{f}$, points de Chebyshev')
ax.plot(t, np.abs(SD.f(t) - l_poly_chebyshev_pts(t)), '--', color='blue',
label='$L_{f}$, points de Chebyshev, erreur')
ax.plot(interpolate_pts, SD.f(interpolate_pts), 'o', color='red', ax.plot(interpolate_pts, SD.f(interpolate_pts), 'o', color='red',
label='Points équidistants') label='Points équidistants')
ax.plot(chebyshev_points_mapped[::-1], ax.plot(chebyshev_points_mapped[::-1],
...@@ -244,85 +250,71 @@ def ex3_lagrange_interpolation_poly(): ...@@ -244,85 +250,71 @@ def ex3_lagrange_interpolation_poly():
plt.show() plt.show()
def ex3_newton_interpolation_poly(): ex3_lagrange_interpolation_poly()
# merce l'ami
def divided_differences(x, y):
n = len(y) # def ex3_newton_interpolation_poly():
coef = np.zeros([n, n]) # # merce l'ami
coef[:, 0] = y # def divided_differences(x, y):
# n = len(y)
for j in range(1, n): # coef = np.zeros([n, n])
for i in range(n - j): # coef[:, 0] = y
coef[i, j] = (coef[i + 1, j - 1] - coef[i, j - 1]) / \ #
(x[i + j] - x[i]) # for j in range(1, n):
# for i in range(n - j):
return coef[0, :] # coef[i, j] = (coef[i + 1, j - 1] - coef[i, j - 1]) / \
# (x[i + j] - x[i])
def newton_polynomial(x, x_points, coef): #
n = len(coef) # return coef[0, :]
p = coef[n - 1] #
for k in range(1, n): # def newton_polynomial(x, x_points, coef):
p = coef[n - k - 1] + (x - x_points[n - k - 1]) * p # n = len(coef)
# p = coef[n - 1]
return p # for k in range(1, n):
# p = coef[n - k - 1] + (x - x_points[n - k - 1]) * p
nb_points = np.linspace(3, 19, 6, dtype=np.uint64) #
fig, axes = plt.subplots(2, 3, figsize=(20, 12)) # return p
#
t = np.linspace(SD.a, SD.b, Nmbre_pts) # nb_points = np.linspace(3, 19, 6, dtype=np.uint64)
# fig, axes = plt.subplots(2, 3, figsize=(20, 12))
for i, ax in enumerate(axes.flat): #
chebyshev_points = np.cos(
(2 * np.arange(nb_points[i]) + 1) / (2 * nb_points[i]) * np.pi)
chebyshev_points_mapped = 0.5 * \
(SD.b - SD.a) * (chebyshev_points + 1) + SD.a
interpolate_pts = np.linspace(SD.a, SD.b, nb_points[i])
y_points_uni = SD.f(interpolate_pts)
coef_uni = divided_differences(interpolate_pts, y_points_uni)
y_plot_uni = newton_polynomial(t, interpolate_pts, coef_uni)
y_points_cheb = SD.f(chebyshev_points_mapped)
coef_cheb = divided_differences(chebyshev_points_mapped, y_points_cheb)
y_plot_cheb = newton_polynomial(t, chebyshev_points_mapped, coef_cheb)
ax.plot(t, SD.f(t), color='black', label='f')
ax.plot(t, y_plot_uni, color='red',
label='$N_{f}$, intervalle équidistants')
ax.plot(t, y_plot_cheb, color='blue',
label='$N_{f}$, points de Chebyshev')
ax.plot(interpolate_pts, SD.f(interpolate_pts), 'o', color='red',
label='Points équidistants')
ax.plot(chebyshev_points_mapped[::-1],
SD.f(chebyshev_points_mapped[::-1]), 'o', color='blue',
label='Points de Chebyshev')
ax.set_title(f'n = {nb_points[i]}')
ax.set_ylim([-1.2, 1.2])
ax.legend()
fig.suptitle(f'Polynôme d\'interpolation de Newton de $f$ avec 2 subdivisions différentes d\'intervalle: Équidistantes (rouge) / Points de Chebyshev (bleu)')
fig.tight_layout()
plt.show()
ex3_newton_interpolation_poly()
# Graphique des polynômes de Taylor
# fig, axes = plt.subplots(1, 3)
# for i, pt in enumerate(SD.Taylor_points):
# ax = axes[i]
# t = np.linspace(SD.a, SD.b, Nmbre_pts) # t = np.linspace(SD.a, SD.b, Nmbre_pts)
# ax.plot(t, SD.f(t), label="Fonction f(x)") #
# taylor_poly = np.poly1d( # for i, ax in enumerate(axes.flat):
# SD.Taylor_derivatives_values[SD.Taylor_points[i]][::-1]) # chebyshev_points = np.cos(
# ax.plot(t, taylor_poly(t), label=f"Polynôme de Taylor en x={pt}") # (2 * np.arange(nb_points[i]) + 1) / (2 * nb_points[i]) * np.pi)
# ax.legend() #
# ax.set_title(f"Polynôme de Taylor en x={pt}") # chebyshev_points_mapped = 0.5 * \
# plt.show() # (SD.b - SD.a) * (chebyshev_points + 1) + SD.a
#
# interpolate_pts = np.linspace(SD.a, SD.b, nb_points[i])
#
# y_points_uni = SD.f(interpolate_pts)
#
# coef_uni = divided_differences(interpolate_pts, y_points_uni)
# y_plot_uni = newton_polynomial(t, interpolate_pts, coef_uni)
#
# y_points_cheb = SD.f(chebyshev_points_mapped)
#
# coef_cheb = divided_differences(chebyshev_points_mapped, y_points_cheb)
# y_plot_cheb = newton_polynomial(t, chebyshev_points_mapped, coef_cheb)
#
# ax.plot(t, SD.f(t), color='black', label='f')
# ax.plot(t, y_plot_uni, color='red',
# label='$N_{f}$, intervalle équidistants')
# ax.plot(t, y_plot_cheb, color='blue',
# label='$N_{f}$, points de Chebyshev')
# ax.plot(interpolate_pts, SD.f(interpolate_pts), 'o', color='red',
# label='Points équidistants')
# ax.plot(chebyshev_points_mapped[::-1],
# SD.f(chebyshev_points_mapped[::-1]), 'o', color='blue',
# label='Points de Chebyshev')
# ax.set_title(f'n = {nb_points[i]}')
# ax.set_ylim([-1.2, 1.2])
#
# ax.legend()
#
# fig.suptitle(f'Polynôme d\'interpolation de Newton de $f$ avec 2 subdivisions différentes d\'intervalle: Équidistantes (rouge) / Points de Chebyshev (bleu)')
#
# fig.tight_layout()
# plt.show()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment