Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Surface Fitting Equation

Subject: Surface Fitting Equation

From: Chee

Date: 17 Oct, 2013 12:49:37

Message: 1 of 8

Hi everyone,

I'm doing surface fitting in MATLAB. The thing that I'm curious is about how the coefficients are calculated. I'm using 'poly22' fitting, and below is the equation of the fitting:

zVal = p00 + p10 .* x + p01 .* y + p20 .* x.^2 + p11 .* x .* y + p02 .* y.^2;

What sort of equation does MATLAB use to calculate p00,p10,p20,p11,p02 ? Even a simple surface fitting algorithm that shows how the coefficient is helpful, so that I can understand how the coefficients are calculated.

Thank you.

Subject: Surface Fitting Equation

From: Torsten

Date: 18 Oct, 2013 06:41:07

Message: 2 of 8

"Chee" wrote in message <l3omd1$bf1$1@newscl01ah.mathworks.com>...
> Hi everyone,
>
> I'm doing surface fitting in MATLAB. The thing that I'm curious is about how the coefficients are calculated. I'm using 'poly22' fitting, and below is the equation of the fitting:
>
> zVal = p00 + p10 .* x + p01 .* y + p20 .* x.^2 + p11 .* x .* y + p02 .* y.^2;
>
> What sort of equation does MATLAB use to calculate p00,p10,p20,p11,p02 ? Even a simple surface fitting algorithm that shows how the coefficient is helpful, so that I can understand how the coefficients are calculated.
>
> Thank you.

If (x(i),y(i),zval(i)) are your measurement points, the coefficients are determined by minimizing the function
F(p00,p10,p01,p20,p11,p02) = sum_i (zVal(i)-(p00 + p10*x(i) + p01*y(i) + p20*x(i)^2 + p11*x(i)*y(i) + p02*y(i)^2))^2
with respect to p00,p10,p01,p20,p11,p02.
This can be done by solving the overdetermined system of equations
p00 + p10*x(i) + p01*y(i) + p20*x(i)^2 + p11*x(i)*y(i) + p02*y(i)^2 = zVal(i)
in the least-squares sense using the backslash operator (\).
Take a look at
http://www.mathworks.de/de/help/matlab/math/systems-of-linear-equations.html
and follow the link to "Overdetermined Systems".

Best wishes
Torsten.

Subject: Surface Fitting Equation

From: Chee

Date: 21 Oct, 2013 19:46:14

Message: 3 of 8

Thanks for your help, its very useful. However, the value of the coefficients doesn't seem to be similar to the one gotten from the fitting toolbox's fit function. Is it because it uses linear regression? How do I do linear regression in this case? There isn't much paper or equation on 3D linear regression.

Thank you.

Regards,
Chee

"Torsten" wrote in message <l3ql63$g3$1@newscl01ah.mathworks.com>...
> "Chee" wrote in message <l3omd1$bf1$1@newscl01ah.mathworks.com>...
> > Hi everyone,
> >
> > I'm doing surface fitting in MATLAB. The thing that I'm curious is about how the coefficients are calculated. I'm using 'poly22' fitting, and below is the equation of the fitting:
> >
> > zVal = p00 + p10 .* x + p01 .* y + p20 .* x.^2 + p11 .* x .* y + p02 .* y.^2;
> >
> > What sort of equation does MATLAB use to calculate p00,p10,p20,p11,p02 ? Even a simple surface fitting algorithm that shows how the coefficient is helpful, so that I can understand how the coefficients are calculated.
> >
> > Thank you.
>
> If (x(i),y(i),zval(i)) are your measurement points, the coefficients are determined by minimizing the function
> F(p00,p10,p01,p20,p11,p02) = sum_i (zVal(i)-(p00 + p10*x(i) + p01*y(i) + p20*x(i)^2 + p11*x(i)*y(i) + p02*y(i)^2))^2
> with respect to p00,p10,p01,p20,p11,p02.
> This can be done by solving the overdetermined system of equations
> p00 + p10*x(i) + p01*y(i) + p20*x(i)^2 + p11*x(i)*y(i) + p02*y(i)^2 = zVal(i)
> in the least-squares sense using the backslash operator (\).
> Take a look at
> http://www.mathworks.de/de/help/matlab/math/systems-of-linear-equations.html
> and follow the link to "Overdetermined Systems".
>
> Best wishes
> Torsten.

