Thread Subject: determining the equation of a 3-D surface

Subject: determining the equation of a 3-D surface

From: loic paccard

Date: 21 Nov, 2008 16:52:01

Message: 1 of 18

Hi everybody,

I collect some data in this way:
13 positions x (discrete points: values from -36 to 36 by a 6 step)
13 positions y (discrete points: values from -36 to 36 by a 6 step)
basically it is a grid of 169 points
And each point has a z-value that I am trying to minimize.

I can plot a surface z=f(x,y) but I need to minimize it for several samples.
So the best minimizer would be fmincon in my case but how I can get an equation z=f(x,y) from 169 discrete points??

I want to say that using griddata is helpless in my case, I do not need to interpolate but to be able to use fmincon for my discrete values.

I have looked for hours and it does not seem like fmincon can use directly elements values in matrix referenced by row and column as a function AND there is no function able to get a 3-D equation in my case.

I will be glad to receive some help,
loic.

Subject: determining the equation of a 3-D surface

From: loic paccard

Date: 21 Nov, 2008 17:48:02

Message: 2 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg6ovh$4db$1@fred.mathworks.com>...
> Hi everybody,
>
> I collect some data in this way:
> 13 positions x (discrete points: values from -36 to 36 by a 6 step)
> 13 positions y (discrete points: values from -36 to 36 by a 6 step)
> basically it is a grid of 169 points
> And each point has a z-value that I am trying to minimize.
>
> I can plot a surface z=f(x,y) but I need to minimize it for several samples.
> So the best minimizer would be fmincon in my case but how I can get an equation z=f(x,y) from 169 discrete points??
>
> I want to say that using griddata is helpless in my case, I do not need to interpolate but to be able to use fmincon for my discrete values.
>
> I have looked for hours and it does not seem like fmincon can use directly elements values in matrix referenced by row and column as a function AND there is no function able to get a 3-D equation in my case.
>
> I will be glad to receive some help,
> loic.

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.

Subject: determining the equation of a 3-D surface

From: Bruno Luong

Date: 21 Nov, 2008 19:22:02

Message: 3 of 18

"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

Subject: determining the equation of a 3-D surface

From: John D'Errico

Date: 21 Nov, 2008 19:43:12

Message: 4 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg6ovh$4db$1@fred.mathworks.com>...
> Hi everybody,
>
> I collect some data in this way:
> 13 positions x (discrete points: values from -36 to 36 by a 6 step)
> 13 positions y (discrete points: values from -36 to 36 by a 6 step)
> basically it is a grid of 169 points
> And each point has a z-value that I am trying to minimize.
>
> I can plot a surface z=f(x,y) but I need to minimize it for several samples.
> So the best minimizer would be fmincon in my case but how I can get an equation z=f(x,y) from 169 discrete points??
>
> I want to say that using griddata is helpless in my case, I do not need to interpolate but to be able to use fmincon for my discrete values.
>
> I have looked for hours and it does not seem like fmincon can use directly elements values in matrix referenced by row and column as a function AND there is no function able to get a 3-D equation in my case.
>
> I will be glad to receive some help,
> loic.

There is no magic solution that will give you an equation
from data points. Not fmincon, not griddata, nor my own
gridfit.

The fact is, there are an infinite (I won't hazard a guess at
the order of the infinity) number of ways to model any set
of points. The splines produced by some tools (griddata
is one version of what might call a low order spline) will
give you an interpolant, but they do not give you a function
in any usable form that you can write down easily.

In order to realize a usable model, you need to invest some
time and effort of your own. (As with almost anything, you
get what you pay for.) You need to postulate a viable model
for the process, then you can try to estimate the parameters
of that model from the data. Without that model, you are
left with little more than something simple, like a polynomial
model. My polyfitn can build such a model, but even there
you must tell it the order of the model or the specific terms
to be used.

http://www.mathworks.com/matlabcentral/fileexchange/10065

HTH,
John

Subject: determining the equation of a 3-D surface

From: loic paccard

Date: 21 Nov, 2008 19:44:02

Message: 5 of 18

"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).

Subject: determining the equation of a 3-D surface

From: Bruno Luong

Date: 21 Nov, 2008 19:53:02

Message: 6 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg7322$p7o$1@fred.mathworks.com>...

> 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 don't think minimizer require an analytic expression. All it needs is you will be able to evaluate a function - preferable continuously derivable - at any point.

And that is exactly interp2 with spline function can give you.

Otherwise you have to give up the fact that the model (could be an analytic expression) interpolate the data, but only *approximate* your data. In this case you might call a fitting with multi-variate polynomial, or spline functions.

Bruno

Subject: determining the equation of a 3-D surface

From: Johan Carlson

Date: 21 Nov, 2008 19:54:02

Message: 7 of 18

"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');

Subject: determining the equation of a 3-D surface

