Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Function about Logistic Equation

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading