How to draw a closed, smooth cubic spline curve varying the number of breaks?

5 views (last 30 days)
I have a set of points in space which describe a circumference with noise. I want to approximate those points to a closed curve using cubic splines with the breaks (or knots) as the input. The function spap2 seems to accomplish most of my requirements but I am not able to create a closed curve.
Any idea?
close
clear
r = 10;
s = 1;
for i = 0:5:64;
x(s) = r*sin(i/10) + rand;
y(s) = r*cos(i/10) + rand;
z(s) = r;
s=s+1;
end
% Repeat first point to make it periodic
x = horzcat(x, x(1));
y = horzcat(y, y(1));
z = horzcat(z, z(1));
xyz=[x;y;z];
figure(1)
npts = length(x);
plot3(xyz(1,:),xyz(2,:),xyz(3,:),'ro','LineWidth',2);
text(xyz(1,:),xyz(2,:),xyz(3,:),[repmat(' ',npts,1), num2str((1:npts)')])
hold on
% using the number of knots = 5
[pp] = spap2(5,4,x, [y;z]);
val = fnval(pp, linspace(min(x), max(x),100));
plot3(linspace(min(x), max(x),100),val(1,:), val(2,:), 'g-','LineWidth',2);
grid on
hold off

Answers (0)

Community Treasure Hunt

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

Start Hunting!