Attempting to plot a function with a for loop, but nothing is working.

2 ビュー (過去 30 日間)
This is my first time posting a question...I'm getting stuck on this portion of my code.
1: I can't seem to figure out why my function won't plot at all.
2: I can't figure out how to plot multiple lines on one plot.
figure(1);
for i = 1:2:50 %for loop to run for 2miles of radius, or 1 hr of time
con = (kilotons * (1.2*10^8))/(pi*i^2); %Formula for calculating the concentration at each interval
plot(i,con) %plots hours/radius against concentration
ylabel('Concentration'), xlabel('Time (hr)') %Labels plot
title('Decreasing Concentration By Hour')
grid on
hold on %Supposed to hold each plot as the for loop moves through its sequence
end
hold off
  2 件のコメント
Wyatt Leuck
Wyatt Leuck 2021 年 12 月 9 日
For the future: I ended up using this...
figure(1);
for ii = 1:10
xdata = 1:2:50;
ydata= (kk*1.2*10^8)./(pi*xdata);
plot(xdata,ydata)
ylabel('Concentration'), xlabel('Time (hr)')
title('Decreasing Concentration By Hour')
grid on
hold on
end
Michael Van de Graaff
Michael Van de Graaff 2021 年 12 月 10 日
Presumably you meant
ydata= (ii*1.2*10^8)./(pi*xdata);
since kk hasn't been defined?
but yeah, that plots pretty lines. you can modify as follows:
figure(1);
for ii = 1:10
xdata = 1:2:50;
ydata= (ii*1.2*10^8)./(pi*xdata);
plot(xdata,ydata,'displayname',['ii = ',num2str(ii)]) % this will tell a legend object what to label, handy
ylabel('Concentration'), xlabel('Time (hr)')
title('Decreasing Concentration By Hour')
grid on
hold on
end
legend % without calling legend it won't show up.
Do keep in mind that legend is much slower than everything else in matlab.

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

採用された回答

Michael Van de Graaff
Michael Van de Graaff 2021 年 12 月 9 日
編集済み: Michael Van de Graaff 2021 年 12 月 9 日
figure(1);
kilotons = 2; %i needed to add this
% i always use double indices ii,jj,kk, helps avoid confusing and also is
% i=sqrt(-1)?
for ii = 1:2:50 %for loop to run for 2miles of radius, or 1 hr of time
con(ii) = (kilotons * (1.2*10^8))/(pi*ii^2); %Formula for calculating the concentration at each interval
plot(ii,con(ii),'o') % the plotmarker 'o' is key if you INSIST on doing it this way %plots hours/radius against concentration
ylabel('Concentration'), xlabel('Time (hr)') %Labels plot
title('Decreasing Concentration By Hour')
grid on
hold on %Supposed to hold each plot as the for loop moves through its sequence
end
hold off
The important part is using plot works best with vectors, and it defaults to line plots, so your data ARE there, but the plot markers aren't visible.
I would do it this way:
figure(1);
kilotons = 2;
xdata = 1:2:50;
ydata= (kilotons*1.2*10^8)./(pi*xdata)
plot(xdata,ydata)
ylabel('Concentration'), xlabel('Time (hr)') %Labels plot
title('Decreasing Concentration By Hour')
grid on
  1 件のコメント
Wyatt Leuck
Wyatt Leuck 2021 年 12 月 9 日
Hey thanks so much! I really appreciated the comments and pointers you added in. Much help clearing the path!

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

その他の回答 (1 件)

David Hill
David Hill 2021 年 12 月 9 日
kilotons=input('kilotons of blast');
i=1:2:50;
con = (kilotons * (1.2*10^8))./(pi*i.^2);
plot(i,con);
ylabel('Concentration'), xlabel('Time (hr)');
title('Decreasing Concentration By Hour');
grid on;

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by