Help plotting all points for each iteration on one graph.

22 ビュー (過去 30 日間)
Brooks Nelson
Brooks Nelson 2018 年 12 月 8 日
コメント済み: Brooks Nelson 2018 年 12 月 8 日
I am trying to plot the elements in my y array over the iteration of n in the for loop. The calcualations are correct when displayed in command window. How do I plot each element on the graph together? So, I have 4 seperate plotted lines. I only get the last element plotted on screen. Is this because I am not storing each element individually? Any help would be greatly apprciated.
y = [0.5;0.99;1.01;1.3];
n = 1:100;
prob = 0;
for ii=1:length(y)
prob = y(ii).^n
end
plot (n,prob)

採用された回答

madhan ravi
madhan ravi 2018 年 12 月 8 日
編集済み: madhan ravi 2018 年 12 月 8 日
Plotting them as subplots is a better idea because obvously the scales are different.
y = [0.5;0.99;1.01;1.3];
n = 1:100;
c={'m','r','g','b'};
prob=cell(1,length(y)); % preallocation
for ii=1:length(y)
prob{ii} = y(ii).^n;
subplot(4,1,ii)
plot(n,[prob{ii}],c{ii})
end
  2 件のコメント
Brooks Nelson
Brooks Nelson 2018 年 12 月 8 日
Thank for your help. After you said scales were different I though subplot to.
Brooks Nelson
Brooks Nelson 2018 年 12 月 8 日
Just curious.. Is there a way I can create titles for each of the c{ii} iteration without using the plot editor?

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

その他の回答 (1 件)

shariq khan
shariq khan 2018 年 12 月 8 日
編集済み: shariq khan 2018 年 12 月 8 日
hello Brooks,
I try your code, now to plot all the values use plot within for loop like this
clc
clear
y = [0.5;0.99;1.01;1.3];
%prob = 0; % i didnt find it useful to initialize as 0 in this
for i =1:length(y)
prob = y(i).^n;
plot(n,prob)
hold on %this will hold plot until next y value according to pointer i
end
Also, I see that the value of y at 1.3 would be very high or much different than the prob values for other y values which You can check by plotting same way but giving input one by one value. only at y = 1.3, it shows only one plot though other plot are also plotted but are not shown. (try changing or plotting 1.3 at different figure)
Please see attachment also

カテゴリ

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