# How does one use Cubic Spline Interpolation?

• May 23rd 2008, 09:50 AM
AAKhan07
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?
• May 23rd 2008, 10:02 AM
arbolis
Say you have 20 points, \$\displaystyle x_0\$, \$\displaystyle x_1\$, to \$\displaystyle 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 \$\displaystyle [x_0,x_1]\$, \$\displaystyle [x_1,x_2]\$, etc. The approximation is made by cubic polynomials on each interval.
You said :
Quote:

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 \$\displaystyle x_0\$ and at \$\displaystyle x_n\$, or in our case \$\displaystyle 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).
• May 23rd 2008, 10:42 AM
AAKhan07
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.

Quote:

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.
• May 23rd 2008, 11:26 AM
arbolis
Ok, I will give you more or less the idea.

Let S be our cubic spline, \$\displaystyle x_0=1\$, \$\displaystyle x_1=2\$, \$\displaystyle x_2=5\$.
\$\displaystyle S(x)\$ can be written as \$\displaystyle S(x)=a_0x^3+b_0x^2+c_0x+d_0\$ for all \$\displaystyle x\$ in \$\displaystyle [1,2]\$,
\$\displaystyle a_1x^3+b_1x^2+c_1x+d_1\$ for all \$\displaystyle x\$ in \$\displaystyle [2,5]\$.
We have the information that it is continuous, so looking at the nodes (the \$\displaystyle x_i\$s), we can write \$\displaystyle 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 \$\displaystyle a_0\$, \$\displaystyle b_0\$, \$\displaystyle c_0\$, \$\displaystyle d_0\$ and \$\displaystyle a_1\$, \$\displaystyle b_1\$, \$\displaystyle c_1\$, \$\displaystyle d_1\$. Clearly we do not have enough information (conditions) to determine them. That's why we add the condition that \$\displaystyle S''(x_0)=0\$ and \$\displaystyle S''(x_5)=0\$.
So deriving twice S(x), we obtain that \$\displaystyle S''(x)=6a_0x+2b_0\$ for all x in [1,2] and \$\displaystyle 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 : \$\displaystyle 6a_0(2)+2b_0=6a_1(2)+2b_1\$. Also, we add the condition that's a natural cubic spline, therefore we have \$\displaystyle S''(x)=0 \Leftrightarrow 6a_1x_0+2b_1=0\$ and \$\displaystyle 6a_0x_2+2b_0=0\$, from which you can replace \$\displaystyle x_0\$ and \$\displaystyle x_2\$ by their known values. Calculating \$\displaystyle 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.
• May 23rd 2008, 11:41 AM
AAKhan07
Great stuff
This is very interesting maths, I understand what you've done so far but when you said...

Quote:

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 \$\displaystyle S(x)\$?
• May 23rd 2008, 11:47 AM
arbolis
I meant the coefficient \$\displaystyle a_0\$, \$\displaystyle a_1\$, \$\displaystyle 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...
• May 23rd 2008, 11:55 AM
AAKhan07
Hmm...
I'm still doing Advanced Subsidary mathematics so haven't even touched matrices yet, maybe I should teach myself matrices... is it complex?
• May 23rd 2008, 01:42 PM
arbolis
Quote:

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 \$\displaystyle 8\$x\$\displaystyle 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 \$\displaystyle a_0\$ and the \$\displaystyle 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.