"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 leastsquares sense using the backslash operator (\).
> > > Take a look at
> > > http://www.mathworks.de/de/help/matlab/math/systemsoflinearequations.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
