0

I'm trying to plot a straight line from a point in x to different values of t, thereby making a line in a for loop. But I see no lines generated in my figure in MATLAB

Following is my code:

 t=linspace(0,8,11)
xs=(1.+t).^0.5
x0=xs./(1.+t)
m=size(t)
n=max(m)
hold on
for k=1:n
plot(x0(k),t(1:k),'-')
 hold on
end

Thanks

4
  • 1
    Possible duplicate of Plotting graph using for loop in MatLab Commented Dec 2, 2018 at 6:14
  • @Cris I do not think this is a duplicate. Your dupe target says why the plot does not show anything, but it does not solve OPs problem. According to his top line he wants to plot a line from x to t. Commented Dec 2, 2018 at 8:43
  • @Rahul neither x or t are points, they are values. What is their other coordinate? Commented Dec 2, 2018 at 8:47
  • @NickyMattsson: I probably misunderstood the problem. Re-reading, I don’t understand at all what the question is. Commented Dec 2, 2018 at 16:36

2 Answers 2

1

You do not need the loop to perform the plot.

plot(x0,t,'-')

Will work just fine! Unless you were attempting to plot points...use scatter() for that:

scatter(x0,t)

plot() and scatter() (and most of Matlab's functions) are meant to be used with vectors, which can take some time to get used to if you are used to traditional programming languages. Just as you didn't need a loop to create the vector x0, you don't need a loop to use plot().

Sign up to request clarification or add additional context in comments.

Comments

0

You are adding one point in Y axis along a line in X Axis use this code

t=linspace(0,8,11)
xs=(1.+t).^0.5
x0=xs./(1.+t)
m=size(t)
n=max(m)
hold on
for k=1:n
plot(x0(1:k),t(1:k),'-')
hold on
end

for more fun and see exactly how for is performed use this for loop

for k=1:n
pause('on')
plot(x0(1:k),t(1:k),'-')
hold on
pause(2)
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.