The original logistic function that represents the change in population size per year is delta_N=r*(1-N/K)*N. I’m trying to write a new function that takes in r, K, N0 (The initial value of N, which stands for initial population size), and t (the number of years) and return the population N after t years. Below is my code. it does work but when I plug in t=0 (which is supposed to mean there is no change in time), it stills return some N that is different from N0 (which means my code is likely wrong?). Do people have any idea as to how I can fix my code?
def equation2(r,K,N0,t):
i=0
N=N0
while i<=t:
Nf=N+(r*(1-N/K)*N)
N=Nf
i=i+1
return Nf
>Solution :
I can see 2 potential problems with your code:
- Plugging in t = 0 for your function will still go inside your
whileloop once because it will check for0<=0Which istrue. You need to remove the=sign from your condition - After fixing this,
Nfwill be undefined when passing in t = 0. This also needs to be dealt with accordingly
def equation2(r,K,N0,t):
i=0
N=N0
while i<t:
N += (r*(1-N/K)*N)
i=i+1
return N