Subject: Surface Fitting Equation

From: John D'Errico

Date: 22 Oct, 2013 03:15:06

Message: 4 of 8

"Chee" wrote in message <l440a6$2c3$1@newscl01ah.mathworks.com>...
> Thanks for your help, its very useful. However, the value of the coefficients doesn't seem to be similar to the one gotten from the fitting toolbox's fit function. Is it because it uses linear regression? How do I do linear regression in this case? There isn't much paper or equation on 3D linear regression.
>

You are kidding, right? That there are no papers on 3D linear
regression?

There are complete textbooks written on the topic. Many in fact.
I'd start with Draper and Smith, the classic I learned from many
years go.

I think your problem stems from the fact that you think 3D
regression models are somehow different from models in other
numbers of dimensions.

John

Subject: Surface Fitting Equation

From: Bruno Luong

Date: 22 Oct, 2013 05:59:08

Message: 5 of 8

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <l44qjq$9cd$1@newscl01ah.mathworks.com>...

>
> You are kidding, right? That there are no papers on 3D linear
> regression?
>

That must be google fault. It can't return enough right papers when typing "3D linear regression".

Bruno

Subject: Surface Fitting Equation

From: Torsten

Date: 22 Oct, 2013 06:32:07

Message: 6 of 8

>
> "Torsten" wrote in message <l3ql63$g3$1@newscl01ah.mathworks.com>...
> > "Chee" wrote in message <l3omd1$bf1$1@newscl01ah.mathworks.com>...
> > > Hi everyone,
> > >
> > > I'm doing surface fitting in MATLAB. The thing that I'm curious is about how the coefficients are calculated. I'm using 'poly22' fitting, and below is the equation of the fitting:
> > >
> > > zVal = p00 + p10 .* x + p01 .* y + p20 .* x.^2 + p11 .* x .* y + p02 .* y.^2;
> > >
> > > What sort of equation does MATLAB use to calculate p00,p10,p20,p11,p02 ? Even a simple surface fitting algorithm that shows how the coefficient is helpful, so that I can understand how the coefficients are calculated.
> > >
> > > Thank you.
> >
> > If (x(i),y(i),zval(i)) are your measurement points, the coefficients are determined by minimizing the function
> > F(p00,p10,p01,p20,p11,p02) = sum_i (zVal(i)-(p00 + p10*x(i) + p01*y(i) + p20*x(i)^2 + p11*x(i)*y(i) + p02*y(i)^2))^2
> > with respect to p00,p10,p01,p20,p11,p02.
> > This can be done by solving the overdetermined system of equations
> > p00 + p10*x(i) + p01*y(i) + p20*x(i)^2 + p11*x(i)*y(i) + p02*y(i)^2 = zVal(i)
> > in the least-squares sense using the backslash operator (\).
> > Take a look at
> > http://www.mathworks.de/de/help/matlab/math/systems-of-linear-equations.html
> > and follow the link to "Overdetermined Systems".
> >
> > Best wishes
> > Torsten.
>>
"Chee" wrote in message <l440a6$2c3$1@newscl01ah.mathworks.com>...
> Thanks for your help, its very useful. However, the value of the coefficients doesn't seem to be similar to the one gotten from the fitting toolbox's fit function. Is it because it uses linear regression? How do I do linear regression in this case? There isn't much paper or equation on 3D linear regression.
>
> Thank you.
>
> Regards,
> Chee

