【发布时间】:2017-03-13 01:32:01
【问题描述】:
我正在尝试使用 numpy 绘制非线性系统的相图,但是
odeint 给我以下警告并打印不真实的情节。
ODEintWarning:在此调用上完成的工作过多(可能是错误的 Dfun 类型)。以 full_output = 1 运行以获取定量信息。 warnings.warn(warning_msg, ODEintWarning)
RuntimeWarning:在 double_scalars 中遇到除以零 x2_d = x1 - 4 * 1/np.tan(x1 + x2)
ODEintWarning:检测到非法输入(内部错误)。以 full_output = 1 运行以获取定量信息。 warnings.warn(warning_msg, ODEintWarning)
据我了解,这是因为在某些情况下值为 np.tan() = 0。我怎样才能克服这一点并获得更准确的情节?
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
def nonlinear(state, t):
# unpack the state vector
x1 = state[0]
x2 = state[1]
# compute state derivatives
x1_d = x2
x2_d = x1 - 4 * 1/np.tan(x1 + x2)
# return the state derivatives
return [x1_d, x2_d]
def generate_initial_states(start, stop, step):
states = []
for i in np.arange(start, stop, step):
states.append([i, i])
states.append([i, -i])
return states
t = np.linspace(0.0, 20, 1000)
initial_states = generate_initial_states(-1.0, 1.0, 0.2)
outputs = []
for state in initial_states:
outputs.append(odeint(nonlinear, state, t))
fig = plt.figure()
for output in outputs:
plt.plot(output[:, 0], output[:, 1], 'r-')
plt.xlabel('x_1')
plt.ylabel('x_2')
plt.title('phase portrait')
plt.grid(True)
plt.show()
【问题讨论】:
-
首先计算分母
t = tan(x1 + x2),制作一个掩码m = t != 0,然后创建一个空的x2_d并使用掩码填充它:x2_d[m] = x1[m] - 4 * 1/t[m]和x2_d[~m] = -numpy.inf或类似的东西。跨度> -
谢谢,它解决了除以零的问题,但 odeint 仍会生成警告 ** ODEintWarning: 在此调用上完成的工作过多(可能是错误的 Dfun 类型)** 并且相图看起来不真实。