4545import scipy as sp
4646import numpy as np
4747import math
48+ import sys as system
4849from .ctrlutil import unwrap
4950from .bdalg import feedback
5051from .margins import stability_margins
@@ -85,6 +86,8 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
8586 If Hz=True the limits are in Hz otherwise in rad/s.
8687 omega_num: int
8788 number of samples
89+ margins : boolean
90+ if True, plot gain and phase margin
8891 *args, **kwargs:
8992 Additional options to matplotlib (color, linestyle, etc)
9093
@@ -219,7 +222,7 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
219222 phase_plot = phase
220223 ax_phase .semilogx (omega_plot , phase_plot , * args , ** kwargs )
221224
222- #show the phase and gain margins in the plot
225+ # Show the phase and gain margins in the plot
223226 if margins :
224227 margin = stability_margins (sys )
225228 gm , pm , Wcg , Wcp = margin [0 ], margin [1 ], margin [3 ], margin [4 ]
@@ -235,33 +238,33 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
235238
236239 if pm != float ('inf' ) and Wcp != float ('nan' ):
237240 if dB :
238- ax_mag .semilogx ([Wcp , Wcp ], [0 , - ( 10 ** 5. )], color = 'k' , linestyle = ':' )
241+ ax_mag .semilogx ([Wcp , Wcp ], [0. , - 1e5 ], color = 'k' , linestyle = ':' )
239242 else :
240- ax_mag .loglog ([Wcp ,Wcp ], [1 , 10. ** - 20. ],color = 'k' ,linestyle = ':' )
243+ ax_mag .loglog ([Wcp ,Wcp ], [1. , 1e-8 ],color = 'k' ,linestyle = ':' )
241244
242245 if deg :
243- ax_phase .semilogx ([Wcp , Wcp ], [( 10 ** 5. ) if phase_limit > 0. else 10. ** - 20. , phase_limit + pm ], color = 'k' , linestyle = ':' )
244- ax_phase .semilogx ([Wcp , Wcp ], [phase_limit + pm , phase_limit ], color = 'k' , linestyle = '- ' )
246+ ax_phase .semilogx ([Wcp , Wcp ], [1e5 , phase_limit + pm ],color = 'k' , linestyle = ':' )
247+ ax_phase .semilogx ([Wcp , Wcp ], [phase_limit + pm , phase_limit ],color = 'k' )
245248 else :
246- ax_phase .semilogx ([Wcp , Wcp ], [10. ** - 20. , - math .pi + math .radians (pm )], color = 'k' , linestyle = ':' )
247- ax_phase .semilogx ([Wcp , Wcp ], [- math .pi + math .radians (pm ), - math .pi ], color = 'k' , linestyle = '- ' )
249+ ax_phase .semilogx ([Wcp , Wcp ], [1e5 , math .radians ( phase_limit ) + math .radians (pm )],color = 'k' , linestyle = ':' )
250+ ax_phase .semilogx ([Wcp , Wcp ], [math .radians ( phase_limit ) + math .radians (pm ), math .radians ( phase_limit )], color = 'k' )
248251
249252 if gm != float ('inf' ) and Wcg != float ('nan' ):
250253 if dB :
251- ax_mag .semilogx ([Wcg , Wcg ], [- 20. * np .log10 (gm ), - ( 10 ** 5. )], color = 'k' , linestyle = ':' )
252- ax_mag .semilogx ([Wcg , Wcg ], [0 ,- 20 * np .log10 (gm )], color = 'k' , linestyle = '- ' )
254+ ax_mag .semilogx ([Wcg , Wcg ], [- 20. * np .log10 (gm ), - 1e5 ], color = 'k' , linestyle = ':' )
255+ ax_mag .semilogx ([Wcg , Wcg ], [0 ,- 20 * np .log10 (gm )],color = 'k' )
253256 else :
254- ax_mag .semilogx ([Wcg , Wcg ], [1. / gm , 10. ** - 20. ], color = 'k' , linestyle = ':' )
255- ax_mag .semilogx ([Wcg , Wcg ], [1. , 1. / gm ], color = 'k' , linestyle = '- ' )
257+ ax_mag .loglog ([Wcg , Wcg ], [1. / gm ,1e-8 ], color = 'k' , linestyle = ':' )
258+ ax_mag .loglog ([Wcg , Wcg ], [1. ,1. / gm ],color = 'k' )
256259
257260 if deg :
258- ax_phase .semilogx ([Wcg , Wcg ], [10. ** - 20. , phase_limit ], color = 'k' , linestyle = ':' )
261+ ax_phase .semilogx ([Wcg , Wcg ], [1e-8 , phase_limit ],color = 'k' , linestyle = ':' )
259262 else :
260- ax_phase .semilogx ([Wcg , Wcg ], [10. ** - 20. , - math .pi ], color = 'k' , linestyle = ':' )
263+ ax_phase .semilogx ([Wcg , Wcg ], [1e-8 , math .radians ( phase_limit )], color = 'k' , linestyle = ':' )
261264
262265 ax_mag .set_ylim (mag_ylim )
263266 ax_phase .set_ylim (phase_ylim )
264- plt .suptitle ('Gm = %.2f %s(at %.2f rad/s), Pm = %.2f %s (at %.2f rad/s)' % (20 * np .log10 (gm ) if dB else gm ,'dB ' if dB else '\b ' ,Wcg ,pm if deg else math .degrees (pm ),'deg' if deg else 'rad' ,Wcp ))
267+ plt .suptitle ('Gm = %.2f %s(at %.2f rad/s), Pm = %.2f %s (at %.2f rad/s)' % (20 * np .log10 (gm ) if dB else gm ,'dB ' if dB else '\b ' ,Wcg ,pm if deg else math .radians (pm ),'deg' if deg else 'rad' ,Wcp ))
265268
266269 if nyquistfrq_plot :
267270 ax_phase .axvline (nyquistfrq_plot , color = pltline [0 ].get_color ())
0 commit comments