Advertisement
Guest User

CO67_2

a guest
Jan 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.53 KB | None | 0 0
  1. function [ pop_time_data ] = simulate_decay ( init_pop , half_lives , t)
  2. % Author: Sneha Ramshanker  , Date: ??/??/????
  3. % Simulating decay of D elements given the initial population, half lives, and the time grid.
  4. % Input/output vectors in this function are sorted with the first element decays to the second
  5. % element, then to the third element, and so on until the last element.
  6. % So the last element is the stable element.
  7. % Input:
  8. % ? init_pop: D?elements vector of the initial population of each element, sorted
  9. % such that the stable element is at the last.
  10. % ? half_lives: (D?1)?elements vector of half lives of each element.
  11. % ? t: N?elements vector of the time grid where the population is returned.
  12. %
  13. % Output:
  14. % ? pop_time_data: (N x D) matrix that contains the population of each element in every time step.
  15. %
  16. % Constraints:
  17. % ? The half life of an element can be very small, so in this case, the function must
  18. % make an appropriate adjustment/approximation to handle this.
  19. %
  20. % Example use:
  21. % >> init_pop = [1000, 0, 0, 0, 0];
  22. % >> half_lives = [2.5e5, 8e4, 1.62e3, 4/365];
  23. % >> t = linspace(0, 1e6, 1000);
  24. % >> pop = simulate_decay(init_pop, half_lives, t);
  25. % >> plot(t, pop(:,1)); % plot the population of the 1st element vs time
  26.  
  27. %Defining initial constraints
  28.     dt = t(2)-t(1);                     %time interval for the integration
  29.     n_points = length(t);               %number of time intervals
  30.     pop_time_data = [init_pop];         %initializing output vector
  31.     pop = init_pop;                     %Initializing current population vector
  32.     N = sum(init_pop);                  %Total number of atoms
  33.    
  34.     half_lives = half_lives(1:3);       %accounting for stable conditions
  35.  
  36.     l =  log(2)./half_lives;            %conversion to activity
  37.    
  38.     for i = 1:n_points-1                %iterating through time intervals
  39.         for j = 1: length(l)            %iterating through the different particles
  40.  
  41.             %prob = l*dt;
  42.             prob = (1-exp(-l*dt));      %setting probability of decay
  43.  
  44.             for k = 1:pop(j)            % Iterating through each atom in each particle type
  45.                 if prob(j)>rand()       % Applying monte-carlo integration
  46.                     if pop(j)>0
  47.                         pop(j) = pop(j)-1;
  48.                         pop(j+1) = pop(j+1)+1;
  49.                     end
  50.                 end
  51.             end
  52.         end
  53.         pop(4) = N - (pop(1)+pop(2)+pop(3));
  54.         pop_time_data = [pop_time_data;pop]; %Storing population values
  55.     end
  56.  
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement