フィルターのクリア

How to plot for a loop

2 ビュー (過去 30 日間)
Ender Rencuzogullari
Ender Rencuzogullari 2018 年 5 月 7 日
Hi Everybody, I need to get plot of this loop, below, in an one plot. However, I am having an empty plot. How can I solve this problem? I mean that I need delta_V value for every value of 1000 on x axis. Thanks in advance.
Edit: It is surprising that when I run this script a few more times, It worked. So, why did that happen? Can you please explain?
E = 15000;
delta_p = 1000;
Vo = 7.854 ;
for i = 1:10
delta_V(i) = delta_p * Vo / E;
Vo = Vo - delta_V(i);
end
figure
plot(1000:1000:10000,delta_V)
title('Compression Ratio')
xlabel('Pressure Change')
ylabel('Volume Change')
hold on

採用された回答

sloppydisk
sloppydisk 2018 年 5 月 7 日
編集済み: sloppydisk 2018 年 5 月 7 日
This should work just fine, although I would suggest using
figure(1)
instead of
figure
, which makes a new figure on every run. This is probably what caused you to see an empty figure at some point.
Also for good practice preallocate delta_V before the for loop:
V = zeros(1, 10);
And you don't need the hold on in this case, because you aren't plotting multiple lines in one figure.
  3 件のコメント
sloppydisk
sloppydisk 2018 年 5 月 7 日
編集済み: sloppydisk 2018 年 5 月 7 日
Probably because this is a very small loop so it is not really necessary, but I would still do it to get used to it. Here the time gained by not dynamically expanding the array is apparently smaller than the time lost by preallocating. Try increasing the number of elements to 10000, then you should see it being a lot faster.
Ender Rencuzogullari
Ender Rencuzogullari 2018 年 5 月 7 日
I got it now, thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by