# Thread: How does one use Cubic Spline Interpolation?

1. ## How does one use Cubic Spline Interpolation?

Hello all, I would like to know how cubic spline interpolation is used to create a smooth curve given a set of data points. This is the method used by a lot of software when the user wants to generate a function from a set of data points. I know the method involves the value of the second derivative being equated to 0 between points but not much else, please can someone explain?

2. Say you have 20 points, $x_0$, $x_1$, to $x_{19}$. And you know the value of a function f only in these points. Cubic splines are used to approximate the function f (from which you don't know the formula, or its value everywhere) between each intervals $[x_0,x_1]$, $[x_1,x_2]$, etc. The approximation is made by cubic polynomials on each interval.
You said :
I know the method involves the value of the second derivative being equated to 0 between points
. This is not always true, but when it happens, we call it a natural cubic spline. It is made because if we don't use this condition, then we cannot find the cubic spline (because it has too much unknowns. In order to determine it, we add 2 conditions : its second derivative is equals to 0 at $x_0$ and at $x_n$, or in our case $x_{19}$.)
The cubic spline must satisfy some other conditions, for example it must be continuous and its order must equals to 3 (in our case).
Maybe I can look if I can give you an example (just ask me if you would like to).

3. ## Thanks

Thanks for that, it sounds very interesting! Now that I know the difference between cubic splines and natural cubic splines, I will try to learn how to use cubic splines first.

Maybe I can look if I can give you an example (just ask me if you would like to).
Yes please that'd be fantastic! Can you please explain how to perform the operation mathematically.

4. Ok, I will give you more or less the idea.

Let S be our cubic spline, $x_0=1$, $x_1=2$, $x_2=5$.
$S(x)$ can be written as $S(x)=a_0x^3+b_0x^2+c_0x+d_0$ for all $x$ in $[1,2]$,
$a_1x^3+b_1x^2+c_1x+d_1$ for all $x$ in $[2,5]$.
We have the information that it is continuous, so looking at the nodes (the $x_i$s), we can write $a_0*2^3+b_0*2^2+2c_0+d_0=a_1*2^3+b_1*2^2+2c_1+d_1$
We want to determine $a_0$, $b_0$, $c_0$, $d_0$ and $a_1$, $b_1$, $c_1$, $d_1$. Clearly we do not have enough information (conditions) to determine them. That's why we add the condition that $S''(x_0)=0$ and $S''(x_5)=0$.
So deriving twice S(x), we obtain that $S''(x)=6a_0x+2b_0$ for all x in [1,2] and $S''(x)=6a_1x+2b_1$ for all x in [2,5]. I forgot to mention that the derivative of S and the second derivative of S must be continuous too, therefore we have another condition : $6a_0(2)+2b_0=6a_1(2)+2b_1$. Also, we add the condition that's a natural cubic spline, therefore we have $S''(x)=0 \Leftrightarrow 6a_1x_0+2b_1=0$ and $6a_0x_2+2b_0=0$, from which you can replace $x_0$ and $x_2$ by their known values. Calculating $S'(x)$ and making it equals at the nodes, you obtain 2 more conditions. And that's enough to solve the problem. The problem is now to solve an 8x8 matrix. But generally you can find a nice trick and solve it fast.

5. ## Great stuff

This is very interesting maths, I understand what you've done so far but when you said...

The problem is now to solve an 8x8 matrix. But generally you can find a nice trick and solve it fast.
... what did you mean? Are matrices the next part of finding $S(x)$?

6. I meant the coefficient $a_0$, $a_1$, $b_0$, etc. All the systems of equations we have collected till here can make an 8x8 matrix. You are not obligated to use matrices, but you need to solve a big system of equations. There are 8 unknowns, so it's not that easy, but possible (since we have 8 conditions in total). Maybe you could try to solve the coefficients in my example, and show your work...

7. ## Hmm...

I'm still doing Advanced Subsidary mathematics so haven't even touched matrices yet, maybe I should teach myself matrices... is it complex?

8. I'm still doing Advanced Subsidary mathematics so haven't even touched matrices yet, maybe I should teach myself matrices... is it complex?
All depends on what you are looking for. In our example, we just need to reduce an $8$x $8$ matrix, or equivalently, solve a system of 8 equations involving 8 unknowns. The "matrix theory" needed for that is very basic (the 3 basics operations to get the reduced matrix). I suggest you not to study matrices only for this purpose. Don't forget that you still can find $a_0$ and the $7$ other unknowns without passing by matrices. Having found the 8 unknowns, you found the natural cubic spline that pass through the 3 given points.