Generate an equation from a 3d surface

Hi there,
I am wondering if it is possible to generate an equation from a 3d surface? I have a surface that is composed of three different equations at certain ranges of 'y'. Once graphed, I want to be able to create one equation which can reproduce the same shape. Is this possible? Here's a sample of the curve I have that I would like to convert to equation:
x = [0:10];
y = {0:3; 3:5; 5:10};
Eq1 = @(x,y)(x.*y);
Eq2 = @(x,y)(2.*x.*y);
Eq3 = @(x,y)(3.*x.*y);
[X1,Y1] = meshgrid(x,y{1});
[X2,Y2] = meshgrid(x,y{2});
[X3,Y3] = meshgrid(x,y{3});
Z1 = Eq1(X1,Y1);
Z2 = Eq2(X2,Y2);
Z3 = Eq3(X3,Y3);
figure(1)
s1 = surf(X1,Y1,Z1);
hold on
s2 = surf(X2,Y2,Z2);
s3 = surf(X3,Y3,Z3);
hold off

2 Comments

As you must be aware, the infinite surface in terms of the original equations, Eq1, Eq2, and Eq3, has discontinuities along the lines y = 3 and y = 5. There can be no analytic function that expresses such a surface, and, in the ordinary sense of the term 'equation', there is also no single equation that would define such a surface.
In terms of the particular discrete grid you used for x and y, there is of course a way of defining a continuous surface that would pass through all 141 points, and in fact there are infinitely many such possible surfaces since there are no constraints on the values of z between the discrete points for x and y, so again your request seems ill-defined.
Can you possibly describe what you are trying to accomplish in more careful detail? It doesn't seem to make sense as it stands.
Thank you for your response. Basically, I am trying to create a new formula which encompasses three formulas which come into play at different 'ranges' of y. Does this make sense?

Sign in to comment.

Answers (3)

In Matlab, you can compact the three equations into one as written below. It is not a theoretical modeling anyway, but simply a easier way to implement multiple equations under certain constraints.
Eq = @(x,y) x.*y.*(y>=0 & y<3) + 2*x.*y.*(y>=3 & y<5) + 3*x.*y.*(y>=5 & y<=10);
x = 0:10; y = 0:10;
[X,Y] = meshgrid(x,y);
Z = Eq(X,Y);
surf(X,Y,Z)

1 Comment

Thank you. I knew how to do that.
But what I'm looking for is to create an equation which would reproduce the same surface outside of matlab.

Sign in to comment.

If you'd like to model/estimate it as a 2D polynomial, you can use John D'Errico's polyfitn() http://www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn

2 Comments

Thank you! Would I be able to do it for a 3d surface such as the one above?
It fits in n dimensions. But you don't have a 3D equation . You have two independent variables, x and y, and one dependent variable, z. Thus you have a 2D situation. The fact that you can make a 2.5D rendering of your Z data with a surface does not change the fact that it is still a 2D problem . Attached is a demo where I use polyfitn() to fit a background illumination image to a 2D quadratic.

Sign in to comment.

You can play a trick here. Use your code first:
Eq = @(x,y) x.*y.*(y>=0 & y<3) + 2*x.*y.*(y>=3 & y<5) + 3*x.*y.*(y>=5 & y<=10);
x = 0:10; y = 0:10;
[X,Y] = meshgrid(x,y);
Z = Eq(X,Y);
Get polynomial model of Z in terms of X and Y. I used curved fitting tool, and observed that you can model your data with a second degree polynomial. The polynomial is: f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2. You will also find the coefficients while using the tool:
p00 = 4.021;
p10 = -2.545;
p01 = -2.681;
p20 = -8.291e-16;
p11 = 3.273;
p02 = 0.2681;
Now, evaluate the model function:
f = @(x,y) p00 + p10*x + p01*y + p20*x.^2 + p11*x.*y + p02*y.^2;
Z2 = f(X,Y);
And plot both original (Z) and modeled (Z2) data on same figure to see how they are similar to each other.
h(1) = surf(X,Y,Z); hold on
h(2) = surf(X,Y,Z2);
You may also set the two different colormaps for two surfaces to differentiate them clearly.
set(h(1),'CData',zeros(size(X))); % colormap 1
set(h(2),'CData',0.5*ones(size(X))); % colormap 2
legend('Original','Fitted')

10 Comments

I could be wrong here but he said he wants to "generate an equation from a 3d surface" and when you showed him how to create the surface, he said "I knew how to do that." So he's starting with the surface, not the equation . So what I think he wants to do is to start with some arbitrary surface - not one that was generated by some equation that's known already . And then he wants to get some kind of model equation from the surface. He wants to go from surface to equation, NOT equation to surface. The equation he gave was just an example . What he really wants is to start with the surface, and not knowing that equation in advance, somehow derive the equation. For example if you had an image of a big floodlight illuminating a surface and so the surface was a big hump, to derive the equation of a 2D quadratic or Gaussian, or whatever, that would describe the surface. "A", correct me if I'm wrong.
First of all, thanks to you both for the help.
I am essentially trying to create one new equation which describes three different equations which only come into play at certain ranges of y. The surface is just a way to visualize all of this (I thought it would be the only way to do what I'm trying to do). If there is a way which bypasses the 'surface', then so be it.
But basically I have three equations: Eq1, Eq2, and Eq3.
For values of y < 3, I want Eq1 to be the 'active' one. For values > 3 < 5, I want Eq2 to be the 'active' one. For values > 5 < 10, I want Eq3 to be the 'active' one.
I can easily program this in Matlab using what Rahman has suggested. However, I want to come up with ONE equation which would describe the above three equations, so that in real-world application, that ONE equation can be used to come up with the answer rather than having to rely on heavy if-statements in a program like Matlab.
Does this make sense? I am more than happy to clarify further. Thanks again.
In general, that's not possible (as Roger clairvoyantly said long ago up above in his comment) because of possible discontinuities at the "break / dividing" points. You'd need to do it piecewise with a function that knows how to handle the different ranges.
What if... instead of those 'breaks'... there was just a vertical displacement/transition from one graph to the other?
Same answer - there's a discontinuity that can't be perfectly modeled unless you do a piecewise function. What is your problem/issue with doing a piecewise function?
I suppose I could do that. I just wanted a simpler, single-equation solution.
Sure, if you want to do a regression or fit to a model, you could do that. But it won't match up everywhere.
Do you think that's the best option I have? Thanks.
That is exactly what I did in my second answer! The regression model is look like: f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2, and this equation can describe your three equation of interest, at least in this case.
You can use higher order of regression polynomial, but what I see for this data set, higher order will be redundant since in this case at least, second degree is enough as this gives R-squared value of approximately 99%.
No, I don't think that doing a regression/fit is your best option. I think that using the original piecewise function is your best option. It's not hard or complicated, and it's more accurate. In fact it's easier because you don't have to do any kind of fit or regression at all. Why would you want to take the harder, less accurate approach????

Sign in to comment.

Categories

Asked:

A
A
on 3 Jan 2015

Commented:

on 4 Jan 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!