Cubic Spline Interpolation Tutorial

• hotvette
In summary, the conversation includes a request for two tutorials on cubic splines, one for the classic tri-diagonal formulation and one for parametric splines. The tutorials include example problems and a discussion on fitting a circle with cubic polynomials. The conversation also includes a question on calculating the coefficients for a spline and a discussion on a potential error in one of the examples. Finally, there is a request for the interpolation error for a cubic spline.
hotvette
Homework Helper
Attached below are two cubic spline tutorials:

1. Explanation of the classic tri-diagonal cubic spline formulation. Included are 2 example problems.

2. Extension to parametric cubic splines. Included are 2 example problems
.

Attachments

• Cubic Spline Tutorial.pdf
65.3 KB · Views: 8,416
• Parametric Spline Tutorialv2.pdf
102.8 KB · Views: 5,595
Last edited by a moderator:
wow,

After a very long search in google i found this tutorial.

And it was the first one, which describes parametric cubic splines in a good way.

Thank you very much :-)

Now i can lay streets through my landscape :-)

Sigh...Thank You!

I was missing something very stupid and now I know what it is!

Thank you for tutorials. This has helped me very mush.

Cubic Spline Interpolation of a Circle

Hey there -
Thanks for the great tutorials - they really helped me! I'm trying to duplicate your results for cubic interpolation of a circle with 4 points and I got the same solution for the 2nd derivatives in the x and y directions. However, when I solve for the coefficients and plot the cubic polynomials I can't seem to get the same result as you - Heres the code that calculates the coefficients (in MATLAB)

mx - x''
my - y''
Sx - x coefficients
Sy - y coefficients
s - arc length [0 0.25 0.5 0.75 1]

for i = 1:n-1
Sx(i,1) = (mx(i+1) - mx(i))/6*h;
Sx(i,2) = mx(i)/2;
Sx(i,3) = (x(i+1) - x(i))/h - h*(mx(i+1) + 2*mx(i))/6;
Sx(i,4) = x(i);

Sy(i,1) = (my(i+1) - my(i))/6*h;
Sy(i,2) = my(i)/2;
Sy(i,3) = (y(i+1) - y(i))/h - h*(my(i+1) + 2*my(i))/6;
Sy(i,4) = y(i);

end

%Plotting the function
ds = 0.01;
m=1;
i=1;
for i = 1:n-1
for k = s(i):ds:s(i+1)
genpnx(m) = Sx(i,1)*(k - s(i))^3 + Sx(i,2)*(k-s(i))^2 + Sx(i,3)*(k-s(i)) + Sx(i,4);
genpny(m) = Sy(i,1)*(k - s(i))^3 + Sy(i,2)*(k-s(i))^2 + Sy(i,3)*(k-s(i)) + Sy(i,4);
m = m+1;
end
i
end

plot(genpnx,genpny,'r')

Do you have any idea what I might be doing wrong? (Ive attached an image of what the spline looks like for 4 points)

Thanks!

Attachments

• spline.jpg
12.8 KB · Views: 1,308
Last edited:
Though I'm not a MATLAB person, your expressions for calculating the coefficients look correct. Make sure you are using 0.25 for h. Also, for plotting, t should vary from 0 to 0.25 for each segment.

Here are the coefficient values I got for the 1st segment:

ax = 32
bx = -24
cx = 0
dx = 1

ay = -32
by = 0
cy = 6
dy = 0

Actually, this isn't the best way to fit a circle with cubic polynomials. It is better to fit a quarter circle with a single parametric cubic. The tutorial has been updated - check it out.

Last edited:
Solution

Thanks for the coefficients - they helped me figure out what the problem was - a syntax problem - the correct expression for the coefficients are

Sx(i,1) = (mx(i+1) - mx(i))/(6*h)

Sx(i,1) = (mx(i+1) - mx(i))/6*h

Thanks a ton!

