
How to draw a closed, smooth cubic spline curve varying the number of breaks?
조회 수: 5 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Jayanti
2025년 3월 18일
편집: Jayanti
2025년 3월 18일
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
참고 항목
카테고리
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!