From: loic paccard

Date: 21 Nov, 2008 20:07:02

Message: 8 of 18

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gg730g$ofn$1@fred.mathworks.com>...
> There is no magic solution that will give you an equation
> from data points. Not fmincon, not griddata, nor my own
> gridfit.
>
> The fact is, there are an infinite (I won't hazard a guess at
> the order of the infinity) number of ways to model any set
> of points. The splines produced by some tools (griddata
> is one version of what might call a low order spline) will
> give you an interpolant, but they do not give you a function
> in any usable form that you can write down easily.
>
> In order to realize a usable model, you need to invest some
> time and effort of your own. (As with almost anything, you
> get what you pay for.) You need to postulate a viable model
> for the process, then you can try to estimate the parameters
> of that model from the data. Without that model, you are
> left with little more than something simple, like a polynomial
> model. My polyfitn can build such a model, but even there
> you must tell it the order of the model or the specific terms
> to be used.
>
> http://www.mathworks.com/matlabcentral/fileexchange/10065
>
> HTH,
> John


Thank you John for your realistic message.
To be honest I was expecting that I was actually impossible to get a 3-D analytical expression from data without knowing some hypothesis about this hypothetic equation.
And that is my case, I have no model of this curve, I am just trying to minimize (by fmincon) the z value of it in order to get the x and y positions.

But assuming that my function could be approximate by a polynomial analytical expression...

We both know that we can use polyfit for a row or column vector.
Is there a function in matlab able to do it for a matrix?
is your polyfit function able to give me an approximated analytical expression if I can guess the polynom order that I need to use??


Subject: determining the equation of a 3-D surface

From: loic paccard

Date: 21 Nov, 2008 20:17:01

Message: 9 of 18

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gg73iu$44n$1@fred.mathworks.com>...
> "loic paccard" <loic.paccard@ecam.fr> wrote in message <gg7322$p7o$1@fred.mathworks.com>...
>
> > 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 don't think minimizer require an analytic expression. All it needs is you will be able to evaluate a function - preferable continuously derivable - at any point.
>
> And that is exactly interp2 with spline function can give you.
>
> Otherwise you have to give up the fact that the model (could be an analytic expression) interpolate the data, but only *approximate* your data. In this case you might call a fitting with multi-variate polynomial, or spline functions.
>
> Bruno

Bruno,

What I need is minimizing my z values and the best minimizer would be obviously
fmincon in my case.
I have looked for finding a way to use fmincon without an analytical expression but just function evaluations (by using interp2 I can have much more evaluations of my functions in the grid from my real data).

But fmincon seems to requires an analytical expression whatever:

Find minimum of constrained nonlinear multivariable function
Equation

Finds the minimum of a problem specified by

x, b, beq, lb, and ub are vectors, A and Aeq are matrices, c(x) and ceq(x) are functions that return vectors, and f(x) is a function that returns a scalar. f(x), c(x), and ceq(x) can be nonlinear functions.

Subject: determining the equation of a 3-D surface

From: Bruno Luong

Date: 21 Nov, 2008 20:26:02

Message: 10 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message
>
> But fmincon seems to requires an analytical expression whatever:

Not necessary. Here is an example, and it's from John. http://www.mathworks.com/matlabcentral/newsreader/view_thread/236835

Bruno

Subject: determining the equation of a 3-D surface

From: John D'Errico

Date: 21 Nov, 2008 20:28:02

Message: 11 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg74d6$hqs$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <gg730g$ofn$1@fred.mathworks.com>...
> > There is no magic solution that will give you an equation
> > from data points. Not fmincon, not griddata, nor my own
> > gridfit.
> >
> > The fact is, there are an infinite (I won't hazard a guess at
> > the order of the infinity) number of ways to model any set
> > of points. The splines produced by some tools (griddata
> > is one version of what might call a low order spline) will
> > give you an interpolant, but they do not give you a function
> > in any usable form that you can write down easily.
> >
> > In order to realize a usable model, you need to invest some
> > time and effort of your own. (As with almost anything, you
> > get what you pay for.) You need to postulate a viable model
> > for the process, then you can try to estimate the parameters
> > of that model from the data. Without that model, you are
> > left with little more than something simple, like a polynomial
> > model. My polyfitn can build such a model, but even there
> > you must tell it the order of the model or the specific terms
> > to be used.
> >
> > http://www.mathworks.com/matlabcentral/fileexchange/10065
> >
> > HTH,
> > John
>
>
> Thank you John for your realistic message.
> To be honest I was expecting that I was actually impossible to get a 3-D analytical expression from data without knowing some hypothesis about this hypothetic equation.
> And that is my case, I have no model of this curve, I am just trying to minimize (by fmincon) the z value of it in order to get the x and y positions.
>

fmincon will not help you here without a model of
some form.