Glad to help. Fyi, the problem can be made even simpler by making h = 1, meaning s = [0 1 2 3 4]. Second deriveratives are x" = [-3 0 3 -3], y" = [0 -3 0 3 0] and plotting can be done for t varying from 0 to 1 for each segment.

I encourage you to review the updated tutorial on circle fitting.

Attached is an update to the Cubic Spline Tutorial.

To Staff,

Would someone be willing to replace the 1st attachment in this thread with the below? Thanks.

Last edited:
thanks a lot buddy.

Hi hotvette,
I have been looking for a good tutorial on Cubic Splines, nice work. I have found what I think might be an error in one of your examples, however. Example Problem #1 shows c2 = 0, but when I plug in the values:
y3 = 1
y2 = 0.125
h2 = .5
y3" = 0
y2" = 4.5
I come up with c2 = 1, not 0. Plugging 0.5 into the second polynomial with c2 = 1, I get y2 = 1, thus resulting in a discontinuity with the first polynomial. I'm too lazy to try to work out the root of the problem, but I figured I'd flag the discrepancy.
Cheers

Agree that C2 = 1.0 (typo), but I think the evaluation of the 2nd poly is OK:

$$y = a2(x - x_2)^3 + b2(x - x_2)^2 + c2(x - x_2) + d2$$

Update with typo fixed is attached.

Attachments

• Cubic Spline Tutorial v3.pdf
109.2 KB · Views: 2,321
Yep, my mistake. I used ax^3 + bx^2 + cx + d rather than using (x-x0), etc.
Thanks again for the excellent tutorial.

For the cubic spline interpolation if we have
A=(x2-x)/h; B=(x-x1/h); C=1/6*(A^3-A)*h^2; D=1/6*(B^3-B)*h^2;
y=A y1 + B y2 + C y1'' + D y2''

What is the interpolation error of above?

For example for Hermitian cubic spline it is smaller than 5/384 ||f^{(4)}|| h^4

Thanks very much my friend! The tutorial for the parametric C Splines was very helpful to me!
You see, every book I ve read doesn t say how we can relate the derivatives of the actual curve to the derivatives of the parametric equations of x & y. Ok, its easy to proof with the chain rule but what about the values of these derivatives on bounds! Simply, x'(t)/y'(t) must be constant on bounds, so give x'(t),y'(t) any value you want to keep this ratio constant! HAHAHAH, i feel stupid...
Thanks again!

Thanks! Just what i was looking for! :)

Any reading suggestion to smooth out a curve using spline or similar approach.

great tuto, very precise. Thanks a lot

Thanks

I have trouble implementing the interpolation. not sure what is wrong. appreciate if someone can point out which part I'm wrong. I am pretty sure I have the coefficients correct but somehow I am using the spline equations incorrectly (my guess).
So here are my original x and y
x y
0.90 1.30
1.30 1.50
1.90 1.85
2.10 2.10
3.00 1.95
3.80 0.40
4.30 0.25

Here are my coefficients
a b c d
Equation 1 -0.282 0.000 0.545 1.30
Equation 2 1.046 -0.338 0.410 1.50
Equation 3 -4.815 1.545 1.134 1.85
Equation 4 -0.162 -1.344 1.174 2.10
Equation 5 1.757 -1.780 -1.638 1.95
Equation 6 -1.625 2.437 -1.112 0.40

Now If I want to interpolate x=1, my formula would read
y=-0.282*(1-0.9)^3 + 0.000*(1-0.9)^2 +0.545*(1-0.9) + 1.30
y=1.354230991

I think the answer is wrong because I validated it against the utility at http://www.akiti.ca/CubicSpline.html.. Would appreciate if someone can tell me if my coefficients are correct and if i am using the formulas correctly.

Thanks

