Generate an equation from a 3d surface

74 views (last 30 days)
A
A on 3 Jan 2015
Commented: Image Analyst on 4 Jan 2015
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
A
A on 3 Jan 2015
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)

Shoaibur Rahman
Shoaibur Rahman on 3 Jan 2015
Edited: Shoaibur Rahman on 3 Jan 2015
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
A
A on 3 Jan 2015
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.


Image Analyst
Image Analyst on 3 Jan 2015
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
Image Analyst
Image Analyst on 3 Jan 2015
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.


Shoaibur Rahman
Shoaibur Rahman on 4 Jan 2015
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
Image Analyst
Image Analyst on 4 Jan 2015
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.

Community Treasure Hunt

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

Start Hunting!