Home > OS >  Python Function about Logistic Equation
Python Function about Logistic Equation

Time:12-13

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

CodePudding user response:

I can see 2 potential problems with your code:

  1. Plugging in t = 0 for your function will still go inside your while loop once because it will check for 0<=0 Which is true. You need to remove the = sign from your condition
  2. After fixing this, Nf will 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
  • Related