How to reconstruct the polynomial form of a multi-dimensional spline at each interval using csaps?

3 views (last 30 days)
I am using csaps to fit a 3-dimensional cubic spline, and need to reconstruct the polynomial form for each interval. while pp.coefs provides the coefficients (in an array format), I am not sure about correct assignment of provided coefficients to different terms of the cubic spline at each interval. Here is my sample code:
x={-1:1:1;-1:1:1;-1:1:1} [xx,yy,zz] = ndgrid(x{1},x{2},x{3}); v=2*xx.^2+4*yy.^2-3*xx.*zz; [pp,p]=csaps(x,v);

Answers (1)

sanidhyak
sanidhyak on 18 Feb 2025
Edited: sanidhyak on 3 Mar 2025
Hi @Adel,
I understand that you are trying to reconstruct the polynomial form of a multi-dimensional spline at each interval using MATLAB’s csaps function.
When working with multi-dimensional splines, we need to extract and assign the coefficients from pp.coefs to their respective polynomial terms.
The approach would be as follows:
  • Use csaps to generate a 3-dimensional cubic spline.
  • Extract coefficients using pp.coefs.
  • Iterate through each interval, reconstructing and displaying the polynomial terms using syms.
This will make sure that the correct polynomial form for each interval is obtained from the csaps output.
Below is the MATLAB code for the same:
x = {-1:1:1, -1:1:1, -1:1:1};
[xx, yy, zz] = ndgrid(x{1}, x{2}, x{3});
v = 2*xx.^2 + 4*yy.^2 - 3*xx.*zz;
[pp, p] = csaps(x, v);
% Extract coefficients
coefs = pp.coefs;
% Display coefficients for each dimension
for i = 1:size(coefs, 1)
fprintf('Interval %d:\n', i);
disp(coefs(i, :));
end
% Reconstruct polynomial terms manually
syms X Y Z
for i = 1:size(coefs, 1)
poly = coefs(i, 1)*X^3 + coefs(i, 2)*Y^3 + coefs(i, 3)*Z^3 + ...
coefs(i, 4)*X^2 + coefs(i, 5)*Y^2 + coefs(i, 6)*Z^2 + ...
coefs(i, 7)*X*Y + coefs(i, 8)*X*Z + coefs(i, 9)*Y*Z + ...
coefs(i, 10)*X + coefs(i, 11)*Y + coefs(i, 12)*Z + coefs(i, 13);
fprintf('Polynomial for interval %d:\n', i);
disp(vpa(poly, 4));
end
For further reference on csaps, kindly refer to the following documentation:
I hope this helps!
(As can be seen in the screenshot below, the error has been resolved)

Community Treasure Hunt

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

Start Hunting!