我试图解决这个问题:的Python:无法解决使用odeint与符号函数微分方程
其中U是
这里:
s=c*e(t)+e_dot(t)
and
e(t)=theta(t)-thetad(t)
和
e_dot(t)=theta_dot(t)-thetad_dot(t)
其中thetad(希塔需要的话)= SIN(T) - 待跟踪即信号!我试着第一次使用odeint,它在t = 0.4后给出的误差是θ(上面的微分方程的解)平稳地下降到了0, 0并留下。然而,当我试图将mxstep提高到5000000时,我可以得到一些正确的图,直到t = 4.3s。
我想得到一个纯正弦曲线图。那就是我希望theta(上述微分方程的解)跟踪时间即sin(t)。但在t = 4.3秒后无法准确地追踪该点。在这段时间之后,θ(上述微分方程的解)简单地下降到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()
的ODE功能应具有中等规模的连续导数最多的顺序的方法。什么“中等大小”取决于步长算法的内部。即使是阶梯函数的一阶导数,也是一个狄拉克三角洲峰值,较高的一个更差...... – LutzL
非常感谢! Wrzlprmft你救了我很多麻烦!我正在查看integrate.ode中的Vode选项。然而,tanh在这里帮助了我很多! –
另一个变体是'def sign(s):s * = 1e3;返回s /(1 + np.abs(s));'。同时尝试移动sigmoid函数以使其不对称,使其不对称,或者其他小的偏移量。只有在这种变化下解的相似性,你才能希望在给定的时间间隔内存在某种广义的解。 – LutzL