# Generate an equation from a 3d surface

47 visualizzazioni (ultimi 30 giorni)
A il 3 Gen 2015
Commentato: Image Analyst il 4 Gen 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 CommentiMostra NessunoNascondi Nessuno
Roger Stafford il 3 Gen 2015
Modificato: Roger Stafford il 3 Gen 2015
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.
A il 3 Gen 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?

Accedi per commentare.

### Risposte (3)

Shoaibur Rahman il 3 Gen 2015
Modificato: Shoaibur Rahman il 3 Gen 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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
A il 3 Gen 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.

Accedi per commentare.

Image Analyst il 3 Gen 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 CommentiMostra NessunoNascondi Nessuno
A il 3 Gen 2015
Thank you! Would I be able to do it for a 3d surface such as the one above?
Image Analyst il 3 Gen 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.

Accedi per commentare.

Shoaibur Rahman il 4 Gen 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 CommentiMostra 8 commenti meno recentiNascondi 8 commenti meno recenti
Shoaibur Rahman il 4 Gen 2015
Modificato: Shoaibur Rahman il 4 Gen 2015
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%.
Image Analyst il 4 Gen 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????

Accedi per commentare.

### Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by