Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
polyfit in 3D

Subject: polyfit in 3D

From: Nicolas Guillemette

Date: 4 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: polyfit in 3D

From: Bruno Luong

Date: 4 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: polyfit in 3D

From: Bruno Luong

Date: 4 May, 2008 19:33:38

Message: 3 of 5

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

Subject: polyfit in 3D

From: alessandro mura

Date: 8 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: polyfit in 3D

From: Roger Stafford

Date: 8 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

No tags are associated with this thread.

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.

Contact us