problem using hold on in a for loop

조회 수: 4 (최근 30일)
Rebekah Kuehl
Rebekah Kuehl 2019년 7월 16일
댓글: David K. 2019년 7월 16일
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