September 20, 2017, Wednesday
University of Colorado at Boulder Search A to Z Campus Map University of Colorado at Boulder CU 
Search Links


Leslie2.m

From MathBio

Jump to: navigation, search

Leslie2.m

Leslie2.m has 4 inputs: the maximum distance from the wall before breaking, the birth rate, the probability of getting further from the wall, and the probability of staying in the same place). Using these values, the function creates a leslie matrix and plots the result over time.

function ans = leslie2(max, prob, growth, stationary)

x = 0:1:max;
% P = log(x+1)/log(max+1); %Logarithmic function
P = sqrt(x)/sqrt(max); %Square root function
plot(x,P) %Plot of a probability function
title('Probability of Breakage vs. Position')
xlabel('Distance from wall (x10^-6)m')
ylabel('Probability of Breaking')
if growth + stationary <= 1 && growth>0 && stationary>0
   display('The growth and stationary rates are acceptable')
elseif growth < 0
   display('The growth cannot be negative')
elseif stationary < 0
   display('The stationary growth cannot be negative')
else
   display('growth + stationary cannot add to more than 1')
end
  L = zeros(max); %creating the matrix of size max by max
for i = 1:1:max 
   L(1, i) = prob; %Adding the birth rates on the top row of the matrix
end
for i = 1:1:max-1;
   L(i + 1,i) = (1 - P(i+1))*growth; %Growth (lower diagonal)
   L(i + 1, i + 1) = (1 - P(i+1))*stationary; %Stationary (diagonal)
end
for i = 1:1:max-2;
   L(i + 1, i + 2) = (1 - P(i+1))*(1-(growth + stationary)); %Collapse (upper diagonal)
end
n = zeros(max,1); %Initial population state
for i = 1:1:4
   n(i,1) = 1; %Initial population is <1, 1, 1, 1, 0, 0...>
end
x = zeros(50,1);
t = 1:1:50; %for a 50 second time period
for i = t;
   n = L*n; %calculating the population by multiplying the matrix by the initial population
   x(i) = sum(n);
end
figure(2)
plot(t, x); %Graphing the population trends
xlabel('time');
ylabel('population');
title('population vs. time');
figure(3)
semilogy(t, x); %Log plot of the population trends
xlabel('time');
ylabel('log(population)');
title('population vs. time');
end