提问人:Dee 提问时间:11/17/2023 更新时间:11/17/2023 访问量:27
带箭袋的斜率图
Slope Plot with Quiver
问:
我正在尝试绘制给定微分方程组的斜率场图。 在本例中,我的主要目标是绘制 S 与 I1 或 I2 的斜率场。我想要 也可以有一个固定的箭头长度,但我不知道该怎么做。
我尝试了以下方法:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
# Initial Conditions (Suscetible, type 1 infectives, type 2 infectives, and recovered)
(S0, I1_0, I2_0, R0) = (40, 2, 8, 0)
# Constants (infection rate for type 1 infectives, infection rate for type 2 infectives,
# percentage of infectives that are type 1, and recovery rate for both infective classes)
(r1, r2, beta, gamma) = (0.02, 0.03, 0.8, 0.95)
N = S0 + I1_0 + I2_0 + R0 # Initial population
# System of Differential Equations
def model(y, t, beta, gamma):
dSdt = -(r2*I1 + r2*I2) * S
dI1dt = beta * (r2*I1 + r2*I2) * S - (gamma * I1)
dI2dt = (1 - beta) * (r2*I1 + r2*I2) * S - (gamma * I2)
dRdt = gamma * (I1 + I2)
S, I1, I2, R = y
return dSdt, dI1dt, dI2dt, dRdt
# Number of days
t = np.linspace(0, 30, 365)
# Initial conditions for integration
y0 = S0, I1_0, I2_0, R0
# Integrate
sol = odeint(model, y0, t, args=(beta, gamma))
S, I1, I2, R = sol.T
# Slope plot
x = np.arange(0, N, 0.1)
y = np.arange(0, N, 0.1)
X, Y = np.meshgrid(x, y)
f = -(r2*I1 + r2*I2) * S
fig, ax = plt.subplots()
ax.quiver(X, Y, sol[:, 1], f)
plt.show()
但是,当然,代码不会生成任何类型的情节。任何帮助将不胜感激。
答: 暂无答案
评论