How to get polynomial equation of polyfit?

69 views (last 30 days)
Hello. I have the following code and the data. I need to find the equation of the polynomial and write it on the graph. Also, I will be changing the x and y datas so, it needs to be specific for a set of datas. Thank you for your help! It will be appreciated!!
clc;
clear;
y = [4.0432,4.1073,4.0899,4.1319,4.2885,4.4305,4.5249,4.6172,4.6962,4.7235,4.5987,4.7927,4.895,4.9079];
x = [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012];
pf = polyfit(x,y,2);%Calculating the corresponding polynomial function
disp('The coefficients of the desired polynomial:');
fprintf('%3.3f\n',pf) %Showing the coefficients of the fitted polynomial function
pv = polyval(pf,x);%Calculating the values of polynomial function at x's
disp('The values of polynomial at given set of x values:');
fprintf('\n %3.3f',pv)
scatter(x,y)
hold on
plot(x,pv)
hold off
  2 Comments
Berk Cem Yamanlar
Berk Cem Yamanlar on 14 Dec 2021
This is the simple linear regression line for my data and there is the equation of the line. I would like to find the equation of second order polynomial and show the equation on the figure as below.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 14 Dec 2021
hello
my suggestion below :
the equation is given by the string eqn
code :
clc;
clear;
y = [4.0432,4.1073,4.0899,4.1319,4.2885,4.4305,4.5249,4.6172,4.6962,4.7235,4.5987,4.7927,4.895,4.9079];
x = [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012];
% Fit a polynomial p of degree "degree" to the (x,y) data:
degree = 2;
p = polyfit(x,y,degree);
% Evaluate the fitted polynomial p and plot:
f = polyval(p,x);
eqn = poly_equation(flip(p)); % polynomial equation (string)
Rsquared = my_Rsquared_coeff(y,f); % correlation coefficient
figure(3);plot(x,y,'o',x,f,'-')
legend('data',eqn)
title(['Data fit - R squared = ' num2str(Rsquared)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Rsquared = my_Rsquared_coeff(data,data_fit)
% R2 correlation coefficient computation
% The total sum of squares
sum_of_squares = sum((data-mean(data)).^2);
% The sum of squares of residuals, also called the residual sum of squares:
sum_of_squares_of_residuals = sum((data-data_fit).^2);
% definition of the coefficient of correlation is
Rsquared = 1 - sum_of_squares_of_residuals/sum_of_squares;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function eqn = poly_equation(a_hat)
eqn = " y = "+a_hat(1);
for i = 2:(length(a_hat))
if sign(a_hat(i))>0
str = " + ";
else
str = " ";
end
if i == 2
eqn = eqn+str+a_hat(i)+"*x";
else
eqn = eqn+str+a_hat(i)+"*x^"+(i-1)+" ";
end
end
eqn = eqn+" ";
end
  4 Comments
Steven Lord
Steven Lord on 14 Dec 2021
fprintf lets you automatically include the sign of the variable if you include the flag + in your format specifier. The leading + in the result is a little awkward looking, but the command itself is quite simple and you could post-process the string to trim a leading + and to replace x^1 and x^0 with x and nothing if desired.
coeffs = [3 -4 5];
powers = 2:-1:0;
s = sprintf('%+dx^%d', [coeffs; powers])
s = '+3x^2-4x^1+5x^0'
if s(1) == '+'
s = s(2:end);
end
s = replace(s, 'x^1', 'x');
s = replace(s, 'x^0', '')
s = '3x^2-4x+5'

Sign in to comment.

More Answers (1)

KSSV
KSSV on 14 Dec 2021
fprintf('%f*Year^2+%f*YEar+%f\n',pf)

Community Treasure Hunt

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

Start Hunting!