How to add regression line equation to a plot?

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

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

Thank you very much
Sir, I have a question. When I try to use the code you wrote for another x and y variables. I didnt get the equation. Which lines should I change to get the equation for different variables?
The ‘x’ and ‘y’ variables should not matter, unless there is a problem such as NaN or Inf elements.
I would have to see what the vectors are to see what the problem is. Please provide them so I can work with them.
.
y = [0.12, 0.16, 0.20, 0.21, 0.22, 0.21, 0.21, 0.22, 0.24, 0.27, 0.31, 0.33, 0.33, 0.33, 0.33, 0.33, 0.34, 0.37, 0.40, 0.42, 0.44, 0.47, 0.50, 0.52, 0.55, 0.58, 0.61, 0.62, 0.62, 0.63, 0.64, 0.64, 0.65, 0.66, 0.70, 0.74, 0.78, 0.83, 0.87, 0.91, 0.95, 0.98, 1.01];
x = [1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020];
DM = [x(:), ones(size(x(:)))];
B = DM \ y(:)
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
This the code. I just changed the variables. Thank you for giving a time sir.
I needed to make it adaptive with respect to the plot. (My original Answer was designed to solve only that particular problem.)
This should work for any vectors it is supplied with —
y = [0.12, 0.16, 0.20, 0.21, 0.22, 0.21, 0.21, 0.22, 0.24, 0.27, 0.31, 0.33, 0.33, 0.33, 0.33, 0.33, 0.34, 0.37, 0.40, 0.42, 0.44, 0.47, 0.50, 0.52, 0.55, 0.58, 0.61, 0.62, 0.62, 0.63, 0.64, 0.64, 0.65, 0.66, 0.70, 0.74, 0.78, 0.83, 0.87, 0.91, 0.95, 0.98, 1.01];
x = [1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020];
DM = [x(:), ones(size(x(:)))];
B = DM \ y(:)
B = 2×1
0.0193 -38.1710
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(median(xlim)-0.3*diff(xlim), median(ylim)+0.25*diff(ylim), sprintf('CO_2 = %.3f \\cdot Year%+.3f',B))
That should work, and the regereeion equation should be about in the same position on every plot.
Checking by using the original vectors with the new adaptive code —
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(median(xlim)-0.3*diff(xlim), median(ylim)+0.25*diff(ylim), sprintf('CO_2 = %.3f \\cdot Year%+.3f',B))
.
Thank you very much for your help sir. It was very useful.
As always, my pleasure!
.
Sir, do you have a similar method but polynomial regression? I have the below code but still couldnt find the equation
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];
n=input('Enter the desire degree of polynomial: '); %Input the degree of polynomial, you want to fit the given data
pf = polyfit(x,y,n);%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
Try this —
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=input('Enter the desire degree of polynomial: '); %Input the degree of polynomial, you want to fit the given data
n = 3 % Interactive Functions Do Not Work With The Online Run Feature
n = 3
pf = polyfit(x,y,n);%Calculating the corresponding polynomial function
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.
disp('The coefficients of the desired polynomial:');
The coefficients of the desired polynomial:
fprintf('%3.3f\n',pf) %Showing the coefficients of the fitted polynomial function
-0.000 2.180 -4368.672 2918143.027
pv = polyval(pf,x);%Calculating the values of polynomial function at x's
disp('The values of polynomial at given set of x values:');
The values of polynomial at given set of x values:
fprintf('\n %3.3f',pv)
4.012 4.075 4.146 4.224 4.306 4.390 4.474 4.556 4.634 4.705 4.767 4.819 4.857 4.880
scatter(x,y)
hold on
plot(x,pv)
hold off
text1 = sprintf('CO_2 = %.2fx^%d ',[pf(1) n]);
text2 = sprintf('%+.2fx^%d ', [pf(2:end-2); (n-1):-1:2]);
if n == 2
text2 = '';
end
text3 = sprintf('%+.2fx %+.2f',pf(end-1:end));
textall = [text1 text2 text3];
text(median(xlim)-0.4*diff(xlim), median(ylim)+0.4*diff(ylim), textall)
Getting that to work for all polynomial degrees proved to be a bit of a challenge! It is not designed to work with a -degree (linear) fit, however we addressed that earlier. For long polynomials, the equation might stilll run into the plotted values, so it may be necessary to adjust its position in those situations.
.
Thank you very much sir! I really appreciated your help!!
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)

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);

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!