No errors in my code but the program stays busy forever?

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
  end
j1 = 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

Answers (1)

%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 Comments

i did reduce the number to the hundreds but it still didnt work it kept running forever its actually causing my matlab to crash everytime i run
i tested another code made by a colleague with the same numbers and their code gives results in less than 10 seconds
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
yea but NA = NA_O shouldnt that fix it?

"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.

so how should i fix it should i remove this part

% number of remaining atoms
NA  = NA_0;        
NA1 = NA1_0;
NA2 = NA2_0;
NB  = NB_0;

and stick to the initial values of NA_0, NA1_0... etc?

Sign in to comment.

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Asked:

on 19 Apr 2018

Commented:

on 21 Apr 2018

Community Treasure Hunt

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

Start Hunting!