[英]How can I plot the margins in a python bode plot?
我想用 python 控制系統庫繪制系統的波特圖。 這相當容易。 問題是邊緣的情節。 繪制相位裕度沒有問題。 但是我怎樣才能繪制增益裕度呢?
到目前為止,這是我的代碼的一部分:
import control as cn
%matplotlib notebook
import matplotlib.pyplot as plt
Ks=2
T1=5
T2=0.3
T3=0.1
Gs=cn.tf(Ks,[T1*T2*T3, T1*T2+T1*T3+T2*T3, T1+T2+T3, 1])
Vr=1
Tn=1
plt.close()
R=cn.tf([Vr*Tn, Vr],[1, 0])
L=Gs*R
gm, pm, wg, wp = cn.margin(L)
_,_,_ = cn.bode(L,dB=True)
plt.axvline(x = wp,color='r')
不是最優雅的解決方案,但嘿它對我有用。
###Import modules
import numpy as np
import control as ctl
import matplotlib.pyplot as plt
##Functions
def plot_margins(sys):
mag,phase,omega = ctl.bode(sys,dB=True,Plot=False)
magdB = 20*np.log10(mag)
phase_deg = phase*180.0/np.pi
Gm,Pm,Wcg,Wcp = ctl.margin(sys)
GmdB = 20*np.log10(Gm)
##Plot Gain and Phase
f,(ax1,ax2) = plt.subplots(2,1)
ax1.semilogx(omega,magdB)
ax1.grid(which="both")
ax1.set_xlabel('Frequency (rad/s)')
ax1.set_ylabel('Magnitude (dB)')
ax2.semilogx(omega,phase_deg)
ax2.grid(which="both")
ax2.set_xlabel('Frequency (rad/s)')
ax2.set_ylabel('Phase (deg)')
ax1.set_title('Gm = '+str(np.round(GmdB,2))+' dB (at '+str(np.round(Wcg,2))+' rad/s), Pm = '+str(np.round(Pm,2))+' deg (at '+str(np.round(Wcp,2))+' rad/s)')
###Plot the zero dB line
ax1.plot(omega,0*omega,'k--',lineWidth=2)
###Plot the -180 deg lin
ax2.plot(omega,-180+0*omega,'k--',lineWidth=2)
##Plot the vertical line from -180 to 0 at Wcg
ax2.plot([Wcg,Wcg],[-180,0],'r--',lineWidth=2)
##Plot the vertical line from -180+Pm to 0 at Wcp
ax2.plot([Wcp,Wcp],[-180+Pm,0],'g--',lineWidth=2)
##Plot the vertical line from min(magdB) to 0-GmdB at Wcg
ax1.plot([Wcg,Wcg],[np.min(magdB),0-GmdB],'r--',lineWidth=2)
##Plot the vertical line from min(magdB) to 0db at Wcp
ax1.plot([Wcp,Wcp],[np.min(magdB),0],'g--',lineWidth=2)
return Gm,Pm,Wcg,Wcp
#%%%Actuator Dynamics
G = ctl.tf([1],[1,2,1,0])
Gm,Pm,Wcg,Wcp=plot_margins(G)
plt.show()
在0.8版本開始control
,在bode_plot
功能(也別名為bode
)有一個選項,以情節的利潤。
import control
sys = control.tf([1], [1, 1]) # example transfer function
control.bode_plot(sys, margins=True)
# or
control.bode(sys, margins=True)
但是,這種方法並不總是看起來像我想要的,尤其是具有多個傳遞函數時。 如果你想要更多的控制(雙關語意),你可以做同樣的事情到@蒙特卡洛的回答,而是直接由所產生的陰謀策划邊緣bode_plot
/ bode
命令。
import control
import matplotlib as plt
sys = control.tf([1], [1, 1]) # example transfer function
control.bode(sys, dB=True)
gm, pm, wg, wp = control.margin(sys)
fig = plt.gcf() # Get a handle to the current figure (the bode plot)
mag_axis, phase_axis = fig.axes # Get the magnitude and phase subplots
mag_axis.plot([wg, wg], [0, control.mag2db(gm)])
phase_axis.plot([wp, wp], [-180, pm])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.