plotting a linear equation

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
Then:
Y= b0-b1*X
How do I plot this?

답변 (4개)

per isakson
per isakson 2012년 7월 25일

3 개 추천

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

댓글 수: 1

Just a note that as of R2016a, ezplot is no longer recommended. If anyone is reading this now, the code would still work or you can update to:
b0 = 3;
b1 = 4;
f = @(x) b0-b1*x;
fplot( f,[0,5])
See fplot for more info.

댓글을 달려면 로그인하십시오.

Elizabeth
Elizabeth 2012년 7월 25일
편집: DGM 2024년 11월 12일

3 개 추천

Or:
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:
b0=3; b1=4;
x= linspace(xmin,xmax, n); % Adapt n for resolution of graph
y= b0-b1*x;
plot(x,y)

댓글 수: 4

Elizabeth
Elizabeth 2012년 7월 25일
To plot more than one graph on the same plot use the 'hold on' command.
Naga Sai
Naga Sai 2017년 5월 22일
편집: Naga Sai 2017년 5월 22일
xmin,xmax,n undefined variables
Nicholas Copsey
Nicholas Copsey 2020년 3월 28일
xmin, xmax, and n are things you can change in the code for various views of the graph
To clarify:
% curve parameters
b0 = 3; % y-intercept
b1 = 4; % (negative) slope
% define x
xmin = 0; % pick these as needed
xmax = 1;
n = 100; % number of samples
x = linspace(xmin,xmax, n);
% calculate y from x
y = b0 - b1*x;
plot(x,y)

댓글을 달려면 로그인하십시오.

Ben Le
Ben Le 2015년 12월 9일
편집: DGM 2024년 11월 12일

1 개 추천

b0 = 3;
b1 = 4;
% You can choose any range of x. In this case I chose x is from -50 to +50
x = -50:50;
y = b0 - b1*x;
plot(x,y)
Andy
Andy 2012년 7월 25일

0 개 추천

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.

카테고리

태그

질문:

2012년 7월 25일

편집:

DGM
2024년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by