How to plot values without overwriting them?

My code looks like the following:
function result = eulerPROJECT(V, angle, h)
x=0
y=0
t=0;
while t<5
x=x+h*V*cos(angle)
y=y+h*V*sin(angle)-h*9.8*t
t=t+0.2;
plot(x,y)
end
I'm trying to plot y against x for each t but the problem is it only plots the last values of y and x. How can I change this so that each pair of y and x gets plotted?

1 件のコメント

Stephen23
Stephen23 2017 年 11 月 19 日
編集済み: Stephen23 2017 年 11 月 19 日
@ Samuel Alff: the code you show contains no graphics/plotting functions, the function returns none of its internal variables, and you do not show us how you are trying to use this code to plot anything. How can we help you?

サインインしてコメントする。

回答 (2 件)

Anna M
Anna M 2017 年 11 月 19 日
編集済み: Anna M 2017 年 11 月 19 日

0 投票

To plot multiple vectors in one figure, use
hold on
after the first plot. Then just write more plot-orders the way you need them, and use
hold off
after the last one. MatLab should then plot all the vectors in one figure. Example for the code:
x = 1:1:10;
y1 = x;
y2 = x + 1;
y3 = 3*x;
plot(x,y1)
hold on
plot(x,y2)
[...]
plot(x,y3)
hold off
(See the Documentation of plot for details.) But even so, I have to agree, that your question is hardly understandable when we don't even see what the plot-order you are using looks like...
EDIT: plot(x,y1, x,y2, x,y3) should work as well and is shorter.

1 件のコメント

Walter Roberson
Walter Roberson 2017 年 11 月 19 日
You are plotting single points at a time. You will not be able to see any result unless you give a marker, such as
plot(x, y1, '*')

サインインしてコメントする。

SBA
SBA 2017 年 11 月 19 日

0 投票

The above edited code is what pretty much what I have been trying, just putting a plot(x,y) at the end so the problem was that it only plotted the final x and y values because with each iteration I overwrite them.

5 件のコメント

Anna M
Anna M 2017 年 11 月 19 日
Then you should try the longer version, by inserting 'hold on' after your 'plot(x,y)' and 'hold off' after 'end'.
SBA
SBA 2017 年 11 月 19 日
function result = eulerPROJECT(V, angle, h)
x=0
y=0
t=0;
while t<5
x=x+h*V*cos(angle)
y=y+h*V*sin(angle)-h*9.8*t
t=t+0.2;
plot(x,y)
hold on
end
hold off
Like this? It still doesn't give me a plot but this time the scale of the axes is what I would expect. I should clarify that before I assumed it was plotting the final x and y values based on the numbers on my x and y axes. However I only ever see an empty graph space I never actually see any plotted points?
Anna M
Anna M 2017 年 11 月 19 日
編集済み: Anna M 2017 年 11 月 19 日
Did you see Walter's comment on my first suggestion? For plotting single points, you need to specify a symbol, e.g.
plot(x,y,'.') or
plot(x,y,'*')
But the use of 'hold' seems to be correct. ;)
SBA
SBA 2017 年 11 月 19 日
Brilliant! Thank you very much that worked great :)
Anna M
Anna M 2017 年 11 月 19 日
You're very welcome! Glad that I (we) could help :)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

タグが未入力です。

質問済み:

SBA
2017 年 11 月 19 日

コメント済み:

2017 年 11 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by