Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这两个ODE求解程序不';不匹配?_Python_Python 3.x_Ode_Odeint - Fatal编程技术网

Python 为什么这两个ODE求解程序不';不匹配?

Python 为什么这两个ODE求解程序不';不匹配?,python,python-3.x,ode,odeint,Python,Python 3.x,Ode,Odeint,我不明白为什么这个代码(): 不会产生与我编写的代码相同的结果: import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt beta = 10. / (40 * 8 * 24) gamma = 3. / (15 * 24) def flu(y, t): S = y[0] P = y[1] R = y[2] S = - beta * S * P

我不明白为什么这个代码():

不会产生与我编写的代码相同的结果:

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


beta = 10. / (40 * 8 * 24)
gamma = 3. / (15 * 24)


def flu(y, t):
    S = y[0]
    P = y[1]
    R = y[2]

    S = - beta * S * P
    P = beta * S * P - gamma * P
    R = gamma * P

    return [S, P, R]


C_I = [50, 1, 0]
t = np.linspace(0, 1000, 1000)

y = odeint(flu, C_I, t)

S = y[:, 0]
P = y[:, 1]
R = y[:, 2]

fig, ax = plt.subplots()
ax.plot(t, S, 'b--', label='S')
ax.plot(t, P, 'r--', label='I')
ax.plot(t, R, 'g--', label='R')
legend = ax.legend(loc='upper right', shadow=True, fontsize='x-large')
legend.get_frame().set_facecolor('#FFFCCC')
plt.show()
为了避免混淆,我使用了p而不是I

使用odeint求解的方程应与上述参考链接中提供的方程相同。如果我使用的方程是正确的,我确信它们是正确的,我不知道错误在哪里


感谢您的帮助

您设置了
S=y[0]
然后设置了
S=-beta*S*p
。这将覆盖y[0]!!!
P
R

试试这个:

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


beta = 10. / (40 * 8 * 24)
gamma = 3. / (15 * 24)


def flu(y, t):
    S = y[0]
    P = y[1]
    R = y[2]

    dS = - beta * S * P
    dP = beta * S * P - gamma * P
    dR = gamma * P

    return [dS, dP, dR]


C_I = [50, 1, 0]
t = np.linspace(0, 1000, 1000)

y = odeint(flu, C_I, t)

S = y[:, 0]
P = y[:, 1]
R = y[:, 2]

fig, ax = plt.subplots()
ax.plot(t, S, 'b--', label='S')
ax.plot(t, P, 'r--', label='I')
ax.plot(t, R, 'g--', label='R')
legend = ax.legend(loc='upper right', shadow=True, fontsize='x-large')
legend.get_frame().set_facecolor('#FFFCCC')
plt.show()

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


beta = 10. / (40 * 8 * 24)
gamma = 3. / (15 * 24)


def flu(y, t):
    S = y[0]
    P = y[1]
    R = y[2]

    dS = - beta * S * P
    dP = beta * S * P - gamma * P
    dR = gamma * P

    return [dS, dP, dR]


C_I = [50, 1, 0]
t = np.linspace(0, 1000, 1000)

y = odeint(flu, C_I, t)

S = y[:, 0]
P = y[:, 1]
R = y[:, 2]

fig, ax = plt.subplots()
ax.plot(t, S, 'b--', label='S')
ax.plot(t, P, 'r--', label='I')
ax.plot(t, R, 'g--', label='R')
legend = ax.legend(loc='upper right', shadow=True, fontsize='x-large')
legend.get_frame().set_facecolor('#FFFCCC')
plt.show()