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 CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by