How to do Not-a-Knot spline in MATLAB without MATLAB's "spline" function?

I have the following code for a spline with natural end conditions. I am tasked with modifying it to work with not-a-knot and zero slope clamped end conditions. I am having trouble figuring out how to do that. Can anyone help me??
function [yy,dy,d2] = my_spline(x,y,xx)
n=length(x);
if length(y)~=n, error('x and y must have the same length'); end
if any(diff(x)<=0), error('x not strictly ascending'); end
m=length(xx);
aa(1,1)=1; aa(n,n)=1;
bb=zeros(n,1);
for i=2:n-1
aa(i,i-1)=h(x,i-1);
aa(i,i)=2*(h(x,i-1)+h(x,i));
aa(i,i+1)=h(x,i);
bb(i)=3*(fd(i+1,i,x,y)-fd(i,i-1,x,y));
end
c=aa\bb;
for i=1:n-1
a(i)=y(i);
b(i)=fd(i+1,i,x,y)-h(x,i)/3*(2*c(i)+c(i+1));
d(i)=(c(i+1)-c(i))/3/h(x,i);
end
for i=1:m
[yy(i),dy(i),d2(i)]=SplineInterp(x,n,a,b,c,d,xx(i));
end
end
function hh=h(x,i)
hh=x(i+1)-x(i);
end
function fdd=fd(i,j,x,y)
fdd=(y(i)-y(j))/(x(i)-x(j));
end
function [yyy,dyy,d2y]=SplineInterp(x,n,a,b,c,d,xi)
for ii=1:n-1
if xi>=x(ii)-0.000001 & xi<=x(ii+1)+0.000001
yyy=a(ii)+b(ii)*(xi-x(ii))+c(ii)*(xi-x(ii))^2+d(ii)*(xi-x(ii))^3;
dyy=b(ii)+2*c(ii)*(xi-x(ii))+3*d(ii)*(xi-x(ii))^2;
d2y=2*c(ii)+6*d(ii)*(xi-x(ii));
break
end
end
end

1 Comment

Could you please present the code in a code block for better readibility?

Sign in to comment.

Answers (0)

Products

Asked:

on 8 Nov 2016

Edited:

on 8 Nov 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!