# Thread: Numerical Analysis- Cubic Spline?

1. ## Numerical Analysis- Cubic Spline?

In my numerical analysis class i was sick during the class they did the Cubic Spline and I'm getting confused.

Question: Determine the FREE cubic spline S that interpolates the data f(0)=0, f(1)=1 and f(2)=2.

So far from following the examples in the book i have:

S(x)={S_0(x)=a_0+b_o(x-0)+c_0(x-0)^2+d_0(x-0)^3 0<=x<=1
{S_1(x)=a_1+b_1(x-1)+c_1(x-1)^2+d_1(x-1)^3 1<=x<=2

Like i said I was following an example in the book and its not very clear so I'm not even sure how i got that far. I know i have to form a matrix but I have no clue how...the answer in the back of the books says : S(x)=x on [0,2]

If someone can please help maybe to explain the steps and then I could follow suit. And i'm pretty sure it belongs in this category if not please redirect me. Thanks so much.

2. A Free Cubic Spline is also called a Natural Cubic Spline.
First, let's define a set of data with x-coordinates $\displaystyle x_0,\dots, x_n$ and corresponding y-values [tex]y_0,\dots,y_n[tex]. To determine cubic splines we must have a closed system. To close the system we could choose various conditions. For a natural spline we choose the boundary conditions $\displaystyle S''(x_0) = S''(x_n) = 0$. For your example this implies $\displaystyle S''(0) = S''(2) = 0$. Let's define the cubic spline on the i-th interval $\displaystyle [x_i,x_{i+1}]$by
$\displaystyle S_i(x) = \dfrac{z_i}{6h_i}(x_{i+1} - x)^3 + \dfrac{z_{i+1}}{6h_i}(x - x_i)^3 + C(x - x_i) + D(x_{i+1} - x)$.
Then we can define a system of equations
$\displaystyle \begin{bmatrix}u_1 & h_1 \\ h_1 & u_2 & h_2 \\ & h_2 & u_3 & h_3 \\ & & \ddots & \ddots & \ddots \\ & & & h_{n-3} & u_{n-2} & h_{n-2} \\ & & & & h_{n-2} & u_{n-1}\end{bmatrix} \begin{bmatrix} z_1 \\ z_2 \\ z_3 \\ \vdots \\ z_{n-2} \\ z_{n-1}\end{bmatrix} = \begin{bmatrix} v_1 \\ v_2 \\ v_3 \\ \vdots \\ v_{n-2} \\ v_{n-1}\end{bmatrix}$
where
$\displaystyle \begin{matrix} h_i=x_{i+1} - x_i \\ u_i = 2(h_i + h_{i-1}) \\ b_i = \dfrac{6}{h_i}(y_{i+1} - y_i) \\ v_i = b_i - b_{i-1}\end{matrix}$.
Additionally, since this is a Natural Spline we see that $\displaystyle z_0 = z_n = 0$ and $\displaystyle C = \dfrac{y_{i+1}}{h_i} - \dfrac{z_{i+1}h_i}{6},\; D = \dfrac{y_i}{h_i} - \dfrac{z_ih_i}{6}$.
So in the end finding the cubic spline involves solving the system that I posted. All you have to do is plug things in.

I would say this belongs in the Advanced Applied Mathematics section.

3. "All you have to do is plug things in." <- would it be a stupid question to ask where x_i and x_i+1 are taken from? I know you said the interval but in my example there was no interval unless its from [0,2]. your explanation is a little more indepth than my book, however following my book, I got confused as to what my x_i and stuff if. Otherwise I understand the actual process.

4. Based on your data there is a partition of the interval $\displaystyle [x_0=0,x_2=2]$ into subintervals $\displaystyle [x_0=0,x_1=1]$ and $\displaystyle [x_1=1,x_2=2]$. The corresponding y-values are $\displaystyle y_0 = 0, y_1 = 1, y_2 = 2$.

EDIT: Splines are defined on intervals. In your case there will be a spline function $\displaystyle S_0(x)$ defined on the interval $\displaystyle [0,1]$ and another spline function $\displaystyle S_1(x)$ defined on the interval $\displaystyle [1,2]$.

5. Wait, in my example i have S_0(x) and S_1(x)..am I doing this twice because i have f(0)=0 f(1)=1 and f(2)=2. so my interval would go from [0,2] but I have to split it up from [0,1] and [1,2]? Sorry i'm just trying to figure things out.

6. Originally Posted by lvleph
Based on your data there is a partition of the interval $\displaystyle [x_0=0,x_2=2]$ into subintervals $\displaystyle [x_0=0,x_1=1]$ and $\displaystyle [x_1=1,x_2=2]$. The corresponding y-values are $\displaystyle y_0 = 0, y_1 = 1, y_2 = 2$.

EDIT: Splines are defined on intervals. In your case there will be a spline function $\displaystyle S_0(x)$ defined on the interval $\displaystyle [0,1]$ and another spline function $\displaystyle S_1(x)$ defined on the interval $\displaystyle [1,2]$.
Haha sorry I should have waited...Thanks a bunch, at least i figured that part out

7. Yeah, I realized it might not have been clear, so I added.

8. sorry, but I've been trying to work on matrix A, and the book shows it a little different they have $\displaystyle [1 0 0................0 h_0 2(h_0+h_1) h1 0...........0]$

etc. However my matrix was coming out like this
$\displaystyle [1 0 0 0 0 0 0 0 1 4 1 0 0 0 0 0 0 1 4 1 0 0 0 0 0 0 0 0 1 4 ...]$

however, the answer says this should be the matrix:
$\displaystyle [1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 2 3 0 -1 0 0 0 0 2 6 0 0 -2 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 2 6]$

I don't know why mine isn't coming out the same as the other one. What am i doing wrong? Is it possible to input the numbers in eg: (h_0= 1-0) <-if i even did that right.

9. Originally Posted by tn11631
sorry, but I've been trying to work on matrix A, and the book shows it a little different they have $\displaystyle [1 0 0................0 h_0 2(h_0+h_1) h1 0...........0]$

etc. However my matrix was coming out like this
$\displaystyle [1 0 0 0 0 0 0 0 1 4 1 0 0 0 0 0 0 1 4 1 0 0 0 0 0 0 0 0 1 4 ...]$

however, the answer says this should be the matrix:
$\displaystyle [1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 2 3 0 -1 0 0 0 0 2 6 0 0 -2 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 2 6]$

I don't know why mine isn't coming out the same as the other one. What am i doing wrong? Is it possible to input the numbers in eg: (h_0= 1-0) <-if i even did that right.
sorry those should be matricies of 8x8, I'm not good with the whole adding in math symbols

10. Your system should be a 2x2 system.
$\displaystyle \begin{bmatrix}u_1 & h_1 \\ h_1 & u_2 \end{bmatrix} \begin{bmatrix} z_1 \\ z_2 \end{bmatrix} = \begin{bmatrix} v_1 \\ v_2 \end{bmatrix}$
and $\displaystyle z_0 = z_3 = 0$.
I have to go teach, so it will be a while if you have any other questions.

EDIT: If you double click my equations you can see the commands for them.

11. Originally Posted by lvleph
Your system should be a 2x2 system.
$\displaystyle \begin{bmatrix}u_1 & h_1 \\ h_1 & u_2 \end{bmatrix} \begin{bmatrix} z_1 \\ z_2 \end{bmatrix} = \begin{bmatrix} v_1 \\ v_2 \end{bmatrix}$
and $\displaystyle z_0 = z_3 = 0$.
I have to go teach, so it will be a while if you have any other questions.