I have 2 independent variables and one dependent variable. I would like to get a polynomial equation and surface plot passing through the data points?

조회 수: 9 (최근 30일)
I would like to get an equation for the 2 independent variables and one dependent variable. I have tried using matlab and the interpolation plot is coming nicely but I would like to get an equation for that plot.
The polynomial equation with 5 th order and 5th degree is not giving reasonable equation. When I tried find the value for intermediate points, it is not giving the correct value. please kindly help me to get correct equation and the plot passing through all the data points as shown in above Figure. The variables data is given in attached "New Text Document".

채택된 답변

Torsten
Torsten 2022년 3월 23일
편집: Torsten 2022년 3월 23일
M = [0.970446735 0.068181818 0.2
0.894959908 0.068181818 0.4
0.779725086 0.068181818 0.6
0.61489118 0.068181818 0.8
0.995074456 0.113636364 0.2
0.973768614 0.113636364 0.4
0.92371134 0.113636364 0.6
0.820504009 0.113636364 0.8
0.994845361 0.159090909 0.2
0.973883162 0.159090909 0.4
0.923940435 0.159090909 0.6
0.821076747 0.159090909 0.8
0.978808706 0.204545455 0.2
0.91672394 0.204545455 0.4
0.804810997 0.204545455 0.6
0.618900344 0.204545455 0.8
0.959908362 0.25 0.2
0.666666667 0.25 0.6
0.387399771 0.25 0.8
0.949484536 0.295454545 0.2
0.813860252 0.295454545 0.4
0.598167239 0.295454545 0.6
0.284421535 0.295454545 0.8
0.952462772 0.340909091 0.2
0.825544101 0.340909091 0.4
0.625544101 0.340909091 0.6
0.338946163 0.340909091 0.8
0.966895762 0.386363636 0.2
0.87628866 0.386363636 0.4
0.728522337 0.386363636 0.6
0.50882016 0.386363636 0.8
0.984994273 0.431818182 0.2
0.940091638 0.431818182 0.4
0.857846506 0.431818182 0.6
0.717983963 0.431818182 0.8
0.997021764 0.477272727 0.2
0.983161512 0.477272727 0.4
0.946391753 0.477272727 0.6
0.861168385 0.477272727 0.8
0.995990836 0.522727273 0.2
0.979381443 0.522727273 0.4
0.937571592 0.522727273 0.6
0.841580756 0.522727273 0.8
0.981786942 0.568181818 0.2
0.927835052 0.568181818 0.4
0.82978236 0.568181818 0.6
0.660824742 0.568181818 0.8
0.960366552 0.613636364 0.2
0.851890034 0.613636364 0.4
0.675257732 0.613636364 0.6
0.410080183 0.613636364 0.8
0.941237113 0.659090909 0.2
0.783963345 0.659090909 0.4
0.535967927 0.659090909 0.6
0.179954181 0.659090909 0.8
0.932531501 0.704545455 0.2
0.750171821 0.704545455 0.4
0.457961054 0.704545455 0.6
0.03069874 0.704545455 0.8
0.937457045 0.75 0.2
0.46792669 0.75 0.6
0.004352806 0.75 0.8
0.953608247 0.795454545 0.2
0.818098511 0.795454545 0.4
0.56884307 0.795454545 0.6
0.128247423 0.795454545 0.8
0.973654066 0.840909091 0.2
0.89255441 0.840909091 0.4
0.728636884 0.840909091 0.6
0.390721649 0.840909091 0.8
0.989805269 0.886363636 0.2
0.955784651 0.886363636 0.4
0.879725086 0.886363636 0.6
0.696449026 0.886363636 0.8
0.997823597 0.931818182 0.2
0.989461627 0.931818182 0.4
0.967353952 0.931818182 0.6
0.906414662 0.931818182 0.8
0.999885452 0.977272727 0.2
0.99908362 0.977272727 0.4
0.996563574 0.977272727 0.6
0.989117984 0.977272727 0.8
0.949140893 0.045454545 0.2
0.829438717 0.045454545 0.4
0.680756014 0.045454545 0.6
0.477090493 0.045454545 0.8
0.986139748 0.090909091 0.2
0.943871707 0.090909091 0.4
0.865864834 0.090909091 0.6
0.736540664 0.090909091 0.8
0.997709049 0.136363636 0.2
0.98327606 0.136363636 0.4
0.942153494 0.136363636 0.6
0.851088202 0.136363636 0.8
0.987972509 0.181818182 0.2
0.949713631 0.181818182 0.4
0.873997709 0.181818182 0.6
0.736311569 0.181818182 0.8
0.968957617 0.227272727 0.2
0.881328751 0.227272727 0.4
0.73115693 0.227272727 0.6
0.493241695 0.227272727 0.8
0.953150057 0.272727273 0.2
0.826116838 0.272727273 0.4
0.620733104 0.272727273 0.6
0.315693013 0.272727273 0.8
0.949255441 0.318181818 0.2
0.813745704 0.318181818 0.4
0.600343643 0.318181818 0.6
0.293699885 0.318181818 0.8
0.958648339 0.363636364 0.2
0.847308133 0.363636364 0.4
0.670103093 0.363636364 0.6
0.413516609 0.363636364 0.8
0.976059565 0.409090909 0.2
0.908705613 0.409090909 0.4
0.793928981 0.409090909 0.6
0.61443299 0.409090909 0.8
0.992325315 0.454545455 0.2
0.966208477 0.454545455 0.4
0.911569301 0.454545455 0.6
0.805154639 0.454545455 0.8
0.998281787 0.5 0.2
0.987857961 0.5 0.4
0.955899198 0.5 0.6
0.874684994 0.5 0.8
0.990263459 0.545454545 0.2
0.958533792 0.545454545 0.4
0.893585338 0.545454545 0.6
0.766552119 0.545454545 0.8
0.971363116 0.590909091 0.2
0.890836197 0.590909091 0.4
0.754295533 0.590909091 0.6
0.537800687 0.590909091 0.8
0.949942726 0.636363636 0.2
0.815005727 0.636363636 0.4
0.600343643 0.636363636 0.6
0.288087056 0.636363636 0.8
0.935166094 0.681818182 0.2
0.761626575 0.681818182 0.4
0.487170676 0.681818182 0.6
0.092325315 0.681818182 0.8
0.933333333 0.727272727 0.2
0.750744559 0.727272727 0.4
0.451088202 0.727272727 0.6
0 0.727272727 0.8
0.944558992 0.772727273 0.2
0.786483391 0.772727273 0.4
0.508018328 0.772727273 0.6
0.046849943 0.772727273 0.8
0.963688431 0.818181818 0.2
0.85486827 0.818181818 0.4
0.645017182 0.818181818 0.6
0.245819015 0.818181818 0.8
0.982588774 0.863636364 0.2
0.927262314 0.863636364 0.4
0.809851088 0.863636364 0.6
0.547651775 0.863636364 0.8
0.994845361 0.909090909 0.2
0.976403207 0.909090909 0.4
0.932531501 0.909090909 0.6
0.819014891 0.909090909 0.8
0.999312715 0.954545455 0.2
0.996219931 0.954545455 0.4
0.987170676 0.954545455 0.6
0.960595647 0.954545455 0.8];
Z = M(:,1);
X = M(:,2);
Y = M(:,3);
p0 = ones(21,1);
P = lsqnonlin(@(p)fun(p,X,Y,Z),p0)
xmin = min(X);
xmax = max(X);
ymin = min(Y);
ymax = max(Y);
xs = linspace(xmin,xmax,20);
ys = linspace(ymin,ymax,20);
[XS,YS] = meshgrid(xs,ys);
ZS = fun_poly(P,XS,YS);
surf(XS,YS,ZS)
end
function res = fun(p,x,y,z)
res = fun_poly(p,x,y) - z;
end
function polynom = fun_poly(p,x,y)
polynom = p(1)+...
p(2)*x+p(3)*y+...
p(4)*x.^2+p(5)*x.*y+p(6)*y.^2+...
p(7)*x.^3+p(8)*x.^2.*y + p(9)*x.*y.^2 + p(10)*y.^3+...
p(11)*x.^4+p(12)*x.^3.*y+p(13)*x.^2.*y.^2+p(14)*x.*y.^3+p(15)*y.^4+...
p(16)*x.^5+p(17)*x.^4.*y+p(18)*x.^3.*y.^2+p(19)*x.^2.*y.^3+...
p(20)*x.*y.^4+p(21)*y.^5 ;
end
  댓글 수: 2
