How do I get Matlab to plot a line with a constant slope?
David
2010-12-01 07:47:38 UTC
I have a project to do in matlab where I'm creating a graph with multiple lines. Some of those lines have a constant y value, but when it plots it, it just shows dots for those with constant slopes. The other lines work fine. I want those dots to connect so it looks nice. How do I do that?
For an example code this is what I'm talking about:
x=[1:10]
y=5
plot(x,y)
Three answers:
2016-05-31 12:06:16 UTC
Subscript indices must either be real positive integers or logicals. You cannot use 0 as a subscript. You should have the same number of elements for x and y. In your first case x has 10 elements but y is a scalar (one value only). Use this, for example: x = linspace(0, 10, 10) y = linspace(5, 5, 10) plot(x,y) Type ' help linspace ' on your command window to see a description on how linspace works.
2010-12-01 17:52:27 UTC
Subscript indices must either be real positive integers or logicals. You cannot use 0 as a subscript.
You should have the same number of elements for x and y. In your first case x has 10 elements but y is a scalar (one value only).
Use this, for example:
x = linspace(0, 10, 10)
y = linspace(5, 5, 10)
plot(x,y)
Type ' help linspace ' on your command window to see a description on how linspace works.
2010-12-01 08:08:22 UTC
MODIFIED:
lets consider the graph y=5x+6 and to plot it from x=0 to 10.Here's how we do it:-
x=1:11;
y=5*(x-1)+6;
n=0:10;
plot(y)
it's nothing but the shifting of axes.You take values for x from 1 to 11.However the value of y varies with (x-1) not with x.Hence when x=1,y=5*0+6=6.
and so on.
and while you are plotting the graph you are simply shifting the starting point from 1 to 0.
to know more follow me at
matrixmathtricks.blogspot.com
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.