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

1 view (last 30 days)
Wyatt Leuck on 9 Dec 2021
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 CommentsShowHide 1 older comment
Michael Van de Graaff on 10 Dec 2021
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 on 9 Dec 2021
Edited: Michael Van de Graaff on 9 Dec 2021
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 CommentShowHide None
Wyatt Leuck on 9 Dec 2021
Hey thanks so much! I really appreciated the comments and pointers you added in. Much help clearing the path!

### More Answers (1)

David Hill on 9 Dec 2021
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;
##### 1 CommentShowHide None
Wyatt Leuck on 9 Dec 2021
Awesome! Thanks for the assist!

R2021b

### Community Treasure Hunt

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

Start Hunting!