|
|
| Line 1: |
Line 1: |
| '''Cubic spline interpolation''' is the process of finding a '''continuous and differentiable up to second degree''' function $C(x)$ consisting of a series of cubic functions $C_i(x)$ that for a definite set of datapoints $\{(x_i,y_i)| i\in [0,n], i \in \mathbb{N}\}$ satisfies $C(x_i)=y_i$.
| | PLACEHOLDER |
| | |
| i.e., find
| |
| | |
| $$
| |
| C(x)=C_i(x),\, x_i \le x \le x_{i+1} \\
| |
| C_i(x)=a_i(x-x_i)^3+b_i(x-x_i)^2+c_i(x-x_i)+d_i,\, \forall i\in [0,n-1], i \in \mathbb{N}\
| |
| $$
| |
| | |
| so
| |
| | |
| $$
| |
| C(x_i)=y_i,\, \forall \{(x_i,y_i)| i\in [0,n], i \in \mathbb{N}\} \tag{1} \\
| |
| C_i(x_{i+1})=C_{i+1}(x_{i+1})=y_{x+1},\, \forall i \in [0,n-1], i \in \mathbb{N} \tag{2} \\
| |
| \dot C_i(x_{i+1})=\dot C_{i+1}(x_{i+1}),\, \forall i \in [0,n-2], i \in \mathbb{N} \tag{3} \\
| |
| \ddot C_i(x_{i+1})=\ddot C_{i+1}(x_{i+1}),\, \forall i \in [0,n-2], i \in \mathbb{N} \tag{4} \\
| |
| $$
| |
| | |
| == Solution ==
| |
| | |
| $$
| |
| \begin{aligned} g_{i}(x) &=a_{i}\left(x-x_{i}\right)^{3}+b_{i}\left(x-x_{i}\right)^{2}+c_{i}\left(x-x_{i}\right)+d_{i} \tag{5} \\
| |
| g_{i}^{\prime}(x) &=3 a_{i}\left(x-x_{i}\right)^{2}+2 b_{i}\left(x-x_{i}\right)+c_{i} \tag{6}\\ g_{i}^{\prime \prime}(x) &=6 a_{i}\left(x-x_{i}\right)+2 b_{i} \tag{7} \end{aligned}
| |
| $$
| |
| | |
| From 1 and 5, $d_i=y_i,\, \forall i \in [0,n-1], i \in \mathbb{N}$,
| |
| | |
| Let $h_i=x_{i+1}-x_i$,\, f_i=y_{i+1}-y_i$,
| |
| | |
| then from 2 and 5, $a_{i} h_{i}^{3}+b_{i} h_{i}^{2}+c_{i} h_{i}=f_{i}\forall i \in [0,n-1], i \in \mathbb{N} \tag{8, n equations}$;
| |
| | |
| from 3 and 6, $3 a_{i} h_{i}^{2}+2 b_{i} h_{i}+c_{i}=c_{i+1}, ,\, \forall i \in [0,n-2], i \in \mathbb{N} \tag{9, n-1 equations}$;
| |
| | |
| from 4 and 7, $3 a_{i} h_{i}+b_{i}=b_{i+1} ,\, \forall i \in [0,n-2], i \in \mathbb{N} \tag{10, n-1 equations}$.
| |
| | |
| Notice there are $3n$ remaining degrees of freedom: $a_i,b_i,c_i\, \forall i \in [0,n-1], i \in \mathbb{N}$.
| |
| | |
| To reduce the degrees of freedom, extend 10 up to $i=n-1$:
| |
| | |
| $$
| |
| 3 a_{n-1} h_{n-1}+b_{n-1}=b_{n}
| |
| $$
| |
| | |
| From 10,
| |
| | |
| $$
| |
| a_{i}=\frac{1}{3 h_{i}}\left(b_{i+1}-b_{i}\right), ,\, \forall i \in [0,n-1], i \in \mathbb{N} \tag{11, n equations}
| |
| $$
| |
| | |
| Then use 11 to substitute for $a_i$ in 8,
| |
| | |
| $$
| |
| \begin{aligned} c_{i} &=\frac{1}{h_{i}}\left(f_{i}-a_{i} h_{i}^{3}-b_{i} h_{i}^{2}\right) \\ &=\frac{1}{h_{i}}\left(f_{i}-\frac{1}{3 h_{i}}\left(b_{i+1}-b_{i}\right) h_{i}^{3}-b_{i} h_{i}^{2}\right) \\ &=\frac{f_{i}}{h_{i}}-\frac{1}{3} h_{i}\left(b_{i+1}+2 b_{i}\right), \quad i=0 \text { to } n-1 . \end{aligned} \tag{12, n equations}
| |
| $$
| |
| | |
| Now use 11,12 to substitute for $a_i$, $c_i$ in 9:
| |
| | |
| $$
| |
| \begin{gathered} 3\left(\frac{1}{3 h_{i}}\left(b_{i+1}-b_{i}\right)\right) h_{i}^{2}+2 b_{i} h_{i}+\left(\frac{f_{i}}{h_{i}}-\frac{1}{3} h_{i}\left(b_{i+1}+2 b_{i}\right)\right) \\ =\left(\frac{f_{i+1}}{h_{i+1}}-\frac{1}{3} h_{i+1}\left(b_{i+2}+2 b_{i+1}\right)\right) \end{gathered}
| |
| $$
| |
| | |
| On simplification,
| |
| | |
| $$
| |
| \frac{1}{3} h_{i} b_{i}+\frac{2}{3}\left(h_{i}+h_{i+1}\right) b_{i+1}+\frac{1}{3} h_{i+1} b_{i+2}=\frac{f_{i+1}}{h_{i+1}}-\frac{f_{i}}{h_{i}} ,\, \forall i \in [0,n-2], i \in \mathbb{N} \tag{13, n-1 equations}
| |
| $$
| |
| | |
| At this point, $n-1$ equations and $n+1$ degrees of freedom, $b_i,\, i \in [0,n+1], i \in \mathbb{N}$, remain. Therefore, two additional conditions are required for one particular solution.
| |
| | |
| A commonly accepted set of 2 additional conditions, known as the '''not-a-knot''' conditions, are:
| |
| | |
| $$
| |
| g_{0}^{\prime}\left(x_{0}\right)=\alpha, \quad g_{n-1}^{\prime}\left(x_{n}\right)=\beta . \nonumber
| |
| $$
| |
| | |
| Which requires $a_0=a_1$, $a_{n-2}=a_{n-1}$,
| |
| | |
| From 11,
| |
| | |
| $$
| |
| \begin{cases}
| |
| \frac{1}{3 h_{0}}\left(b_{1}-b_{0}\right)=\frac{1}{3 h_{1}}\left(b_{2}-b_{1}\right) \\
| |
| \frac{1}{3 h_{n-2}}\left(b_{n-1}-b_{n-2}\right)=\frac{1}{3 h_{n-1}}\left(b_{n}-b_{n-1}\right).
| |
| \end{cases}
| |
| $$
| |
| | |
| On simplification,
| |
| | |
| $$
| |
| \begin{cases}
| |
| h_{1} b_{0}-\left(h_{0}+h_{1}\right) b_{1}+h_{0} b_{2}=0 \\
| |
| h_{n-1} b_{n-2}-\left(h_{n-2}+h_{n-1}\right) b_{n-1}+h_{n-2} b_{n}=0.
| |
| \end{cases} \tag{14, 2 equations}
| |
| $$
| |
| | |
| Rewrite everything into matrices:
| |
| | |
| $$
| |
| \left(\begin{array}{cccccccc} h_1 & -(h_0+h_1) & h_0 & 0 & \cdots & 0 & 0 \\ \frac{1}{3} h_{0} & \frac{2}{3}\left(h_{0}+h_{1}\right) & \frac{1}{3} h_{1} & 0 & \cdots & 0 & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots \\ 0 & 0 & 0 & \cdots & \frac{1}{3} h_{n-2} & \frac{2}{3}\left(h_{n-2}+h_{n-1}\right) & \frac{1}{3} h_{n-1} \\ 0 & 0 & 0 & \cdots & h_{n-1} & -(h_{n-2}+h{n-1}) & h_{n-2}\end{array}\right)\left(\begin{array}{c} b_{0} \\ b_{1} \\ \vdots \\ b_{n-1} \\ b_{n} \end{array}\right)\left(\begin{array}{c} 0 \\ \frac{f_{1}}{h_{1}}-\frac{f_{0}}{h_{0}} \\ \vdots \\ \frac{f_{n-1}}{h_{n-1}}-\frac{f_{n-2}}{h_{n-2}} \\ 0 \end{array}\right)
| |
| $$
| |
| | |
| Finally, solve the matrix numerically (e.g., with python) to obtain the interpolating curve.
| |
| | |
| <!-- add general algorithm python code, defined as a function -->
| |
| | |
| {{Hidden example}}
| |
| <!-- add real-world example, code (reusing functions defined above) and final interpolated graph -->
| |
| {{End hidden}}
| |