Mani S
Mani S 2022년 3월 24일
Dear Torsten,
I do not know exactly, how to use this code. could you please elaborate?
How to find correct polynomial equation?
Torsten
Torsten 2022년 3월 24일
The polynomial coefficients of the polynomial written in function "fun_poly" (which is the same polynomial that you used in the Curve Fitting Tool) that best fit your data are printed out when you run the program.
That's what I get as output:
p00 = 0.2168
p10 = 14.9962
p01 = 12.2608
p20 = -27.5487
p11 = -112.4505
p02 = -86.0305
p30 = 51.5244
p21 = 72.2586
p12 = 336.8598
p03 = 265.7033
p40 = -40.9577
p31 = -106.8256
p22 = -16.3586
p13 = -473.4161
p04 = -358.9357
p50 = 11.3488
p41 = 51.5825
p32 = 13.1715
p23 = -1.1717
p14 = 236.9484
p05 = 173.7824
and the polynomial is
p(x,y) =
p00 +
p10*x + p01*y +
p20*x^2 + p11*x*y + p02*y^2 +
p30*x^3 + p21*x^2*y + p12*x*y^2 + p02*y^3 +
p40*x^4 + p31*x^3*y + p22*x^2*y^2 + p13*x*y^3 + p04*y^4 +
p50*x^5 + p41*x^4*y + p32*x^3*y^2 + p23*x^2*y^3 + p14*x*y^4 + p05*y^5
But if you read the documentation of the Curve Fitting Tool, you should be able to extract the information you need.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Valeria Alejandra
Valeria Alejandra 2024년 9월 6일
편집: Valeria Alejandra 2024년 9월 6일
FitType = "poly22"; % this value can be changed depending on the order you want for your model
% For example, use "poly43" if you want a 4th order fit for your first variable and
% a 3rd order fit for your second variable
% The max order is 5 ("poly55")
myFit = fit([nf3, ncp], rce, FitType); % this will return a "sfit" object
% nf3, ncp and rce are the vectors from New Text Document.txt
equation = formula(myFit); % will return your equation
coefficients = coeffnames(myFit); % will return the coefficients of your equation
Then if you can evaluate your function using "feval" to obtain an intermediate value (https://www.mathworks.com/help/matlab/ref/feval.html)
val = feval(myFit, [nf3_value, ncp_value])

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by