feat: working newton schema

This commit is contained in:
git-sandro 2026-03-17 22:27:13 +01:00
parent 5638b8aa38
commit dac54d71a2

View File

@ -3,11 +3,9 @@ import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
# %% # %%
print("----------------------------------------------------------------") print(60 * "-")
print(__file__) print(__file__)
print("Aufgabe 2. Interpolationspolynome gemäss NEWTON-Schema berechnen") print("Aufgabe 2. Interpolationspolynome gemäss NEWTON-Schema berechnen")
print("----------------------------------------------------------------")
# punkte [[x0, x1, x2, xn], [y0, y1, y2, yn]] # punkte [[x0, x1, x2, xn], [y0, y1, y2, yn]]
punkte = [ punkte = [
[np.array([4, 8]), np.array([1, -1])], [np.array([4, 8]), np.array([1, -1])],
@ -21,30 +19,43 @@ for punkt in punkte:
x_data = punkt[0] x_data = punkt[0]
y_data = punkt[1] y_data = punkt[1]
x_0 = x_data[0] x_0 = x_data[0]
x_e = x_data[-1] x_E = x_data[-1]
N = 201 N = 201
lw = 30 lw = 3
fig = 1 fig = 1
# Berechnung # Berechnung
n = np.size(y_data) n = np.size(y_data)
TAB = np.block([[y_data], [np.zeros((n-1, n))]]) TAB = np.block([[y_data], [np.zeros((n - 1, n))]])
c_data = np.zeros(n) c_data = np.zeros(n)
c_data[0] = y_data[0] c_data[0] = y_data[0]
print(x_data)
print(TAB)
for i in range(1, n): for i in range(1, n):
for j in range(1, n): for j in range(1, n):
print(TAB[i][j-1]) TAB[i][j] = (TAB[i - 1][j] - TAB[i - 1][j - 1]) / (
print(TAB[i-1][j-1]) x_data[j] - x_data[j - i]
print(x_data[i]) )
print(x_data[i-1]) c_data[i] = TAB[i][i]
TAB[i][j] = (TAB[i][j-1]-TAB[i-1][j-1])/(x_data[i]-x_data[i-1])
break
print(TAB) # Funktionen:
print() def p(x):
d = 1
y = c_data[0]
for k in range(1, n):
d = d * (x - x_data[k - 1])
y = y + c_data[k] * d
return y
print("----------------------------------------------------------------") # Daten
u_data = np.linspace(x_0, x_E, N)
v_data = p(u_data)
fh = plt.figure(fig)
plt.plot(u_data, v_data, linewidth=lw)
plt.plot(x_data, y_data, "o", linewidth=lw)
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
plt.grid(visible=True)
plt.axis("image")
print(60 * "-")