Path: news.mathworks.com!newsfeed-00.mathworks.com!kanaga.switch.ch!switch.ch!news.belwue.de!news.germany.com!aioe.org!not-for-mail
From: "alessandro mura" <fake@address.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: polyfit in 3D
Date: Thu, 8 May 2008 23:50:19 +0200
Organization: Aioe.org NNTP Server
Lines: 55
Message-ID: <fvvsir$mni$1@aioe.org>
References: <fvl1q3$8sv$1@fred.mathworks.com>
NNTP-Posting-Host: uoQB/dM3E5DXspWCV/kC+Q.user.aioe.org
X-Complaints-To: abuse@aioe.org
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-Priority: 3
X-MSMail-Priority: Normal
Xref: news.mathworks.com comp.soft-sys.matlab:467506


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