非线性系统numpy的阶段画象

时间:2016-10-30 22:09:40

标签: python numpy scipy

我试图使用numpy绘制非线性系统的相位图,但是 odeint给了我以下警告并打印出unrial情节。

  

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()

1 个答案:

答案 0 :(得分:0)

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

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