我試圖解決這個問題:的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