Linear Regression (polyfit) how to show equation and find R?

105 visualizzazioni (ultimi 30 giorni)
Hi there,
Nothing I try seems to work. I have a graph with a linear regression line. I want to know the equation of the line (y=mx+b) and the error R value. Here is my code:
clc
clear all
ActE=[1.672 2.573 2.696 3.300 3.835 4.489]*1e-19;
Freq=[3.60 4.77 5.03 5.72 6.60 7.51]*1e14;
c=polyfit(Freq,ActE,1);
ActE_est=polyval(c,Freq);
plot(Freq,ActE,'*');
grid
title('Frequency vs Activation Energy');
xlabel('Frequency (Hz)');
ylabel('Activation Energy (J)');
hold on
plot(Freq,ActE_est);
legend('Data','Fitted Curve','Location','northwest');

Risposte (1)

Subhadeep Koley
Subhadeep Koley il 1 Nov 2019
Use the code below to find the line equation and fit error (R). This code saves Frequency, Activation Energy, Fit, and Fit Error (R) inside the table T.
warning off; close all;clc;
ActE=[1.672 2.573 2.696 3.300 3.835 4.489]*1e-19;
Freq=[3.60 4.77 5.03 5.72 6.60 7.51]*1e14;
c=polyfit(Freq,ActE,1); % Here 'c' contains the 'm' and 'b'
ActE_est=polyval(c,Freq);
plot(Freq,ActE,'*');
grid
title(['Line Equation is y = ',num2str(c(1)),'*x + ','(',num2str(c(2)),')']);
xlabel('Frequency (Hz)');
ylabel('Activation Energy (J)');
hold on
plot(Freq,ActE_est);
legend('Data','Fitted Curve','Location','northwest');
disp(['Line Equation is y = F(x) = ',num2str(c(1)),'*x + ','(',num2str(c(2)),')']);
% Fit error (R) = ActE-ActE_est
T = table(Freq',ActE',ActE_est',(ActE-ActE_est)','VariableNames',{'Frequency','Activation_Energy','Fit','Fit_Error_R'})
fit.png
Hope this helps!

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by