Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: determining the equation of a 3-D surface
Date: Fri, 21 Nov 2008 19:54:02 +0000 (UTC)
Organization: Lulea University of Technology
Lines: 48
Message-ID: <gg73kq$53o$1@fred.mathworks.com>
References: <gg6ovh$4db$1@fred.mathworks.com> <gg6s8i$4mr$1@fred.mathworks.com> <gg71op$4c1$1@fred.mathworks.com> <gg7322$p7o$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1227297242 5240 172.30.248.37 (21 Nov 2008 19:54:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 21 Nov 2008 19:54:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1595763
Xref: news.mathworks.com comp.soft-sys.matlab:502396


"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg7322$p7o$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gg71op$4c1$1@fred.mathworks.com>...
> > "loic paccard" <loic.paccard@ecam.fr> wrote in message <gg6s8i$4mr$1@fred.mathworks.com>...
> > 
> > > 
> > > I have tried to use spline functions but it only can work with a row x  or a column y of my matrix for the values of elements and I need a global equation for a fmincon minimization.
> > 
> > You might try to use interp2 with 'spline' method.
> > 
> > Bruno
> 
> Thanks for your help Bruno.
> 
> I don't really think that interp2 is useful in my case.
> interp2 can provide efficiently discrete values in different x and y positions than where my data has been taken.
> This is helpful for getting discrete values but it can not give the analytical equation of my surface.
> Maybe I am wrong, in this case tell me how to use interp2 in order to get this analytical equation z= f(x,y).


I agree with John D'Errico. From what you describe it seems like you'd like to find a function that minimizes the total sum of squared distances to your measured data (or minimizes some other norm). In general such a function does not exist.

However, if you have a polynomial model in x and y, for example a second order surface with linear-, cross- and quadratic terms, you could fit this function using a simple least-squares approach. Below is an example. It should run if you just copy and paste... Good luck
/JC

% First, simulate some data... with random noise on it.
[X1,X2] = meshgrid(-1:.2:1, -1:.2:1);
a = [1 .5 -.5 .25 1.2 -1.3];
Y = a(1) + a(2)*X1 + a(3)*X2 + a(4)*X1.*X2 + a(5)*X1.^2 + a(6)*X2.^2;
Y = Y + 0.1*randn(size(Y));   % Add some noise

% Then, generate a grid for your "estimated surface"
[XX1,XX2] = meshgrid(-1:.1:1, -1:.1:1);
X = [ones(prod(size(X1)),1), X1(:) X2(:), ...
    X1(:).*X2(:), X1(:).^2, X2(:).^2];

% Do the least-squares magic
a_hat = inv(X'*X)*X'*Y(:);
Y_hat = a_hat(1) + a_hat(2)*X1 + a_hat(3)*X2 + a_hat(4)*X1.*X2 ...
    + a_hat(5)*X1.^2 + a_hat(6)*X2.^2;

% Plot the result
plot3(X1,X2,Y,'black+','linewidth',2);
hold on;
mesh(X1,X2,Y_hat); colormap(gray(1));
hold off;
grid on;
xlabel('x_1'); ylabel('x_2'); zlabel('y');