Using natural boundary conditions (f" = 0 at either end) I derived the same result as you. The utility doesn't state what algorithm is being used nor what assumptions are being made on boundary conditions. The routines mentioned in the credits refer to Hermite interpolation. If that's what is being used by the utility, then it's a different algorithm than what I posted. From what I read, Hermite interpolation isn't C2 compatible at spline boundaries whereas classic cubic spline interpolation is. Bottom line is that there are many interpolation methods. There is no right one.

The other possibility is that the utility is performing cubic spline interpolation but is making some assumption about the end boundary conditions. Just for kicks I was able to replicate the result of the utility by adjusting the slope at the first point to 0.811 and the slope of the last point to 2.226. I checked several points in between and they all matched.

Black box utilities are great because they can be easy to use, but a caveat is that you may not know what they are doing.

Last edited:
I believe I've unraveled the mystery of the black box utility. It appears to use cubic splines with end boundary conditions called 'not-a-knot', which is continuous 3rd derivatives in the end pairs of splines. See page 5 in the following link:

http://www.maths.lth.se/na/courses/FMN081/FMN081-06/lecture11.pdf

This means the cubic term (a in the tutorial) is the same in the end pairs of splines. If we equate the cubic terms of the 1st two splines, the resulting boundary condition after some rearranging is:

-h2y1'' + (h1+h2)y2'' - h1y3'' = 0

There is a similar condition at the other end. If these are used as the end boundary conditions, the resulting splines produce the same results as the utility. Fyi, according to the link, MATLAB uses this boundary condition.

An interesting way to confirm would be to obtain 4 interpolated points from the utility (2 end points are given so you'd just need 2 intermediate points), from which you could easily derive the coefficients of the cubic polynomial and compare to the ones calculated using the algorithm in the cubic spline tutorial (and not-a-knot boundary conditions). They should match.

Last edited:
Hi all,

I have a stupid question regarding calculating the derivative of cubic spline. I'm currently looking at the code here http://www.ee.ucl.ac.uk/~mflanaga/java/CubicSpline.java at method calcDeriv(), and I do not understand what are these 2 lines are for
sig=(this.x-this.x[i-1])/(this.x[i+1]-this.x[i-1]);
p=sig*this.d2ydx2[i-1]+2.0;

Any help would be appreciated.

Cheers,
Wins

Hi All,
I am trying to convert a file that has three arrays of 528 samples into three arrays of 301.
The first is log spaced frequency points the second is the impedance values at each frequency and the last is phase values. I have the routines spline and splint. My problem is the the second to last sample [299] is off scale. The last sample [300] is correct. I know it is some kind of boundary problem, but it is beyond me to figure it out.

fr,re,ph are arrays[1..528] of float

Spline(fr,re,528,re[1],re[528],y2);
Spline(fr,ph,528,ph[1],ph[528],y4);
fm:=Power((20000.0/10.0),1.0/300);
For i:=1 to 301 do
begin
tempARRF:=10.0*Power(fm,i-1);
end;
For i:=1 to 301 do
begin
splint(fr,re,y2,528,tempARRF,tempARRR);
splint(fr,ph,y4,528,tempARRF,tempARRP);
end;
CloseFile(impFile);
For i:=0 to 300 do
begin
impARR.Freq:=tempARRF[i+1];
impARR.Res:=tempARRR[i+1];
impARR.Phs:=tempARRP[i+1];
end;

Thanks a lot

Thanks. It really help me a lot.

you have no idea how this tutorial helped me
it is one of the bests that i found

I only wish it had more exemples

ty anyway

Worst thing about this topic is that you get different formulae each time from different tutorials and books. I was freaked out of this diversity before exam and almost screwd a 15 mark question but hope it was all ok

By using spherical coordinates how can we get the volume of a right circular cylinder with radius a and height h

Thnks it really helped me a lot on my project that I'm doing... keep up the noble work...lol

I was just browsing for related blog posts for my project research and I happened to

discover yours. Thanks for the excellent information!
---------------------
Watch TV Online

Hello,
Thank you for this excellent tutorial!

I've now implemented cubic splines in C++ and shared the result on my website should anyone want a reference C++ implementation...

http://www.marcusbannerman.co.uk/index.php/home/latestarticles/42-articles/96-cubic-spline-class.html"

Marcus

Last edited by a moderator:
toastedcrumpets said:
Hello,
Thank you for this excellent tutorial!

I've now implemented cubic splines in C++ and shared the result on my website should anyone want a reference C++ implementation...

http://www.marcusbannerman.co.uk/index.php/home/latestarticles/42-articles/96-cubic-spline-class.html"

Marcus

Thanks for the acknowledgment on your website!

Last edited by a moderator:
Excellent tutorial.

Somebody knows the formula to calulate the 95%CI for the spline?

Hi,
I want the verilog or VHDL coding for performing cubic spline interpolation used in biomedical EMD processor

<h2>1. What is cubic spline interpolation?</h2><p>Cubic spline interpolation is a mathematical method used to estimate values between data points. It involves fitting a series of cubic polynomials to the data points in order to create a smooth curve that passes through all the points.</p><h2>2. When is cubic spline interpolation used?</h2><p>Cubic spline interpolation is commonly used in computer graphics, engineering, and scientific applications to create smooth curves from discrete data points. It is also used in data analysis to fill in missing values or to create a smooth function from noisy data.</p><h2>3. How does cubic spline interpolation differ from other interpolation methods?</h2><p>Unlike linear interpolation, which uses straight lines to connect data points, cubic spline interpolation uses a series of curves to create a smoother and more accurate estimate. Additionally, cubic spline interpolation ensures that the resulting curve is continuous and has a continuous first and second derivative.</p><h2>4. What are the advantages of using cubic spline interpolation?</h2><p>Cubic spline interpolation has several advantages over other interpolation methods. It produces a smoother curve, which is particularly useful for data that has a lot of noise. It also guarantees continuity and smoothness of the curve, and it can handle non-uniformly spaced data points.</p><h2>5. Are there any limitations to using cubic spline interpolation?</h2><p>One limitation of cubic spline interpolation is that it can produce unrealistic values outside of the range of the original data points. Additionally, it may not accurately represent the behavior of the data if there are extreme outliers. It also requires a larger number of data points compared to other interpolation methods to produce an accurate estimate.</p>

1. What is cubic spline interpolation?

Cubic spline interpolation is a mathematical method used to estimate values between data points. It involves fitting a series of cubic polynomials to the data points in order to create a smooth curve that passes through all the points.

2. When is cubic spline interpolation used?

Cubic spline interpolation is commonly used in computer graphics, engineering, and scientific applications to create smooth curves from discrete data points. It is also used in data analysis to fill in missing values or to create a smooth function from noisy data.

3. How does cubic spline interpolation differ from other interpolation methods?

Unlike linear interpolation, which uses straight lines to connect data points, cubic spline interpolation uses a series of curves to create a smoother and more accurate estimate. Additionally, cubic spline interpolation ensures that the resulting curve is continuous and has a continuous first and second derivative.

4. What are the advantages of using cubic spline interpolation?

Cubic spline interpolation has several advantages over other interpolation methods. It produces a smoother curve, which is particularly useful for data that has a lot of noise. It also guarantees continuity and smoothness of the curve, and it can handle non-uniformly spaced data points.

5. Are there any limitations to using cubic spline interpolation?

One limitation of cubic spline interpolation is that it can produce unrealistic values outside of the range of the original data points. Additionally, it may not accurately represent the behavior of the data if there are extreme outliers. It also requires a larger number of data points compared to other interpolation methods to produce an accurate estimate.

Replies
11
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
2
Views
1K
Replies
10
Views
4K
Replies
1
Views
2K
Replies
4
Views
2K
Replies
1
Views
3K
Replies
2
Views
3K
Replies
2
Views
1K