The problem you described is a usual linear regression problem (it's irrelevant that the polynomial is quadratic in x and y - what counts is the linearity in p00,p10,p01,p20,p11,p02), and the solution method I showed you above is the usual method to solve linear regression problems.
Maybe you can show us your code with a small example to see where the differences in the coefficients stem from.

Best wishes
Torsten.

Subject: Surface Fitting Equation

From: Chee

Date: 22 Oct, 2013 13:22:14

Message: 7 of 8

"Torsten" wrote in message <l45657$4eu$1@newscl01ah.mathworks.com>...
> >
> > "Torsten" wrote in message <l3ql63$g3$1@newscl01ah.mathworks.com>...
> > > "Chee" wrote in message <l3omd1$bf1$1@newscl01ah.mathworks.com>...
> > > > Hi everyone,
> > > >
> > > > I'm doing surface fitting in MATLAB. The thing that I'm curious is about how the coefficients are calculated. I'm using 'poly22' fitting, and below is the equation of the fitting:
> > > >
> > > > zVal = p00 + p10 .* x + p01 .* y + p20 .* x.^2 + p11 .* x .* y + p02 .* y.^2;
> > > >
> > > > What sort of equation does MATLAB use to calculate p00,p10,p20,p11,p02 ? Even a simple surface fitting algorithm that shows how the coefficient is helpful, so that I can understand how the coefficients are calculated.
> > > >
> > > > Thank you.
> > >
> > > If (x(i),y(i),zval(i)) are your measurement points, the coefficients are determined by minimizing the function
> > > F(p00,p10,p01,p20,p11,p02) = sum_i (zVal(i)-(p00 + p10*x(i) + p01*y(i) + p20*x(i)^2 + p11*x(i)*y(i) + p02*y(i)^2))^2
> > > with respect to p00,p10,p01,p20,p11,p02.
> > > This can be done by solving the overdetermined system of equations
> > > p00 + p10*x(i) + p01*y(i) + p20*x(i)^2 + p11*x(i)*y(i) + p02*y(i)^2 = zVal(i)
> > > in the least-squares sense using the backslash operator (\).
> > > Take a look at
> > > http://www.mathworks.de/de/help/matlab/math/systems-of-linear-equations.html
> > > and follow the link to "Overdetermined Systems".
> > >
> > > Best wishes
> > > Torsten.
> >>
> "Chee" wrote in message <l440a6$2c3$1@newscl01ah.mathworks.com>...
> > Thanks for your help, its very useful. However, the value of the coefficients doesn't seem to be similar to the one gotten from the fitting toolbox's fit function. Is it because it uses linear regression? How do I do linear regression in this case? There isn't much paper or equation on 3D linear regression.
> >
> > Thank you.
> >
> > Regards,
> > Chee
>
> The problem you described is a usual linear regression problem (it's irrelevant that the polynomial is quadratic in x and y - what counts is the linearity in p00,p10,p01,p20,p11,p02), and the solution method I showed you above is the usual method to solve linear regression problems.
> Maybe you can show us your code with a small example to see where the differences in the coefficients stem from.
>
> Best wishes
> Torsten.

The code that I have done so far is as shown below:

[xData, yData, zData] = prepareSurfaceData( x, y, z );
ft = fittype( 'poly22' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf -Inf -Inf -Inf];
opts.Robust = 'Bisquare';
opts.Upper = [Inf Inf Inf Inf Inf Inf];

% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );

I am trying to understand the maths behind it, which is very new to me. I understand how to use the tool, but not the maths behind the tool in detail. I'm very grateful for the help given so far, just need some directions so that I can work on it.

Best Regards,
Chee

Subject: Surface Fitting Equation

From: dpb

Date: 22 Oct, 2013 13:54:12

Message: 8 of 8

On 10/22/2013 8:22 AM, Chee wrote:
...

> I am trying to understand the maths behind it, which is very new to me.
> I understand how to use the tool, but not the maths behind the tool in
> detail. I'm very grateful for the help given so far, just need some
> directions so that I can work on it.
...

_Applied_Linear_Regression_, Draper and Smith
_Matrix_Algebra_Useful_for_Statstics_, Searle

(both Wiley) are my two standby references -- thorough but not
excessively complex

--

Tags for this Thread

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.

Contact us