Click here to Skip to main content
15,664,272 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to code the logistic growth in python where time can take float numbers.

dN/dt = rN(1-N/K)
N is the population
r is the growth rate
K is the carrying capacity
t is the time

The time in my code can take only integers but it must definitely take float numbers as later I will use it for the logistic growth of the tumours and I couldn't fix it.

I will really appreciate all the help possible even if it is not related to my code.

What I have tried:

import numpy as np #for basic numerical computation
import matplotlib.pyplot as plt #for visualization
import csv
import random
#val = int(input("Enter your value: ")) 
r = .25 # growth rate / year ,|(birth/death rate)
K = 100 # carrying capacity
#t = 40 # time
num = np.zeros(t+1)#always add +1 for your upper limit #array([ 0.,  0.,  0.,  0.,  0.]) which means that it creates 41 arrays filled with zeroes 
num[0] = 1
for i in range(t):
    num[i+1] = num[i]+r*num[i]*(1-num[i]/K)
    row= (i+1,'\t\t',format(num[i], '.8f'))
    print (row)
    with open('plot.csv', 'a') as csvFile:
            writer = csv.writer(csvFile, delimiter=' ')
plt.plot(range(t+1),num, 'b')
plt.ylabel(' Cell Number')
plt.title('Logistic Growth')
plt.axvline(np.argmax(np.diff(num)),  color = 'k'  )
Updated 11-Jul-19 1:59am
Richard MacCutchan 11-Jul-19 7:40am    
If you want help then please explain exactly what your problem is.
Z.BOZ 11-Jul-19 7:50am    
Hi Richard,
I have to input float numbers to t(time)for example 0.01. The problem is this code can't take float values. I am a beginner in python and can't figure out how to fix it.
Richard MacCutchan 11-Jul-19 7:59am    
Sorry, I do not understand. Python works fine with integers or floating point numbers.

1 solution

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900