How to add regression line equation to a plot?

179 views (last 30 days)
clear all
close all
clc
%linear regression
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];
n=length(x);
xy=x.*y; %calculate the of x*y
x2=x.^2; %calculate the of x square
xplus=sum(x); %calculate the sum of x
yplus=sum(y); %calculate the sum of y
xyplus=sum(xy); %calculate the sum of x*y
x2plus=sum(x2); %calculate the sum of x square
xm=xplus/n; %calculate the mean of x
ym=yplus/n; %calculate the mean of y
b=(n*xyplus-xplus*yplus)/(n*x2plus-xplus*xplus); %calculate the b
a=ym-b*xm; %calculate the a
y1=a+b*x;
figure(1)
scatter(x,y) %make a graph of data point for i=1:n %loop to calculate summition
hold on
plot(x,y1)
hold off
dy='Total World CO2 Emission (tonnes)';
ylab = sprintf('%s ',dy);
ylabel(ylab,'FontSize',12)
dx='Year';
xlab = sprintf('%s ',dx);
xlabel(xlab,'FontSize',12)
fl='Total World CO2 Emission x Year';
tit = sprintf('%s %s ',fl);
title(tit,'FontSize',12)
Hi I want to show the line equation on plot, if its not possible at least I want to find it. What is the code for it?

Accepted Answer

Star Strider
Star Strider on 11 Dec 2021
One approach —
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];
DM = [x(:), ones(size(x(:)))];
B = DM \ y(:)
B = 2×1
0.0716 -139.0386
y1 = DM * B;
figure(1)
scatter(x,y) %make a graph of data point for i=1:n %loop to calculate summition
hold on
plot(x,y1)
hold off
dy='Total World CO2 Emission (tonnes)';
ylab = sprintf('%s ',dy);
ylabel(ylab,'FontSize',12)
dx='Year';
xlab = sprintf('%s ',dx);
xlabel(xlab,'FontSize',12)
fl='Total World CO2 Emission x Year';
tit = sprintf('%s %s ',fl);
title(tit,'FontSize',12)
text(2000, 4.8, sprintf('CO_2 = %.3f \\cdot Year%.3f',B))
y1998 = [1998 1] * B
y1998 = 3.9523
The mldivide,\ function,operator does the linear least-squares regression (single or multivariate) in one operation. To get the accompanying statistics, use the regress or fitlm functions.
.
  12 Comments
berk26092
berk26092 on 14 Dec 2021
Thank you very much sir! I really appreciated your help!!
Star Strider
Star Strider on 14 Dec 2021
As always,. my pleasure!
I note that this request also appeared in a separate Question about the same time as posted here. (I was sleeping, being UCT-7 here in the Southwest U.S.) I prefer my format, since it corresponds to the MATLAB convention with the coefficients in descending powers of the independent variable, even though that is a bit more difficult to code.
.

Sign in to comment.

More Answers (1)

Mitchell Thurston
Mitchell Thurston on 11 Dec 2021
the most simple way I know of would be to add it to the legend
you can add this to the last line of your code
legend({'Data Points',sprintf('Linear Fit y = %.2f + %.2f*x',a,b}, 'Location','best')
You can also use this built-in for line fitting
p = polyfit(x,y,1);
b = p(1); a = p(2);

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!