
How to draw a closed, smooth cubic spline curve varying the number of breaks?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (1)
Jayanti
le 18 Mar 2025
Modifié(e) : Jayanti
le 18 Mar 2025
Hi Celia,
To create a closed curve, you can use a common parameter "t" that spans from 0 to 1 for all dimensions. Then, generate separate splines for each dimension (x, y, z) using this shared parameter "t".
Please refer to the below code for your reference:
t = linspace(0, 1, npts);
pp_x = spap2(5, 4, t, x);
pp_y = spap2(5, 4, t, y);
pp_z = spap2(5, 4, t, z);
t_dense = linspace(0, 1, 100);
val_x = fnval(pp_x, t_dense);
val_y = fnval(pp_y, t_dense);
val_z = fnval(pp_z, t_dense);
plot3(val_x, val_y, val_z, 'g-', 'LineWidth', 2);
This will create a smooth, closed 3D curve by fitting separate splines to the x, y, and z data using a common parameter “t”.
I have also included an example of the output curve for your reference:

0 commentaires
Voir également
Catégories
En savoir plus sur Spline Postprocessing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!