Real case ODE using solve_ivp

43 Views Asked by At

I'm new to ODEs (Differential equations) and need to model y' = ax

This is the story : Let's assume that a population increases by 100% per unit of time ( rate = 1) For example, if at time 1, the population is 1, at time 2 it will be 2 persons, and at time 3 , there will be 4 persons, at time 4, there will be 8 persons. (Constant increase)

The speed of change of the population is therefore proportional to the quantity of population.

How can I model this using scipy solve_ivp and draw a good line ? Thank you

This is my try out :

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def model(y,t):
    k = 1 # rate  100%
    dydt = k * y
    return dydt

# Initial condition
y0 = 1

#  Interval , max point, number of points
t = np.linspace(1,3,3)

# Solve Diff eq
y = odeint(model,y0,t)

# Draw
plt.plot(t,y)
plt.xlabel('time')
plt.ylabel('y(t) nb persons population')
plt.show()

Result is wrong :

enter image description here

Could you please point me in the right direction ? Thank you

This is my second try out, result is wrong in my opinion, Should I understand another thing about odes ? :

import numpy as np
from scipy.integrate import odeint
from matplotlib import pyplot as plt
k = 1.0 ## k is a rate meaning 100%
def f(y,x): ## This function returns derivatives
    return k*y
xs = np.arange(1,4,1) ## I need growth population infos from 1 to the third hour.
yO = 1 ## At the beggining, population is one person
ys = odeint(f,yO,xs)
print(xs)
print(ys)

plt.plot(xs,ys)
plt.plot(xs,ys,'b*')
plt.xlabel('time in hours')
plt.ylabel('population')
plt.title('population grows 100% per hour')
plt.show()

enter image description here

I think It should be solved using geometric series, but I am trying to understand ODES ...for now, I fail.

Here is the link I used : https://www.epythonguru.com/2020/09/ODE-Growth-model-in-Python.html

0

There are 0 best solutions below