MATLAB Answers


plotting a linear equation

Asked by Andy
on 25 Jul 2012
Latest activity Edited by Naga Sai
on 22 May 2017

How do I plot a linear equation y=mx+b?

So let's say I have this:

b0= 3 where b0 is the y-intercept b1= 4 where b1 is the x coefficient


Y= b0-b1*X

How do I plot this?


Log in to comment.


4 Answers

Answer by per isakson
on 25 Jul 2012


    b0 = 3;
    b1 = 4;
    f = @(x) b0-b1*x;
    ezplot( f, 0, 5 )


Log in to comment.

Answer by Elizabeth on 25 Jul 2012


By all means, the solution method above will work. However, as your function increases in complexity, that command becomes more and more expensive. Try defining your domain x, then, as a vector:

 MATLAB code
  b0=3; b1=4;
  x= linspace(xmin,xmax, n); % Adapt n for resolution of graph
  y= b0-b1*x;


To plot more than one graph on the same plot use the 'hold on' command.

xmin,xmax,n undefined variables

Log in to comment.

Answer by Andy
on 25 Jul 2012

How do I get that into the same graphs? I have points for my data and the equation above is the linear regression. Ezplot gave me two graphs. :(. I want both the points and the linear equation graphed in one figure.

  1 Comment

use the 'hold on' command

Log in to comment.

Answer by Ben Le
on 9 Dec 2015

bo = 3;

b1 = 4;

% You can choose any range of x. In this case I chose x is from -50 to +50

x = -50:50;



Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today