>
> But assuming that my function could be approximate by a polynomial analytical expression...
>
> We both know that we can use polyfit for a row or column vector.
> Is there a function in matlab able to do it for a matrix?
> is your polyfit function able to give me an approximated analytical expression if I can guess the polynom order that I need to use??
>

polyfitn does it for the n-d case, but you do need to
choose the order of your model. And beware fitting
very high order models, just to get an accurate fit.

Use meshgrid to generate x and y. For example,

[x,y] = meshgrid(-36:6:36);
xy = [x(:),y(:)];
model = polyfitn(xy,z,3);

HTH,
John

Subject: determining the equation of a 3-D surface

From: loic paccard

Date: 21 Nov, 2008 20:38:01

Message: 12 of 18

"Johan Carlson" <Johan.E.Carlson@gmail.com> wrote in message <gg73kq$53o$1@fred.mathworks.com>...

Your code is very great for minimizing the total sum of squared distances to my measured data but this is not really the solution to my problem..

Maybe I should explain more..
I am working on calibrating a probe and data is taken in 169 points in a plane x by y where x is between -36 to 36 by a 6 degree step and the same for y.

By measuring data in these points, a calculation using data discrete values give me a z value for each point.
So 169 values in my grid.
These 169 values define my surface, there is no noise, it is a physical shape surface of the probe's sensitivity.
The goal is to minimize this value and get the position (x,y) associated with.
For this I can interpolate easily my data values in other points of the grid by interp2 but what I need is using fmincon for get a minimum in a optimized way.

And fmincon seems to work only with analytical expression function and not with
evaluated values of a variable.

