【问题标题】:Phase portrait of a nonlinear system numpy非线性系统 numpy 的相图
【发布时间】: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 类型)** 并且相图看起来不真实。

标签: python numpy scipy


【解决方案1】:

你的系统有一个奇点,它确实出现在数值评估中。

物理上合理的解决方案是x1 + x2 = 0 线上开始轨迹。然而,这正是您通过添加起点[i, -i] 所做的事情。您可以添加相空间的其他区域,例如规则间隔的网格(但要避免 x1=-x2)。

【讨论】:

  • 是的,你完全正确。现在我生成initial_conditions,没有[i, -i],从0.1到1,步长为0.1。这解决了除以零的问题。但是以下问题仍然存在 ODEintWarning: E​​xcess work done on this call (也许是错误的 Dfun 类型) 并且相位 protrait 仍然看起来很糟糕。
猜你喜欢
  • 2020-04-03
  • 2019-01-15
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 2013-03-18
  • 2017-03-26
相关资源
最近更新 更多