Demo entry 6761933

hw4

   

Submitted by anonymous on Oct 08, 2018 at 21:37
Language: Matlab. Code size: 2.0 kB.

close all; clear;clc;

X0 = 100;           % Number of individuals in the first generation
max_t = 50;         % Time limit of the simulation
max_types = 1000;   % a safely large number (matlab reallocates size if this


mu = 1.05;          % Poisson process rate
q = 10^-2;          % Rate of mutation

X=zeros(max_types, max_t);              % preallocate vector for population size
numb_of_types=zeros(1, max_t);          % preallocate vector for number of types

X(1:X0,1) = 1;                          % Start with X0 women, one of each type
numb_of_types(1)=X0;                    %initialize first generation
numb_of_extinct_types=zeros(1,max_t);

for n=2:max_t
    numb_of_types(n)=numb_of_types(n-1);
    
    for type = 1:numb_of_types(n-1)
        for i = 1:X(type,n-1)
            daughters = poissrnd(mu,1,1);   % draw number of daughters
            mutation = binornd(1,q,1,1);    % draw mutation indicator
            
            if mutation
                numb_of_types(n) = numb_of_types(n)+1;
                X(numb_of_types(n),n) = daughters;
            else
                X(type,n) = X(type,n) + daughters;
            end 
        end 
        
        
        if X(type,n)== 0
            numb_of_extinct_types(n)=numb_of_extinct_types(n)+1;
        end 
    end 
end 
figure;
plot(1:max_t, X);
xlabel('generation');
ylabel('number of women of each type');grid on;

figure;
stairs(1:max_t, X');
xlabel('generation');
ylabel('number of women of each type');grid on;

figure;
stairs(1:max_t, [numb_of_types;numb_of_extinct_types]');
xlabel('generation');
ylabel('number of women of each type');grid on;
axis([0 50 0 numb_of_types(end)])
legend('number of types','number of extinct types','Location','Best')

figure
bar(1:numb_of_types(end), X(1:numb_of_types(end),max_t),'r')
xlabel('types')
ylabel('number of women of each type')
title('Histogram of the final number of women of each type')

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).