Use Spline Toolbox elements (SPAP2, CSAPS, etc)
They make multidimensional surfaces possible.
You can use backslash after creating a matrix containg the polynomial terms
you want. Here I generate data exactly fitting a model with terms up to 2nd
order:
>> x = rand(100,1);
>> y = 5 + 10*rand(100,1);
>> z = 100 - 5*x + y - 2*x.*y + x.^2;
>> [ones(100,1), x, y, x.^2 y.^2 x.*y] \ z
ans =
100.0000
-5.0000
1.0000
1.0000
0.0000
-2.0000
If you have the Statistics Toolbox, you can use functions like x2fx to
create matrix of term values, and regstats to compute other statistics in
addition to the coefficient estimates.
-- Tom
It's very easy to fit a polynomial surface in N-D the way that POLYFIT does it
in 2D, you just have to decide what main terms and interaction terms you need
and create the design matrix, and use the backslash operator.
But your mention of CAD makes me think you don't want to fit a polynomial the
way that POLYFIT does. As the other respondent mentioned, you might want to
look into the Splines Toolbox.
Hope this helps.
- Peter Perkins
The MathWorks, Inc.
HOW do you do this?
(I really do want to fit a 2nd order polynomial to a 2D surface. I
have an m by n matrix of noisy data and I need to find the overall 2D
trend by fitting a function z=x + x*x + y + y*y + x*y + const.)
Everything I've found so far has been along the lines of finding a
surface that includes non-uniformly spaced points, rather than a
smooth one going vaguely near uniformly spaced ones.
You can now use polyfitn to fit this surface.
Its on the file exchange.
<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10065&objectType=FILE>
If your surface is in the array z, then
assuming x is the row index, and y the
column index, it would work something like
this:
[x,y] = meshgrid(1:n,1:m);
P = polyfitn([x(:),y(:)],z(:),'x, x*x, y, y*y, x*y, constant');
This would fit a model with terms in the order
you specified above.
Aternatively, you could have just had it
generate a complete second order model:
P = polyfit([x(:),y(:)],z(:),2);
Get predictions:
zhat = polyvaln(P,[x(:),y(:)]);
zhat = reshape(zhat,m,n);
and plot the surface with the data:
surf(x,y,zhat)
hold on
plot3(x,y,z,'o')
hold off
HTH,
John D'Errico
Assume you meant 'polyfitn' in this line though?:
P = polyfit([x(:),y(:)],z(:),2);
Susan.
Sigh. Yes. Thats what I get for not verifying
my response. 8-)
John