problem using hold on in a for loop

3 ビュー (過去 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 Center および File ExchangeGraphics Object Programming についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by