I know that I could use min(min(MYmatrix) but the final purpose is to get accuracy on x and y about 0.1 for a great number of samples. So a basic search like min(min(..)) will be too lomg.

So I definitively need to use a powerful minimizer like fmincon.

Subject: determining the equation of a 3-D surface

From: Bruno Luong

Date: 21 Nov, 2008 20:47:02

Message: 13 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg7679$i6v$1@fred.mathworks.com>...

>
> And fmincon seems to work only with analytical expression function and not with
> evaluated values of a variable.
>

I repeat again: the above is not true. fmincon can even work with any function computed from a sophisticated algorithm, there is no requirement what so ever about analytical form of the function.

Bruno

Subject: determining the equation of a 3-D surface

From: John D'Errico

Date: 21 Nov, 2008 20:55:03

Message: 14 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg7679$i6v$1@fred.mathworks.com>...
> "Johan Carlson" <Johan.E.Carlson@gmail.com> wrote in message <gg73kq$53o$1@fred.mathworks.com>...
>
> Your code is very great for minimizing the total sum of squared distances to my measured data but this is not really the solution to my problem..
>
> Maybe I should explain more..
> I am working on calibrating a probe and data is taken in 169 points in a plane x by y where x is between -36 to 36 by a 6 degree step and the same for y.
>
> By measuring data in these points, a calculation using data discrete values give me a z value for each point.
> So 169 values in my grid.
> These 169 values define my surface, there is no noise, it is a physical shape surface of the probe's sensitivity.
> The goal is to minimize this value and get the position (x,y) associated with.
> For this I can interpolate easily my data values in other points of the grid by interp2 but what I need is using fmincon for get a minimum in a optimized way.
>
> And fmincon seems to work only with analytical expression function and not with
> evaluated values of a variable.
>
> I know that I could use min(min(MYmatrix) but the final purpose is to get accuracy on x and y about 0.1 for a great number of samples. So a basic search like min(min(..)) will be too lomg.
>
> So I definitively need to use a powerful minimizer like fmincon.
>

You actually want to find a minimum point on the
surface?

Just use interp2, with one of the smooth interpolants,
not the default interpolation method which is bilinear.
Thus, use either 'cubic' or 'spline' as the method.

Then, use an optimizer, applied to the prediction
interpolated by interp2. The starting value you would
use is just the location of the minimum value at any
of the nodes in the lattice. You might want to apply
bounds to the optimizer, to keep it within the range
of the data points, so fmincon is an option, but also
my fminsearchbnd from the file exchange. You do
not need anything complicated for the optimization.

http://www.mathworks.com/matlabcentral/fileexchange/8277

John

Subject: determining the equation of a 3-D surface

From: loic paccard

Date: 21 Nov, 2008 21:16:03

Message: 15 of 18

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gg7777$52l$1@fred.mathworks.com>...
> "loic paccard" <loic.paccard@ecam.fr> wrote in message <gg7679$i6v$1@fred.mathworks.com>...
> > "Johan Carlson" <Johan.E.Carlson@gmail.com> wrote in message <gg73kq$53o$1@fred.mathworks.com>...
> >
> You actually want to find a minimum point on the
> surface?
>
> Just use interp2, with one of the smooth interpolants,
> not the default interpolation method which is bilinear.
> Thus, use either 'cubic' or 'spline' as the method.
>
> Then, use an optimizer, applied to the prediction
> interpolated by interp2. The starting value you would
> use is just the location of the minimum value at any
> of the nodes in the lattice. You might want to apply
> bounds to the optimizer, to keep it within the range
> of the data points, so fmincon is an option, but also
> my fminsearchbnd from the file exchange. You do
> not need anything complicated for the optimization.
>
> http://www.mathworks.com/matlabcentral/fileexchange/8277
>
> John

I just to minimize you are right.
Either fmincon or fminsearchbnd would be a way.
Ok but how can I write my objective function fun??
I have only predicted values from real data and interpolations.

x = fminsearch(fun,x0,options)

Subject: determining the equation of a 3-D surface

From: John D'Errico

Date: 21 Nov, 2008 22:06:03

Message: 16 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg78ej$pbp$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <gg7777$52l$1@fred.mathworks.com>...
> > "loic paccard" <loic.paccard@ecam.fr> wrote in message <gg7679$i6v$1@fred.mathworks.com>...
> > > "Johan Carlson" <Johan.E.Carlson@gmail.com> wrote in message <gg73kq$53o$1@fred.mathworks.com>...
> > >
> > You actually want to find a minimum point on the
> > surface?
> >
> > Just use interp2, with one of the smooth interpolants,
> > not the default interpolation method which is bilinear.
> > Thus, use either 'cubic' or 'spline' as the method.
> >
> > Then, use an optimizer, applied to the prediction
> > interpolated by interp2. The starting value you would
> > use is just the location of the minimum value at any
> > of the nodes in the lattice. You might want to apply
> > bounds to the optimizer, to keep it within the range
> > of the data points, so fmincon is an option, but also
> > my fminsearchbnd from the file exchange. You do
> > not need anything complicated for the optimization.
> >
> > http://www.mathworks.com/matlabcentral/fileexchange/8277
> >
> > John
>
> I just to minimize you are right.
> Either fmincon or fminsearchbnd would be a way.
> Ok but how can I write my objective function fun??
> I have only predicted values from real data and interpolations.
>
> x = fminsearch(fun,x0,options)

I'll assume that z is a 13x13 array of points.

[x,y] = meshgrid(-36:6:36);
fun = @(xy) interp2(x,y,z,xy(1),xy(2),'spline');
[junk,ind] = min(z(:));
xystart = [x(ind),y(ind)];

Now just optimize fun. You might want to use a
bounded optimizer, either fmincon or fminsearchbnd
to keep you inside [-36,36]X[-36,36]. Something
like this:

xy = fminsearchbnd(fun,xystart,[-36 -36],[36 36]);

John

Subject: determining the equation of a 3-D surface

From: loic paccard

Date: 21 Nov, 2008 22:13:01

Message: 17 of 18

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gg7bcb$asf$1@fred.mathworks.com>...

Thanks a lot John you're very helpful!

Subject: determining the equation of a 3-D surface

From: Simone

Date: 22 Apr, 2009 15:14:02

Message: 18 of 18

"loic paccard" <loic.paccard@ecam.fr> wrote in message <gg7bpd$i6c$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <gg7bcb$asf$1@fred.mathworks.com>...
>
> Thanks a lot John you're very helpful!

Dear All,
I am kind of new to Matlab and confused I suppose, I think we should be able to use the function defined in "fun" by D'Errico also to evaluate other data points but they have to be Meshgrid type or monotonic vectors, is that true? Is there a way of getting the original data points evaluated, instead of the meshgrid points or other interpolations??? Is this interpolation the same as polyfitn(thanks for this toolbox, it's very nice!!!) ???
I have data in x,y,z format, trying to get a relationship among the three, possibly functional, but I'm getting poor fit to my data with polynomial fitting (for sure graphically in Mathematica and instead for sure numerically in Matlab): I thought it could be much more interesting to have a more general (more destructured) form for the function and that the "fun" specified above by D'Errico could be good for my case, if I were able to use it.
(I don't have access to surface Fitting Tool, would that solve it though???)
I can't find the way, maybe I am really too confused!!!
thanks a lot for taking the time to dissipate my doubts,
best regards,
Simone

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
optimization loic paccard 21 Nov, 2008 12:32:51
3d equation loic paccard 21 Nov, 2008 12:29:11
3d surface loic paccard 21 Nov, 2008 12:28:50
minimization loic paccard 21 Nov, 2008 12:28:27
fmincon loic paccard 21 Nov, 2008 12:28:13
fitting data in... loic paccard 21 Nov, 2008 12:04:19
fmincon minimiz... loic paccard 21 Nov, 2008 12:04:02
equation 3d sur... loic paccard 21 Nov, 2008 11:55:06
rssFeed for this Thread
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com