No errors in my code but the program stays busy forever?
Afficher commentaires plus anciens
Hello
I'm trying to write a program that simulates a radioactive decay. Given a certain number of initial atoms, I need to know how many atoms of each isotope is left and then plot it vs. time. However something seems to be wrong with my first loop and I don't know what it is since MATLAB wont show me any error statements. here is the question
% initial number of atoms NA_0 = 20000000; NA1_0 = 10000000; NA2_0 = 20000000; NB_0 = 1000000;
% number of remaining atoms NA = NA_0; NA1 = NA1_0; NA2 = NA2_0; NB = NB_0;
% decay constants LA = 0.7; LA1 = 0.7; LA2 = 0.3;
% number of A atoms at i-1 unit of time i = 1; A_atoms(i)= NA;
% number of A1 atoms at j-1 unit of time j = 1; A1_atoms(j)= NA1;
% number of A2 atoms at k-1 unit of time k = 1; A2_atoms(k) = NA2;
% number of B atoms at m-1 unit of time m = 1; B_atoms(m) = NB;
while NA_0 > 0;
for i = 0:NA;
x = rand; if x < LA
NA = NA - 1; if x < 0.2
A1_atoms(j) = A1_atoms(j) + 1; else if (x>0.2) && (x<0.7)
A2_atoms(k) = A2_atoms(k)+ 1;
end end
end i = i + 1;
A_atoms(i) = NA; j = j + 1;
A1_atoms(j) = NA1; k = k + 1;
A2_atoms(k) = NA2;
end
endj1 = 1; k1 = 1;
while NA1 > 0 || NA2 > 0
if NA1 > 0
N1 = NA1; for k1 = 1:N1
y = rand; if y <= LA1
NA1 = NLA1 - 1;
end end
j1 = j1 + 1;
A1_atoms(j1)= NA1;end
if NA2 > 0
N2 = NA2; for k1 = 1:N2
z = rand;
if z <= L2
NA2 = NA2 - 1;
end
end k1 = k1 + 1;
A2_atoms(k1)= NA2;end
% number of B atoms after adding the decayed atoms from A1 and A2
NB = NB +(N1-NA1)+(N2-NA2);
B_atoms(m) = NB;end
Réponses (1)
KALYAN ACHARJYA
le 19 Avr 2018
Modifié(e) : KALYAN ACHARJYA
le 19 Avr 2018
%Yes it's having millions of numbers of loops % Testing Purpose reduces the values of following and check, is it still run forever? %All for loops having the following cycle NA_0 = 20000000; NA1_0 = 10000000; NA2_0 = 20000000; NB_0 = 1000000;
6 commentaires
reem alhouli
le 19 Avr 2018
reem alhouli
le 19 Avr 2018
Dennis
le 19 Avr 2018
There are a few odd things about your code, however you are never leaving your while loop since you never change NA_0 inside it...hence your code will run forever.
You might want to use elseif instead of else if
reem alhouli
le 21 Avr 2018
Stephen23
le 21 Avr 2018
"but NA = NA_O shouldnt that fix it?"
No. Your while loop condition tests NA_0. You do not change NA_0 inside the loop. Ergo the loop condition never changes: once the loop starts it does not stop. NA is irrelevant.
reem alhouli
le 21 Avr 2018
Catégories
En savoir plus sur Particle & Nuclear Physics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!