How to generate Best fit second order polynomial equation from MATLAB for given data
Show older comments
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
Accepted Answer
More Answers (1)
Dan Mathotaarchchi
on 31 May 2017
0 votes
5 Comments
Star Strider
on 31 May 2017
My pleasure.
This will save the quadratic equation to the ‘Eqn’ variable:
Eqn = sprintf('y = %.2f*x^2%+.2f*x%+.2f', b)
and this will print the equation on the plot:
text(6.5, 260, sprintf('y = %.2f\\cdotx^2%+.2f\\cdotx%+.2f', b))
Change the numeric format descriptors to display the precision you want. See the documentation for sprintf for details.
Dan Mathotaarchchi
on 1 Jun 2017
Star Strider
on 1 Jun 2017
I would do the regression on each column (or row). This will require a loop, because polyfit (and polyval) only operate on vectors. You will get 5 equations, one for each value of inlet pressure, or 3 equations, one for each value of temperature, depending on what you are doing and the result you want.
Dan Mathotaarchchi
on 1 Jun 2017
Star Strider
on 1 Jun 2017
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.
Categories
Find more on Get Started with Curve Fitting Toolbox 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!