Cubic Spline Interpolation Given End Conditions.
조회 수: 7 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Matt J
2013년 11월 20일
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
Matt J
2013년 11월 20일
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
2013년 11월 20일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!