Cubic Spline Interpolation Given End Conditions.
7 views (last 30 days)
Show older comments
I'm writing a MATLAB program which accepts 3 inputs x (a vector containing the x values for interpolation), y (a vector containing the y values for interpolation) and a string specifying the type of cubic spline required ('natural', 'parabolically_terminated', 'not_a_knot') and then interpolates these points accordingly.
I've written a sample program (this clearly isn't the finished function) which computes the "a" vector (a vector containing the a coefficients that define the spline) of a natural cubic spline. The end conditions for a natural cubic spline are a1 = 0 and an = 0. The code is here:
x = [-2 -1 0 1 2];
y = [4 -1 2 1 8];
n=length(x) - 1;
q = length(y) - 2;
h = x(2:n+1)-x(1:n);
v1=[h(1:n-1) 0];
v2=[1 2*(h(1:n-1)+h(2:n)) 1];
v3=[0 h(2:n)] ;
M=diag(v1,-1)+diag(v2)+diag(v3,1);
d = [0 ((y(3: q + 2) - y(2: q + 1))./h(2:q + 1) - (y(2:q + 1) - y(1:q ))./h(1:q)) 0];
a = M\d'
I'm just wondering how I would alter this code to deal with a parabolically terminated cubic spline (which has end conditions -a_1 + a_2 = 0 and -a_{n - 1} + a_n = 0)? I know that the only changes will be to the first and last rows of M and d. I'm just not sure HOW they will differ from the natural cubic spline form. Thanks.
0 Comments
Answers (2)
Matt J
on 20 Nov 2013
I'm just wondering how I would alter this code to deal with a parabolically terminated cubic spline (which has end conditions -a_1 + a_2 = 0 and -a_{n - 1} + a_n = 0)?
Well, these are simply additional linear equalities, right? It looks like a matter of just adding them as additional rows to M and d'.
2 Comments
Matt J
on 20 Nov 2013
I can't tell. What I was picturing was that you would start with the M,d from your original post (assuming that's correct) and then simply add 2 additional rows at the end
[m,n]=size(M);
M(m+2,end-1:end)=[-1,1];
M(m+1,[1,2])=[-1,1];
d(end+2)=0;
Matt J
on 20 Nov 2013
One other thing to consider is to use SPARSE to construct your M, since apparently, it is mostly tridiagonal.
This FEX submission (See in particular Example1.m) might offer a quick starting point:
0 Comments
See Also
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!