Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

function POLYFIT for surfaces ?

817 views
Skip to first unread message

Betti

unread,
Feb 16, 2006, 3:43:53 AM2/16/06
to
Is it possible to use the function POLYFIT to get a polynomial for
surfaces ? Given are points(x,y,z)from a CAD-design (3D) and I need
the coefficients of polynomial.
How can I do that with MATLAB ?
Thanks

BillC <zzvyb6

unread,
Feb 16, 2006, 1:48:33 PM2/16/06
to

Use Spline Toolbox elements (SPAP2, CSAPS, etc)

They make multidimensional surfaces possible.

Tom Lane

unread,
Feb 16, 2006, 1:51:28 PM2/16/06
to

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


Peter Perkins

unread,
Feb 16, 2006, 1:55:11 PM2/16/06
to
Betti wrote:
> Is it possible to use the function POLYFIT to get a polynomial for
> surfaces ? Given are points(x,y,z)from a CAD-design (3D) and I need
> the coefficients of polynomial.

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.

Betti

unread,
Feb 17, 2006, 2:00:58 AM2/17/06
to
Thank you for your reply.
But do you know how I can get the coefficients of polynomial, if I
use the Splin Toolbox ?
Betti

Susan

unread,
Feb 22, 2006, 12:06:08 PM2/22/06
to
Being thick

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.

John D'Errico

unread,
Feb 22, 2006, 12:23:42 PM2/22/06
to

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

susan

unread,
Feb 23, 2006, 10:26:52 AM2/23/06
to
Thanks! Lots of lurvely surfaces being created. Will have to work out
what this is actually doing sometime. But for now, it works.

Assume you meant 'polyfitn' in this line though?:

P = polyfit([x(:),y(:)],z(:),2);

Susan.

John D'Errico

unread,
Feb 23, 2006, 11:59:58 AM2/23/06
to

Sigh. Yes. Thats what I get for not verifying
my response. 8-)

John

Simon Rushton

unread,
Nov 7, 2012, 3:20:12 PM11/7/12
to
I found this thread and it appeared to provide exactly the solution I was looking for - unfortunately it doesn't work...

z is an 85x48 matrix of doubles, so i have

m=size(z,1);
n=size(z,2);
[x,y]=meshgrid(1:n,1:m);

if i do

surf(x,y,z) i see the data as i expect it. so trying to follow the above code as close as possible -

P = polyfit([x(:),y(:)],z(:),2);

zhat = polyval(P,[x(:),y(:)]);
zhat = reshape(zhat,m,n);

surf(x,y,zhat)
hold on
plot3(x,y,z,'o')
hold off

the problem is P = polyfit([x(:),y(:)],z(:),2); generates the following error

??? Error using ==> polyfit at 48
X and Y vectors must be the same size.

any suggestions?

thanks - simon.

Jeff

unread,
Jul 11, 2013, 11:11:06 AM7/11/13
to
"Simon Rushton" <rush...@cardiff.ac.uk> wrote in message <k7efps$i20$1...@newscl01ah.mathworks.com>...
Assuming the author of this last comment has since solved his problem, but for those reading through, there was a coding error above. To use the polyfitN function on exchange, you need to call out polyfitn, and then the evaluation call is to polyvaln. The built-in functions of polyfit and polyval will only take one independent variable.
0 new messages