

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Our aim is to illustrate a basic strategy for how to write a new MATLAB program from scratch. A .m file with the code below is available as cell_reproduction_review_code_1.m from our website.
Let’s say we’re given this problem – similar to the cell reproduction model discussed on the first day of class.
Implement the rule n(t) = 4 p n(t − 1) (1 − n(t − 1)/K)
starting from an initial population size of n(1) = 1000, and simulating for 100 timesteps (or generations). Then, compute a list of all times t where n(t) > 1300. Use p = 0.75, K = 2000.
Where do I start? In my first lines of code, I define all of the parameters.
p=.75; %fraction of cells that survive each reproduction generation_end=100; %predict up to this generation in the future K=2000; %reproduction capacity
OK, now I have to think about what it is that I am going to evolve over time. I am going to predefine a “dummy” list or matrix of numbers representing the variable that I am going to simulate. That dummy list is often just filled in with zeros. Then I will go through and fill it in later. Here, I want a list of values of n at timesteps t = 1, ..., 100. So, let’s just define a list of 100 zeros to use as my dummy list. There are a couple of ways to do this:
nlist=zeros(1,generation_end);
or
nlist=0*(1:generation_end) ;
Alright. Now I need to set my initial conditions. What’s the first value of nlist? That’s
nlist(1)=1000;
Now I need to loop over time. At each timestep, I implement my dynamical rule.
for t=2:generation_end nlist(t)=4nlist(t-1)p*(1-nlist(t-1)/K) ; end
So, I went through and filled in the dummy zero values of nlist with their real values.
Now, I need to make a new list – of generations t when nlist(t)>30. Let’s call this new list list_of_times_over_limit. Note that I DON’T KNOW IN ADVANCE HOW LONG THIS LIST IS SUPPOSED TO BE. In this way, the situation is similar to when we are building up a list of dwell times for the markov chain. We will therefore not make a dummy list in advance, but proceed as follows (all three ways give same answer).
Way 1 – keep growing the list via a loop
Here, we have to define an integer-valued number which_entry_to_update.m that tells me which entry in the list I’m supposed to be changing. We start with the first entry. which_entry_to_update= OK, let’s use this:
for t=1:generation_end if nlist(t)> list_of_times_over_limit(which_entry_to_update)=t ; %next I say: update the NEXT entry down the list next time which_entry_to_update=which_entry_to_update+1; end end
Way 2 – keep growing the list via a loop, version 2
Here, we start with an empty list, and keep appending to it. list_of_times_over_limit=[]
for t=1:generation_end if nlist(t)> list_of_times_over_limit=[list_of_times_over_limit t]; end end