# problem using hold on in a for loop

25 views (last 30 days)
Rebekah Kuehl on 16 Jul 2019
Commented: David K. on 16 Jul 2019
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;
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))))
grid on
end
end
Thanks!

What are you trying to do in this while loop?
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
Currently it just keeps asking the user to input things and then throws them away by overwriting them next time round. Is it your intention to keep all the N values entered?
In which case you need to concatenate them into an array.
Rebekah Kuehl on 16 Jul 2019
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
Ah, ok. So N is scalar, as others have mentioned, for all subsequent analysis.

the cyclist on 16 Jul 2019
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.

Show 1 older comment
David K. on 16 Jul 2019
After you get the input for N, you could make it a vector with code like
N = 1:N;
Then if you want it to plot starting at the given percentage you could do
N = fliplr(N);
Rebekah Kuehl on 16 Jul 2019
Where would I place the vector code that you posted?
David K. on 16 Jul 2019
Right before you calculate age.