<jupyter_start><jupyter_text>Cubic Spline InterpolationAufgabenstellung: Testen ob eine Funktion eine kubische Spline-Interpolierende ist. - Gegeben: $n+1$ Punkte. - Gesucht: kubische Spline-Interpolierende $S(x)$ für die gegebenen Punkte<jupyter_code># input: given points
points = [
    (1, -5),
    (3, 3),
    (4, 4),
]
#
points = [vector(p) for p in points]
n = len(points) - 1<jupyter_output><empty_output><jupyter_text>Berechnen $a_0$ bis $a_{n+1}$$$a_i = y_i$$<jupyter_code>a = [p[1] for p in points]
show(a)<jupyter_output><empty_output><jupyter_text>Berechnen $h_0$ bis $h_{n}$$$h_i = x_{i+1} - x_i$$<jupyter_code>h = [points[i+1][0] - points[i][0] for i in range(n)]
show(h)<jupyter_output><empty_output><jupyter_text>Anlegen der Matrix $A \in M(n \times n)$<jupyter_code>A = matrix([[1] + [0] * (n) ] +\
[[0]*i + [h[i], 2*(h[i] + h[i+1]), h[i+1]] + [0]*(n - 2 - i) for i in range(n-1)] \
+ [ [0] * (n) + [1] ])
show(A)<jupyter_output><empty_output><jupyter_text>und des Vektors $\bar{b}$<jupyter_code>bv = vector([0] + [(3/h[i+1]*(a[i+2] - a[i+1]))- (3/h[i]*(a[i+1] - a[i])) for i in range(n-1)] + [0])
show(bv)<jupyter_output><empty_output><jupyter_text>Erweitere Koeffizientenmatrix vom System $A \cdot c = \bar{b}$<jupyter_code>_Ab = matrix([A.T[i] for i in range(A.ncols())] + [bv]).T
show(_Ab.echelon_form())<jupyter_output><empty_output><jupyter_text>Oder die Inverse um direkt $c = A^{-1} \cdot \bar{b}$ zu berechnen<jupyter_code>show(A^-1)<jupyter_output><empty_output><jupyter_text>Lösung von $A c = \bar{b}$ ergibt $c = \{c_0, c_1, \ldots, c_n\}$<jupyter_code>c = (A^-1 * bv)
show(c)
c = list(c)<jupyter_output><empty_output><jupyter_text>$$b_i = \frac{1}{h_i}(a_{i+1} - a_i) - \frac{h_i}{3}(2 c_i + c_{i+1})$$<jupyter_code>b = [1/h[i] * (a[i+1]-a[i]) - h[i]/3*(2*c[i]+c[i+1]) for i in range(n)]
show(b)<jupyter_output><empty_output><jupyter_text>$$d_i = \frac{c_{i+1} - c_i}{3 h_i}$$<jupyter_code>d = [(c[i+1]-c[i])/(3*h[i]) for i in range(n)]
show(d)
show(LatexExpr('a_i = '), a)
show(LatexExpr('b_i = '), b)
show(LatexExpr('c_i = '), c)
show(LatexExpr('d_i = '), d)
show(LatexExpr('h_i = '), h)<jupyter_output><empty_output><jupyter_text>Die Teilststücke von $S(x)$ sind dann:$$S_i(x) = a_i + b_i(x - x+i) + c_i (x - x_i)^2 + d_i (x - x_i)^3$$<jupyter_code># all the splines
var('x')
S = [a[i] + (b[i] * (x - points[i][0])) + (c[i] * (x - points[i][0])^2) + (d[i] * (x - points[i][0])^3)
     for i in range(n)]
for i, s in enumerate(S):
    show(LatexExpr("S_{} =".format(i)), s.simplify_full())
# plot the points and the splines for their interval
scatter_plot(points) + \
sum(plot(s, xmin=points[i][0], xmax=points[i+1][0]) for i, s in enumerate(S))
# Sanity checking:
# no output means, that the conditions of cubic splines match. This should always be the case if there are no bugs.
for i in range(len(S)-1):
    if S[i].subs(x=points[i+1][0]) != S[i+1].subs(x=points[i+1][0]):
        print "Spline {} and {} don't match at x = {}".format(i, i+1, points[i+1][0])
    if S[i].derivative().subs(x=points[i+1][0]) != S[i+1].derivative().subs(x=points[i+1][0]):
        print "Derivative of spline {} and {} don't match at x = {}".format(i, i+1, points[i+1][0])
    if S[i].derivative().derivative().subs(x=points[i+1][0]) != S[i+1].derivative().derivative().subs(x=points[i+1][0]):
        print "Second derivative of spline {} and {} don't match at x = {}".format(i, i+1, points[i+1][0])

if S[0].derivative().derivative().subs(x=points[0][0]) != 0:
    print "Second derivative of first spline at first point must be 0"
    
if S[-1].derivative().derivative().subs(x=points[-1][0]) != 0:
    print "Second derivative of last spline at last point must be 0"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.23008611410118407, 'fraction_numerical': 0.02691065662002153, 'mean_word_length': 5.278716216216216, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '619018', 'license': 'Unlicense', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1713, 'n_words': 334, 'path': 'nrla_cubic_splines.ipynb', 'repo_name': 'f0rki/nrla_nbs', 'size': '3716'}