Python: 无法使用带符号函数的 odeint 求解微分方程

Python: Unable to solve a differential equation using odeint with signum function

我正在尝试解决这个问题:

U 在哪里

此处:

s=c*e(t)+e_dot(t)

e(t)=theta(t)-thetad(t) 

e_dot(t)=theta_dot(t)-thetad_dot(t)

其中 thetad(theta desired)=sin(t)-- 即要跟踪的信号! thetad_dot=cos(t),J=10,c=0.5,eta=0.5

我首先尝试使用 odeint - 它在 t=0.4 之后给出了错误,即 theta(上述微分方程的解)下降到 0 并此后保持不变。但是,当我尝试将 mxstep 增加到 5000000 时,直到 t=4.3s 之前我都能得到一些正确的图表。

我想得到一个纯正弦曲线图。那就是我想要 theta(上述微分方程的解)来跟踪 thetad,即 sin(t)。但在 t=4.3sec 后无法准确跟踪 thetad。在这段时间之后 theta(上述微分方程的解)简单地下降到 0 并保持在那里。

这是我的代码-

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

c=0.5
eta=0.5
def f(Y,t):
    x=Y[0]
    y=Y[1]
    e=x-np.sin(t)
    de=y-np.cos(t)
    s=c*e+de
    return [y,-c*(de)-np.sin(t)-eta*np.sign(s)]

t=np.linspace(0,10,1000)
y0=[0.5,1.0]
sol=odeint(f,y0,t,mxstep=5000000)
#sol=odeint(f,y0,t)
print(sol)
angle=sol[:,0]
omega=sol[:,1]
error=angle-np.sin(t)
derror=omega-np.cos(t)
plt.subplot(4,2,1)
plt.plot(t,angle)
plt.plot(t,error,'r--')
plt.grid()
plt.subplot(4,2,2)
plt.plot(t,omega)
plt.plot(t,derror,'g--')
plt.grid()
plt.subplot(4,2,3)
plt.plot(angle,omega)
plt.grid()
plt.subplot(4,2,4)
plt.plot(error,derror,'b--')
plt.savefig('signum_graph.eps')
plt.show()

odeint 使用的积分器(可能还有您发现的所有积分器)基于这样的假设:您的导数 (f) 在每个步骤中都是连续的(并且具有连续导数) . signum 函数显然打破了这个要求。这会导致错误估计非常高,无论步长多小,这反过来会导致步长过小和您遇到的问题。

一般有两种解决方法:

  1. 选择您的步长,使标志翻转恰好落在一步上。 (这可能非常乏味。)

  2. 用非常陡峭的 sigmoid 替换 signum 函数,这对于大多数应用程序来说应该没问题甚至更现实。在您的代码中,您可以使用

    sign = lambda x: np.tanh(100*x)
    

    而不是 np.sign。这里的因子 100 控制 sigmoid 的陡度。选择足够高的值以反映您实际需要的行为。如果您选择过高,这可能会对您的运行时间产生负面影响,因为集成器会将步长调整为不必要的小。

在你的情况下,设置一个小的绝对公差似乎也有效,但我不会依赖这个:

sol = odeint(f,y0,t,mxstep=50000,atol=1e-5)