feat: working newton schema
This commit is contained in:
parent
5638b8aa38
commit
dac54d71a2
@ -3,11 +3,9 @@ import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# %%
|
||||
print("----------------------------------------------------------------")
|
||||
print(60 * "-")
|
||||
print(__file__)
|
||||
print("Aufgabe 2. Interpolationspolynome gemäss NEWTON-Schema berechnen")
|
||||
print("----------------------------------------------------------------")
|
||||
|
||||
# punkte [[x0, x1, x2, xn], [y0, y1, y2, yn]]
|
||||
punkte = [
|
||||
[np.array([4, 8]), np.array([1, -1])],
|
||||
@ -21,9 +19,9 @@ for punkt in punkte:
|
||||
x_data = punkt[0]
|
||||
y_data = punkt[1]
|
||||
x_0 = x_data[0]
|
||||
x_e = x_data[-1]
|
||||
x_E = x_data[-1]
|
||||
N = 201
|
||||
lw = 30
|
||||
lw = 3
|
||||
fig = 1
|
||||
|
||||
# Berechnung
|
||||
@ -31,20 +29,33 @@ for punkt in punkte:
|
||||
TAB = np.block([[y_data], [np.zeros((n - 1, n))]])
|
||||
c_data = np.zeros(n)
|
||||
c_data[0] = y_data[0]
|
||||
print(x_data)
|
||||
|
||||
print(TAB)
|
||||
|
||||
for i in range(1, n):
|
||||
for j in range(1, n):
|
||||
print(TAB[i][j-1])
|
||||
print(TAB[i-1][j-1])
|
||||
print(x_data[i])
|
||||
print(x_data[i-1])
|
||||
TAB[i][j] = (TAB[i][j-1]-TAB[i-1][j-1])/(x_data[i]-x_data[i-1])
|
||||
break
|
||||
TAB[i][j] = (TAB[i - 1][j] - TAB[i - 1][j - 1]) / (
|
||||
x_data[j] - x_data[j - i]
|
||||
)
|
||||
c_data[i] = TAB[i][i]
|
||||
|
||||
print(TAB)
|
||||
print()
|
||||
# Funktionen:
|
||||
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 * "-")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user