Searching coefficients of surface polinomial (equivalent to polyfit for surfaces) fixed degree

1 view (last 30 days)
I'm looking for a the coefficients a1...a6 of the polinomial function
N1 = a1 + a2*x + a3*y + a4*x^2 + a5*x*y + a6*y^2
that goes through the following points. There is no a7, the degree is fixed.
x=[0 1 0 0.5 0.5 0 ]';
y=[0 0 1 0 0.5 0.5]';
z=[1 0 0 0 0 0 ]';
I know there generally is an infinite number of functions, yet the same applies in this case
a=[0 1 0 0.5 0.5 0];
b=[1 2 3 4 5 6];
N2=polyfit(a,b,2);
N2 =
-7.3333 6.0000 3.3333
and MATLAB is able to work with that. I'm looking for something like polyfit for surfaces.
So far I tried:
N1 = scatteredInterpolant(x,y,z,'natural');
The function seems to work fine, but I cannot show the coefficients of the function, also I cannot influence the degree which is required in my case.
xq=0:0.5:1;
yq=0:0.5:1;
N1 = griddata(x,y,z,xq,yq);
It only returns the interpolated surface, not the function coefficients.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Sep 2015
x=[0 1 0 0.5 0.5 0 ]';
y=[0 0 1 0 0.5 0.5]';
z=[1 0 0 0 0 0 ]';
coeffs_matrix = [ones(length(x),1), x, y, x.^2, x.*y, y.^2];
A_vals = coeffs_matrix \ z;
% coeffs_matrix * A_vals - z %cross-check should be nearly 0
N1 = @(x,y) A_vals(1) + A_vals(2) .* x + A_vals(3) .* y + A_vals(4) .* x.^2 + A_vals(5) .* x .* y + A_vals(6) .* y.^2;
xq = 0:0.5:1;
yq = 0:0.5:1;
[XQ, YQ] = ndgrid(xq, yq);
N2 = N1(XQ, YQ);
surf(XQ, YQ, N2)

More Answers (1)

Image Analyst
Image Analyst on 28 Sep 2015
If you have the Statistics and Machine Learning Toolbox, I believe fitlm() is the function you want. I think you need more points than 6 though. And I'm not sure what your z variable is used for -- it's not in your model.
  1 Comment
Walter Roberson
Walter Roberson on 28 Sep 2015
If you treat z as being the N1 value for particular x, y, then you have 6 points and 6 coefficients, and a least-squares does fine.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!