Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: polyfit in 3D

Subject: polyfit in 3D

From: Nicolas Guillemette

Date: 04 May, 2008 19:12:03

Message: 1 of 5


I'm looking for a function that finds the coefficients of a
polynomial of degree n that fits the data of a three
dimensional set of variables. I have X, Y and Z vectors and
I want to fit a plan (not a curve)on XY plan ?

How can I proceed ?


Thx

Nicolas Guillemette

Subject: Re: polyfit in 3D

From: Bruno Luong

Date: 04 May, 2008 19:26:03

Message: 2 of 5

"Nicolas Guillemette" <nico_guillemette@hotmail.com> wrote
in message <fvl1q3$8sv$1@fred.mathworks.com>...
>
> I'm looking for a function that finds the coefficients of a
> polynomial of degree n that fits the data of a three
> dimensional set of variables. I have X, Y and Z vectors and
> I want to fit a plan (not a curve)on XY plan ?
>
> How can I proceed ?
>

[X(:) Y(:) ones(numel(z),1)] \ Z(:)

provides you (a,b,c) of the fitted plane z=ax+by+c

Bruno

Subject: Re: polyfit in 3D

From: Bruno Luong

Date: 04 May, 2008 19:33:38

Message: 3 of 5

Sorry for the typo (uppercase z)
>
>
> [X(:) Y(:) ones(numel(Z),1)] \ Z(:)
>
>


Subject: Re: polyfit in 3D

From: alessandro mura

Date: 08 May, 2008 21:50:19

Message: 4 of 5

Here's an example. Also: help inline, help fminsearch, ecc....
Regards

Ale



% make some data

p=randn(1,6);

[x,y]=ndgrid([0:.1:1],[0:.1:1]);

z=polyval(p(1:3),x)+polyval(p(4:6),y)+.1*randn(size(x));

plot3(x(:),y(:),z(:),'+')


hold on

disp(p)

clear('p')



%fit

fun='polyval(p(1:3),x)+polyval(p(4:6),y)';

chi=inline(['sum((' fun '-z).^2)'],'p','x','y','z');

h=@(p)chi(p,x(:),y(:),z(:));


options =
optimset('display','on','MaxIter',1d4,'TolFun',1d-49,'TolX',1d-49,'maxfuneval',1d4);

p=fminsearch(h,randn(1,6),options);


zf=feval(inline(fun,'p','x','y'),p,x,y);

surf(x,y,zf)

disp(p)


--
Alessandro Mura
Istituto Nazionale di Astrofisica - IFSI
http://pptt4.ifsi-roma.inaf.it/~mura/index.html
http://www.alessandromura.it


Subject: Re: polyfit in 3D

From: Roger Stafford

Date: 08 May, 2008 23:07:03

Message: 5 of 5

"Nicolas Guillemette" <nico_guillemette@hotmail.com> wrote in message
<fvl1q3$8sv$1@fred.mathworks.com>...
>
> I'm looking for a function that finds the coefficients of a
> polynomial of degree n that fits the data of a three
> dimensional set of variables. I have X, Y and Z vectors and
> I want to fit a plan (not a curve)on XY plan ?
>
> How can I proceed ?
> Thx
> Nicolas Guillemette
------------
  It isn't clear what you are asking, Nicolas. It doesn't require a polynomial of
degree higher than 1 to define a plane, which is what you seem to be asking
for. With only degree 2 in x, y, and z, you would be in the realm of ellipsoids,
hyperboloids, paraboloids, and the like.

  Also you haven't told us what you regard as your criterion for best fit here.
In the case of a plane you might want to minimize the mean square deviation
of the points' z coordinates from that of the plane, or you might want to
minimize their mean square orthogonal distances from the plane. Matlab
provides methods for solving either kind of problem. Similar questions apply
to higher degree polynomial surfaces.

Roger Stafford

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics