{"id":132,"date":"2019-05-16T06:54:36","date_gmt":"2019-05-16T14:54:36","guid":{"rendered":"https:\/\/linearcontrol.info\/fundamentals\/?p=132"},"modified":"2019-06-03T07:23:03","modified_gmt":"2019-06-03T15:23:03","slug":"simulating-linear-systems-with-non-zero-initial-conditions-in-state-space","status":"publish","type":"post","link":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/2019\/05\/16\/simulating-linear-systems-with-non-zero-initial-conditions-in-state-space\/","title":{"rendered":"Simulating linear systems with non-zero initial conditions in state space"},"content":{"rendered":"\n<p>Matlab&#8217;s <code>lsim<\/code> function for <a href=\"https:\/\/linearcontrol.info\/fundamentals\/index.php\/2019\/05\/15\/simulating-linear-systems-with-matlabs-lsim\/\">simulating linear systems<\/a> will give you the option to provide an initial condition if your system is in state-space but not for transfer-functions.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>One problem is that you either start in state-space, in which case you already know your initial conditions in terms of the initial state, or you have somehow to figure out how to convert initial conditions stated in terms of the system output as an initial state. This is a common source of <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/25165-lsim-with-nonzero-initial-condition\" class=\"broken_link\">confusion<\/a> to readers that are not familiar with state-space techniques.<\/p>\n\n\n\n<p>Before proceeding, one might want to take a look at Chapter 5 for an introductory overview of state-space systems.<\/p>\n\n\n\n<p>Let&#8217;s start by looking at how you can use Matlab to transform a transfer-function, say<\/p>\n\n\n\n<p>$$G(s) = \\frac{1}{s^2 + s + 1},$$<\/p>\n\n\n\n<p> into one of its many possible state-space realizations. For that one can use <code>ss<\/code> or <code>tf2ss<\/code>. For example, after running<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Gtf = tf([1], [1 1 1])\nGss = ss(Gtf)<\/pre>\n\n\n\n<p>the variable <code>Gss<\/code> will contain a state-space realization of the transfer-function <code>Gtf<\/code>. You can check that by using <code>tf<\/code> to transform it back as in<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tf(Gss)<\/pre>\n\n\n\n<p>Now recall that in state-space <code>Gss<\/code> is described by the matrices $(A,B,C,D)$, accessible as <code>Gss.a<\/code>, <code>Gss.b<\/code>, <code>Gss.c<\/code>, and <code>Gss.d<\/code>, or using<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[a,b,c,d] = ssdata(Gss)<\/pre>\n\n\n\n<p>Once in state-space, the function <code>lsim<\/code> will accept an initial state option, as in <code>lsim(Gss, u, t, x0)<\/code>, where <code>x0<\/code> is a vector with the initial condition. If you have a vector of initial conditions $y_0$, how can you transform it to the corresponding initial state $x_0$? That is the question addressed next. <\/p>\n\n\n\n<p>We will use linearity and look only at the part of the response due to the initial condition, that is we shall consider a zero input in the next paragraphs. As seen in Chapter 5, the response&nbsp;to a zero input and nonzero initial state $x(0) = x_0$ is given by<\/p>\n\n\n\n<p>$$y(t)=C e^{At} x_0$$<\/p>\n\n\n\n<p>Say that your system is of order $n$ you have initial conditions $(y(0), \\dot{y}(0), \\cdots, y^{(n-1)}(0))$ and you would like to know how to calculate a corresponding initial state, $x_0$. Because<\/p>\n\n\n\n<p>$$\\dot{y}(t)=C A e^{A t} x_0, \\quad  \\cdots \\quad, y^{(n-1)}(t) = C A^{n-1} e^{A t} x_0$$<\/p>\n\n\n\n<p>one can do that by solving a system of linear equations. Here&#8217;s how it works. Simply rearrange<\/p>\n\n\n\n<p>$$y(0) = C x_0, \\quad \\dot{y}(0) = C A x_0, \\quad \\cdots \\quad, y^{(n-1)}=CA^{n-1} x_0$$<\/p>\n\n\n\n<p>in the form of a set of linear equations<\/p>\n\n\n\n<p>$$\\mathbf{O} x_0 = \\mathbf{y}_0, \\quad \\mathbf{O}=\\begin{bmatrix}C\\\\CA\\\\\\vdots\\\\CA^{n-1}\\end{bmatrix}, \\quad \\mathbf{y}_0 =\\begin{pmatrix}y(0)\\\\\\dot{y}(0)\\\\\\vdots\\\\y^{(n-1)}(0)\\end{pmatrix}$$<\/p>\n\n\n\n<p>and solve for $x_0 = \\mathbf{O}^{-1} \\mathbf{y}_0$ to calculate a suitable initial state $x_0$. The above matrix $\\mathbf{O}$ is known as the <em>observability matrix<\/em> and it is so important that Matlab has a function to calculate it: <code>obsv<\/code>. The following excerpt puts it all together to simulate a step response from a non-zero initial condition.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Gtf = tf([1],[1 1 1]);\nGss = ss(Gtf);\ny0 = [-1; 0]; % initial conditions y(0) = -1, y'(0) = 0\nO = obsv(Gss.a, Gss.c);\nx0 = O \\ y0;\nT = 2;\nt = linspace(0, T, 100);\nu = ones(size(t));\ny = lsim(Gss, u, t, x0);<\/pre>\n\n\n\n<p>P.S.: You might be asking when is the above matrix $\\mathbf{O}$ invertible? The answer is it will be invertible if the corresponding state-space realization is observable (See Section 5.3). For SISO systems this will correspond to not having pole-zero cancellations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matlab&#8217;s lsim function for simulating linear systems will give you the option to provide an initial condition if your system is in state-space but not for transfer-functions.<\/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,12,10,11,13],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/132"}],"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=132"}],"version-history":[{"count":14,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":488,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/posts\/132\/revisions\/488"}],"wp:attachment":[{"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linearcontrol.info\/fundamentals\/index.php\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}