MATLAB: Reconstruct multivariate spline from csapi

csapiCurve Fitting ToolboxMATLABmultivariate spline

Best Answer

Okay, now I finally figured out what's going on with the ppform for multivariate tensor product splines. No thanks at all to the crappy documentation, though. Since some other posts are related to this question, I'll give a code example here that might be helpful if included in the next documentation as well.
Using the same bivariate example as in the toolbox' documentation, we write the following as initialization:
x = -2:.5:2; y = -1:.25:1; [xx, yy] = ndgrid(x,y);
z = exp(-(xx.^2+yy.^2));
sp = csapi({x,y}, z);
For ease of access, we do a little reshaping of the coefficients
coefs = reshape(sp.coefs, [1, 8, 4, 8, 4]);
Let's say we want to evaluate the spline at
x = [0.75; -0.2]
with
fnval(sp,[0.75 -0.2]')
which gives us the result
0.5491
Now for the manual evaluation. We can determine the proper indices of the polynomial piece to use as
i = [ find(x(1) >= sp.breaks{1}, 1, 'last')
find(x(2) >= sp.breaks{2}, 1, 'last') ];
The value of the bicubic spline is then
y = 0;
for r1 = 1 : 4
for r2 = 1 : 4
r = [r1; r2];
y = y + coefs(1, i(1), r(1), i(2), r(2)) * ...
(x(1) - sp.breaks{1}(i(1)))^(sp.order(1) - r(1)) * ...
(x(2) - sp.breaks{2}(i(2)))^(sp.order(2) - r(2));
end;
end;
and stored in the variable y as
0.5491
Et voilá! Here it is. Not even really tough to figure out, if seen from my new perspective, but the way the documentation is written it was like re-inventing the whole thing. As I said, no thanks at all to the toolbox documentation!
Related Question