{"id":114,"date":"2019-05-15T21:40:47","date_gmt":"2019-05-16T05:40:47","guid":{"rendered":"https:\/\/linearcontrol.info\/fundamentals\/?p=114"},"modified":"2019-06-03T07:23:14","modified_gmt":"2019-06-03T15:23:14","slug":"simulating-linear-systems-with-matlabs-lsim","status":"publish","type":"post","link":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/2019\/05\/15\/simulating-linear-systems-with-matlabs-lsim\/","title":{"rendered":"Simulating linear systems with Matlab&#8217;s lsim"},"content":{"rendered":"\n<p>In this post we will go over a couple of ways to simulate linear systems in Matlab.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>You have the transfer-function, say<\/p>\n\n\n\n<p>$$G(s) = \\frac{1}{s + 1},$$<\/p>\n\n\n\n<p>and you would like to calculate the response to some input using Matlab. The transfer-function can be represented in Matlab using <code>tf<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">G = tf([1], [1 1]);<\/pre>\n\n\n\n<p>where the two parameters are vectors with the coefficients of the numerator and denominator of the transfer-function.<\/p>\n\n\n\n<p>If your input is bounded and otherwise well behaved you can use&nbsp;<code>lsim<\/code>&nbsp;to do it. For example, if&nbsp;your input $u$&nbsp;is a sinusoid of amplitude $A$ and frequency $\\omega=2 \\pi f$ then<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">A = 1;\nf = 10;\nT = 1;\nnpoints = 10*T*f;\nt = linspace(0, T, npoints);\nu = A * cos(2*pi*f*t); <\/pre>\n\n\n\n<p>will construct vectors $u$ and $t$ representing your input samples.  Note that <code>npoints<\/code>&nbsp;has to be chosen so that&nbsp;$u$ is <em>well represented<\/em> by its samples. The output $y$ can be calculated using <code>lsim<\/code> as in<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">y = lsim(G, u, t);<\/pre>\n\n\n\n<p>Some types of inputs happen so often that matlab will provide specialized functions for simulation. For example, if $u$ is a constant of amplitude $A$ you might as well just use<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">y = A * step(G);<\/pre>\n\n\n\n<p><code>step<\/code> by itself calculates the response to a unit step input. See the <a href=\"https:\/\/linearcontrol.info\/book\/livescript\/Chapter4.html\">livescritps for Chapter 4<\/a> for more examples of usage of the function <code>step<\/code>.<\/p>\n\n\n\n<p>Another useful function is <code>impulse<\/code>, which calculates the unit impulse response. I will comment on that function on a separate note in connection with initial conditions. Indeed, both <code>lsim<\/code> and <code>step<\/code>, when used as above, assume zero initial conditions.<\/p>\n\n\n\n<p>You can easily use&nbsp;<code>lsim<\/code>&nbsp;to simulate the response to multiple inputs as well. For example, you might have calculated closed-loop transfer-functions&nbsp;$H$&nbsp;and&nbsp;$D$&nbsp;such that<\/p>\n\n\n\n<p>$$y = H \\bar{y} + D w, \\qquad H = \\frac{G K}{1 + G K}, \\qquad D = \\frac{G}{1 + G K} $$<\/p>\n\n\n\n<p>using <code>feedback<\/code> as in<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">K = 1;\nH = feedback(K*G,1);\nD = feedback(G,K); <\/pre>\n\n\n\n<p>I will comment on the use of  <code>feedback<\/code> for calculating closed-loop transfer-functions on another post. In this case<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yBar = ones(size(t));\nw = ones(size(t));\ny = lsim([H, D], [yBar; w], t);<\/pre>\n\n\n\n<p>will calculate the combined response where <code>yBar<\/code> and <code>w<\/code> are vectors with samples of the corresponding inputs. You can also&nbsp;calculate multiple outputs. For example<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">S = feedback(1,G*K);  \nye = lsim([H; S],&nbsp;yBar, t);<\/pre>\n\n\n\n<p>will return an array <code>ye<\/code> containing both the output signals $e$ and $y$ corresponding to<\/p>\n\n\n\n<p>$$\\begin{pmatrix} y \\\\ e \\end{pmatrix} = \\begin{bmatrix} H \\\\ S \\end{bmatrix} \\bar{y}, \\qquad S = \\frac{1}{1 + G K}$$<\/p>\n\n\n\n<p>Of course you can do multiple-inputs and multiple-outputs by combining these two ideas.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will go over a couple of ways to simulate linear systems in Matlab.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[9],"tags":[51,52,10,11],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/114"}],"collection":[{"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":17,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":489,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions\/489"}],"wp:attachment":[{"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}