{"id":56,"date":"2011-12-15T19:07:11","date_gmt":"2011-12-16T01:07:11","guid":{"rendered":"http:\/\/www.graygoo.net\/blog\/?p=56"},"modified":"2012-07-25T10:42:43","modified_gmt":"2012-07-25T15:42:43","slug":"matlab-bode-and-nyquist-plots","status":"publish","type":"post","link":"http:\/\/www.graygoo.net\/blog\/2011\/12\/matlab-bode-and-nyquist-plots\/","title":{"rendered":"Matlab Bode and Nyquist plots"},"content":{"rendered":"<p>Matlab includes classical control theory analysis functions such as for Bode plots. Bode plots are useful for determining the behavior of linear time-invariant systems in filters and controls. Again, we are working with Matlab&#8217;s tf objects. Let&#8217;s define a third-order filter, and compute the Bode plot:<br \/>\n<code><br \/>\n&gt;&gt; L=tf([10000],[1 111 1110 1000]);<br \/>\n&gt;&gt; bode(L)<br \/>\n<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-58 alignright\" title=\"bode12152011\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/12\/bode12152011-300x225.png\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/12\/bode12152011-300x225.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/12\/bode12152011.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>The Bode plot gives the gain (amplitude) and phase of the filter&#8217;s output compared to the input over the frequency domain. Of interest to feedback systems is the filter&#8217;s gain and phase margins and crossover frequencies. These can be determined by looking at the Bode plot, but naturally, Matlab has a function to plot them for us:<\/p>\n<p><code>&gt;&gt; margin(L)<br \/>\n<\/code><\/p>\n<p>There is also a text-only command to compute these values:<\/p>\n<p><code><br \/>\n&gt;&gt; allmargin(L)<\/code><\/p>\n<p>ans =<\/p>\n<p>GainMargin: 12.2210<br \/>\nGMFrequency: 33.3167<br \/>\nPhaseMargin: 54.9018<br \/>\nPMFrequency: 7.7980<br \/>\nDelayMargin: 0.1229<br \/>\nDMFrequency: 7.7980<br \/>\nStable: 1<\/p>\n<p>One definition of phase margin is the amount of phase that can be tolerated before the filter reaches 180\u00b0\u00a0of phase and becomes unstable in a closed feedback loop. A minimum phase margin of 45\u00b0 is typically considered for stability. From here, we can see that the filter has adequate phase margin. The phase may be related to a time delay by dividing it by the frequency. Let&#8217;s create a time-delayed copy of L, Ld, and set the time delay so that Ld is on the verge of instability.<\/p>\n<p><code><br \/>\n&gt;&gt; Ld = L;<br \/>\n&gt;&gt; Ld.InputDelay=(54.9018*pi\/180)\/7.7980; allmargin(Ld)<\/code><\/p>\n<p>ans =<\/p>\n<p>GainMargin: [1&#215;20 double]<br \/>\nGMFrequency: [1&#215;20 double]<br \/>\nPhaseMargin: 1.5869e-04<br \/>\nPMFrequency: 7.7980<br \/>\nDelayMargin: 3.5518e-07<br \/>\nDMFrequency: 7.7980<br \/>\nStable: 1<\/p>\n<p>Ld&#8217;s phase margin is very near to zero, indicating a marginally stable system. Let&#8217;s nudge it over the edge.<\/p>\n<p><code><br \/>\n&gt;&gt; Ld.InputDelay=(54.9018*pi\/180)\/7.7980+.0001; allmargin(Ld)<br \/>\nans = <\/code><\/p>\n<p>GainMargin: [1&#215;20 double]<br \/>\nGMFrequency: [1&#215;20 double]<br \/>\nPhaseMargin: -0.0445<br \/>\nPMFrequency: 7.7980<br \/>\nDelayMargin: -9.9645e-05<br \/>\nDMFrequency: 7.7980<br \/>\nStable: 0<\/p>\n<p>L is stable and Ld is now marginally unstable. How do we demonstrate this? One way is to plot the step response of the closed feedback loop. <em>Note: I found Matlab needs to convert the tf objects into a state-space model with the ss() function to work with time delays.<!--more--><\/em> L reaches equilibrium after a brief overshoot, whereas Ld oscillates with increasing amplitude. In fact, since Ld is unstable, it will eventually shoot off to infinity, in our ideal world of functions.<\/p>\n<p style=\"text-align: center;\"><code><br \/>\nhold on; step(ss(L)\/(1+ss(L)),10); step(ss(Ld)\/(1+ss(Ld)),10); legend('L','Ld')<br \/>\n<\/code><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/12\/bode12152011-21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-60 aligncenter\" title=\"bode12152011-2\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/12\/bode12152011-21-300x225.png\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/12\/bode12152011-21-300x225.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/12\/bode12152011-21.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matlab includes classical control theory analysis functions such as for Bode plots. Bode plots are useful for determining the behavior of linear time-invariant systems in filters and controls. Again, we are working with Matlab&#8217;s tf objects. Let&#8217;s define a third-order &hellip; <a href=\"http:\/\/www.graygoo.net\/blog\/2011\/12\/matlab-bode-and-nyquist-plots\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-ugoo","tag-matlab"],"_links":{"self":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- WP Super Cache is installed but broken. The constant WPCACHEHOME must be set in the file wp-config.php and point at the WP Super Cache plugin directory. -->