close all, clear all,clc
disp('N-Dimensional Cardinal (Catmull-Rom) Spline Interpolation v1.2 ')
str=sprintf('%s','Copyright M Khan: khan_goodluck@yahoo.com'); disp(str);
n=100; % number of intervals (i.e. parametric curve would be evaluted n+1 times)
%%%% Cardinal Spline 1D Interpolation %%%%%%%%%%
x=[35 35 16 15 25 40 65 50 60 80 80];
figure
hold on
Tension=0;
for k=1:length(x)-3
xi=crdatnplusoneval([x(k)],[x(k+1)],[x(k+2)],[x(k+3)],Tension,n);
plot(xi,'linewidth',2);
plot(xi(1),'ro','linewidth',2) ;
plot(length(xi),xi(length(xi)),'ro','linewidth',2) ;
end
%%% Cardinal Spline 1D Interpolation
%%%% Cardinal Spline 2D Interpolation %%%%%%%%%%
Px=[35 35 16 15 25 40 65 50 60 80 80];
Py=[47 47 40 15 36 15 25 40 42 27 27];
% Note first and last points are repeated so that spline curve passes
% through all points
% when Tension=0 the class of Cardinal spline is known as Catmull-Rom spline
Tension=0;
figure
% plot(Px,Py,'ro','linewidth',2)
hold on
for k=1:length(Px)-3
[MatOut2]=crdatnplusoneval([Px(k),Py(k)],[Px(k+1),Py(k+1)],[Px(k+2),Py(k+2)],[Px(k+3),Py(k+3)],Tension,n);
% Between each pair of control points plotting n+1 values of first two rows of MatOut
plot(MatOut2(1,:),MatOut2(2,:),'b','linewidth',2)
end
title('\bf2D Cardinal Spline')
%%%% Cardinal Spline 2D Interpolation
%%%% Cardinal Spline 3D Interpolation %%%%%%%%%%
Px=[35 35 16 15 25 40 65 50 60 80 80];
Py=[47 47 40 15 36 15 25 40 42 27 27];
Pz=[-17 -17 20 15 36 15 25 20 25 -7 -7];
% Note first and last points are repeated so that spline curve passes
% through all points
figure
hold on
plot3(Px,Py,Pz,'ro','linewidth',2)
Tension=0;
for k=1:length(Px)-3
[MatOut3]=crdatnplusoneval([Px(k),Py(k),Pz(k)],[Px(k+1),Py(k+1),Pz(k+1)],[Px(k+2),Py(k+2),Pz(k+2)],[Px(k+3),Py(k+3),Pz(k+3)],Tension,n);
% Between each pair of control points plotting n+1 values of first three rows of MatOut
plot3(MatOut3(1,:),MatOut3(2,:),MatOut3(3,:),'b','linewidth',2)
end
title('\bf3D Cardinal Spline')
view(3);
box;
%%%% Cardinal Spline 3D Interpolation
% Using similar approach you can do Cardinal Spline interpolation for
% N-Dimensional data
% % % --------------------------------
% % % Author: Dr. Murtaza Khan
% % % Email : drkhanmurtaza@gmail.com
% % % --------------------------------