{"id":1714,"date":"2020-04-29T23:20:09","date_gmt":"2020-04-30T07:20:09","guid":{"rendered":"https:\/\/linearcontrol.info\/fundamentals\/?p=1714"},"modified":"2020-05-01T07:45:09","modified_gmt":"2020-05-01T15:45:09","slug":"block-diagrams-for-simulation","status":"publish","type":"post","link":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/2020\/04\/29\/block-diagrams-for-simulation\/","title":{"rendered":"Block Diagrams for Simulation"},"content":{"rendered":"\n<p>In Chapter 5 we discuss how to produce block diagrams for the simulation of linear and certain nonlinear systems described by ordinary differential equations.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>A graphical representation of such diagrams can be directly used in MATLAB Simulink&#8217;s for exploring the response of linear and nonlinear systems, as discussed in the following short video:<\/p>\n\n\n\n<iframe loading=\"lazy\" id=\"kaltura_player\" src=\"https:\/\/cdnapisec.kaltura.com\/p\/2323111\/sp\/232311100\/embedIframeJs\/uiconf_id\/43259572\/partner_id\/2323111?iframeembed=true&amp;playerId=kaltura_player&amp;entry_id=1_obxl4ao9&amp;flashvars[streamerType]=auto&amp;flashvars[localizationCode]=en_US&amp;flashvars[leadWithHTML5]=true&amp;flashvars[sideBarContainer.plugin]=true&amp;flashvars[sideBarContainer.position]=left&amp;flashvars[sideBarContainer.clickToClose]=true&amp;flashvars[chapters.plugin]=true&amp;flashvars[chapters.layout]=vertical&amp;flashvars[chapters.thumbnailRotator]=false&amp;flashvars[streamSelector.plugin]=true&amp;flashvars[EmbedPlayer.SpinnerTarget]=videoHolder&amp;flashvars[dualScreen.plugin]=true&amp;flashvars[Kaltura.addCrossoriginToIframe]=true&amp;&amp;wid=1_8jesqbf5\" width=\"400\" height=\"285\" allowfullscreen=\"\" webkitallowfullscreen=\"\" mozallowfullscreen=\"\" allow=\"autoplay *; fullscreen *; encrypted-media *\" sandbox=\"allow-forms allow-same-origin allow-scripts allow-top-navigation allow-pointer-lock allow-popups allow-modals allow-orientation-lock allow-popups-to-escape-sandbox allow-presentation allow-top-navigation-by-user-activation\" frameborder=\"0\" title=\"Kaltura Player\"><\/iframe>\n\n\n\n<p>Simulink also provides higher level blocks, such as transfer-functions of arbitrary order, state-space blocks, etc.<\/p>\n\n\n\n<p>Alternatively, if one does not want to be interacting with a graphical interface, one can use one of the available ODE solvers. In Matlab a popular choice is <code>ode45<\/code>, which is an implementation of a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Runge%E2%80%93Kutta_methods\">Runge-Kutta integration method<\/a> with adaptive step size. Start by writing your system in state-space form<\/p>\n\n\n\n<p>$$\\dot{x}=f(x,t)$$<\/p>\n\n\n\n<p>and implement a function that takes in $t$ and $x$ as arguments and produces $\\dot{x}$ as output. For example, the function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function xdot = myfun(t, x)\n% &#91;x1'; x2'] = &#91;0 1; -1 0] &#91;x1; x2]\nxdot = &#91;0 1; -1 0] * x<\/code><\/pre>\n\n\n\n<p>can be used to simulate the linear system with state-space model<\/p>\n\n\n\n<p>$$\\begin{pmatrix} \\dot{x}_1 \\\\ \\dot{x}_2 \\end{pmatrix}=\\begin{bmatrix}0&amp;1\\\\-1&amp;0\\end{bmatrix}\\begin{pmatrix}x_1\\\\x_2\\end{pmatrix}$$<\/p>\n\n\n\n<p>Note that the ODE function can be time-varying, that is it depends not only on $x$ but also on $t$. A typical call to <code>ode45<\/code> is as in:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tspan=&#91;0 10];\nx0 = &#91;1; 0];\n&#91;t, x] = ode45(myfun, tspan, x0);<\/code><\/pre>\n\n\n\n<p>in which the vector <code>tspan<\/code> contains the initial and final simulation times, and <code>x0<\/code> the initial conditions. Upon termination, the vector <code>x<\/code> contains a number of samples of the state vector $x$ stacked up corresponding to the time values in the vector <code>t<\/code>. The entire system evolution can be plotted using<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plot(t, x);<\/code><\/pre>\n\n\n\n<p>It used to be a pain to pass parameters to the ODE function but it is now relatively easy to do it using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/anonymous-functions.html\" class=\"broken_link\">anonymous functions<\/a>. For example, one could add parameters to the ode function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function xdot = myfun(t, x, A)\n% x' = A x\nxdot = A * x<\/code><\/pre>\n\n\n\n<p>and call <code>ode45<\/code> as in<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A = &#91;0 1; -1 0];\n&#91;t, x] = ode45(@(t,x) myfun(t,x,A), tspan, x0);<\/code><\/pre>\n\n\n\n<p>or simply<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;t, x] = ode45(@(t,x) A*x, tspan, x0);<\/code><\/pre>\n\n\n\n<p>Of course there is little reason to use <code>ode45<\/code> if you are simulating a linear system, since you could have used the much more flexible <code>lsim<\/code>. So we close with a simple nonlinear example<\/p>\n\n\n\n<p>$$\\begin{pmatrix} \\dot{x}_1 \\\\ \\dot{x}_2 \\end{pmatrix}=\\begin{pmatrix}x_1-x_1 x_2 \\\\ .9 x_1 x_2-x_2\\end{pmatrix}$$<\/p>\n\n\n\n<p>that could be simulated using<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x0 = &#91;1; 1];\n&#91;t, x] = ode45(@(t,x) &#91;x(1)-x(1)*x(2);.9*x(1)*x(2)-x(2)], tspan, x0);<\/code><\/pre>\n\n\n\n<p>ODE solvers such as <code>ode45<\/code> are great for simulating the response to initial conditions of systems but not so great if one would have varying input signals. Still waiting for a simple solution in that case&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Chapter 5 we discuss how to produce block diagrams for the simulation of linear and certain nonlinear systems described by ordinary differential equations.<\/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":[52,65,11],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/1714"}],"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=1714"}],"version-history":[{"count":4,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/1714\/revisions"}],"predecessor-version":[{"id":1721,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/1714\/revisions\/1721"}],"wp:attachment":[{"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/media?parent=1714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/categories?post=1714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/tags?post=1714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}