How to generate Best fit second order polynomial equation from MATLAB for given data

13 ビュー (過去 30 日間)
Hi all can anybody tell me how to generate above equation for this x and y data using MATLAB ? x,y coordinates are 6,460 10,380 12,300 14,180

採用された回答

Star Strider
Star Strider 2017 年 5 月 30 日
This works:
xy = [6 460; 10 380; 12 300; 14 180]; % Data
b = polyfit(xy(:,1), xy(:,2), 2); % Estimate Parameters For Quadratic Fit
y_fit = polyval(b, xy(:,1)); % Evaluate Fitted Curve
figure(1)
plot(xy(:,1), xy(:,2), 'pg')
hold on
plot(xy(:,1), y_fit, '-r')
hold off
grid
axis([5 15 ylim])

その他の回答 (1 件)

Dan Mathotaarchchi
Dan Mathotaarchchi 2017 年 5 月 31 日
thank you strider , for the answer however i need to generate the equation too, could you help me for that
  5 件のコメント
Dan Mathotaarchchi
Dan Mathotaarchchi 2017 年 6 月 1 日
thank you star. but actually it should generate only one equation like this.
z=T^2(3*p^2+2.45*p+4.00)+ 3*p^2*T^2+1.45*pT+2.00T
actually i went through some sources but couldn't find good method to solve this .
Star Strider
Star Strider 2017 年 6 月 1 日
Here is one approach:
T = [500 650 800];
p = 100:100:500;
z = 10*[80 16 225 285 340; 75 150 210 265 310; 70 137.5 195 250 295];
% % % z=T^2(3*p^2+2.45*p+4.00)+ 3*p^2*T^2+1.45*pT+2.00T
zfcn = @(b,p,T) T.^2.*(b(1).*p.^2 + b(2).*p + b(3)) + b(4).*p.^2.*T.^2 + b(5).*T.*p + b(6).*T;
[pM,TM] = meshgrid(p,T);
b0 = ones(1,6);
Z = @(b) norm(z - zfcn(b,pM,TM)); % Residual Norm Cost Function
[B,resnrm] = fminsearch(Z, b0); % Estimate Parameters
figure(1)
stem3(pM, TM, z)
hold on
mesh(pM, TM, zfcn(B,pM,TM))
hold off
grid on
The fit is not good, however the code runs. I will leave it to you to experiment to get the result you want. There are probably other Optimization Toolbox functions that can give a better result.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by