简体   繁体   中英

How to solve the differential equation?

I want to solve the deferential equation

dydt = r * (Y ** p) * (1 - (Y / K) ** alpha) 

I tried to write the code like:

def func(Y, r, p, K, alpha):
    dydt = r * (Y ** p) * (1 - (Y / K) ** alpha)
    return dydt
t = np.linspace(0, len(df), len(df))

# I used 1 to initialize my parameters ( is there a better way ?)
r = 1; p = 1; K = 1; alpha = 1 

y0 = r,p,K,alpha
ret = odeint(func, y0, t)

but when I try to execute the third block I get

TypeError: func() missing 3 required positional arguments: 'p', 'K', and 'alpha'

However I tried to use ret = odeint(func, y0, t, args=(p,K, alpha)) but this resulted in a three straight lines, when the equation is supposed to return a logistic curve. how can I try to put r in the argument and why I need to specify the arguments? how can I get the final shape (logistic curve)

Note: to understand the parameters: Y represents the cumulative number of cases at time t, r is the growth rateat the early stage, and K is the final epidemic size.∈[0,1]is a parameter that allows the model to capture different growth profiles including the constant incidence (=0), sub-exponential growth (0<<1)and exponential growth (=1).

def func(Y, t, r, p, K, alpha):
    return r * (Y ** p) * (1 - (Y / K) ** alpha)

You must add the t parameter in the ODEINT method.

y0 = 0.5 # Your initial condition.
params = (1, 1, 1, 1) # r, p, K, alpha
sol = odeint(func, y0, t, args=params)

From the source! Scipy ODEINT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM