problem using hold on in a for loop

4 次查看(过去 30 天)
For my project, I am calculating the half-life of an organism based on radioactive decay. My code is below. The problem I am experiencing is that he figure only has one point plotted. If someone could explain how to fix this so my entire function would be plotted, that would be greatly appreciated.
function Project
clc, clear
P=menu ('Please choose the element', 'Pm-145', 'Po-209', 'Ra-226', 'Ac-227', 'Am-243', 'Bk-247', 'Cf-251', 'C-14');
switch P %P=element found in sample through AMS
case 1 % Promethium-145
t=17.4;
case 2 % Polonium-209
t=102;
case 3 % Radium-226
t=1600;
case 4 % Actinium-227
t=21.77;
case 5 % Americium-243
t=7370;
case 6 % Berkelium-247
t=1380;
case 7 % Californium-251
t=898;
case 8 % Carbon-14
t=5730;
end
N=input ('Please enter the remaining percentage of substance: \n');
while N<=0 || N>=100 %N=percentage of element found through AMS
fprintf ('Incorrect input. Please try again. \n');
N=input ('Please enter the remaining percentage of substance: \n');
end
clc
age=(((log((N)/(100)))/(-0.693))*t);
fprintf ('The age of your organism is shown below: \n');
fprintf (' %s years',age);
n=length(N);
for i=1:n
plot(age(i), N(i), '*', 'MarkerSiz', 10)
xlim([0 inf])
ylim([0 100])
pause(.01)
hold all
xlabel(strcat('Years passed = ', num2str(age(i))))
ylabel(strcat('Percentage remaining =', num2str(N(i))))
title ('Radioactice Decay')
grid on
end
end
Thanks!
  6 个评论
Rebekah Kuehl
Rebekah Kuehl 2019-7-16
The input requested is a percentage, so the while loop is used to get an appropriate number. For example, if the user inputs a negative number or one larger than 100, the program will not run because those are not acceptable percentages
Adam
Adam 2019-7-16
Ah, ok. So N is scalar, as others have mentioned, for all subsequent analysis.

请先登录,再进行评论。

回答(1 个)

the cyclist
the cyclist 2019-7-16
Your variable N is a single value -- a scalar. (It is an array of length 1.) You then define n to be the length of that array. So n = 1.
Then, you plot in a for loop that goes from 1 to n. But that's from 1 to 1. So, you get one point.
  4 个评论
Rebekah Kuehl
Rebekah Kuehl 2019-7-16
Where would I place the vector code that you posted?
David K.
David K. 2019-7-16
Right before you calculate age.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by