How to build polynomial with cscvn function coeffs?
Show older comments
Hello, could you tell please how to restore the polynomial of a spline segment obtained by the cscvn function? I took the code from the help and now I want to get polynomials, how can I compose them correctly? Thank you
npts = 13;
t = linspace(0,8*pi,npts);
z = linspace(-1,1,npts);
omz = sqrt(1-z.^2);
xyz = [cos(t).*omz; sin(t).*omz; z];
plot3(xyz(1,:),xyz(2,:),xyz(3,:),'ro','LineWidth',2);
text(xyz(1,:),xyz(2,:),xyz(3,:),[repmat(' ',npts,1), num2str((1:npts)')]);
ax = gca;
ax.XTick = [];
ax.YTick = [];
ax.ZTick = [];
box on;
hold on;
fnplt(cscvn(xyz(:,[1:end 1])),'r',2);
s = cscvn(xyz(:,[1:end 1])),'r',2;
%----------------------------------------------------------
figure;
t1 = s.breaks(1):0.02:s.breaks(2);
x1 = s.coefs(1, 1) * t1.^3 + s.coefs(1, 2) * t1.^2 + s.coefs(1, 3)* t1 + s.coefs(1, 4);
y1 = s.coefs(2, 1) * t1.^3 + s.coefs(2, 2) * t1.^2 + s.coefs(2, 3) * t1 + s.coefs(2, 4);
z1 = s.coefs(3, 1) * t1.^3 + s.coefs(3, 2) * t1.^2 + s.coefs(3, 3) * t1 + s.coefs(3, 4);
t2 = s.breaks(2):0.02:s.breaks(3);
x2 = s.coefs(4, 1) * t2.^3 + s.coefs(4, 2) * t2.^2 + s.coefs(4, 3) * t2 + s.coefs(4, 4);
y2 = s.coefs(5, 1) * t2.^3 + s.coefs(5, 2) * t2.^2 + s.coefs(5, 3) * t2 + s.coefs(5, 4);
z2 = s.coefs(6, 1) * t2.^3 + s.coefs(6, 2) * t2.^2 + s.coefs(6, 3) * t2 + s.coefs(6, 4);
t3 = s.breaks(3):0.02:s.breaks(4);
x3 = s.coefs(7, 1) * t3.^3 + s.coefs(7, 2) * t3.^2 + s.coefs(7, 3) * t3 + s.coefs(7, 4);
y3 = s.coefs(8, 1) * t3.^3 + s.coefs(8, 2) * t3.^2 + s.coefs(8, 3) * t3 + s.coefs(8, 4);
z3 = s.coefs(9, 1) * t3.^3 + s.coefs(9, 2) * t3.^2 + s.coefs(9, 3) * t3 + s.coefs(9, 4);
t4 = s.breaks(4):0.02:s.breaks(5);
x4 = s.coefs(10, 1) * t4.^3 + s.coefs(10, 2) * t4.^2 + s.coefs(10, 3) * t4 + s.coefs(10, 4);
y4 = s.coefs(11, 1) * t4.^3 + s.coefs(11, 2) * t4.^2 + s.coefs(11, 3) * t4 + s.coefs(11, 4);
z4 = s.coefs(12, 1) * t4.^3 + s.coefs(12, 2) * t4.^2 + s.coefs(12, 3) * t4 + s.coefs(12, 4);
plot3(x1, y1, z1, "Color", 'r', 'LineWidth', 4);
box on;
hold on;
plot3(x2, y2, z2, "Color", 'b', 'LineWidth', 4);
plot3(x3, y3, z3, "Color", 'g', 'LineWidth', 4);
plot3(x4, y4, z4, "Color", 'b', 'LineWidth', 4);
hold off;
Answers (1)
If you want to evaluate the spline, don't try to make polynomials. Just use the output argument from your call to cscvn with the ppval function.
points=[0 1 1 0 -1 -1 0 0; 0 0 1 2 1 0 -1 -2];
S = cscvn(points);
% Plot the spline
fnplt(S);
% Evaluate the spline at certain points and plot them
x = 0:0.5:7;
values = ppval(S, x);
hold on
plot(values(1, :), values(2, :), 'o')
If you are required to create polynomials, remember that the output of cscvn is in ppform. See the definition of ppform on that page. You need to subtract off the breaks before evaluating your polynomials. But I wouldn't make the polynomials myself, I'd just use polyval as shown on that documentation page.
1 Comment
Categories
Find more on Polynomials in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!