<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://wind-m05.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://wind-m05.github.io/" rel="alternate" type="text/html" /><updated>2024-08-19T13:01:28+00:00</updated><id>https://wind-m05.github.io/feed.xml</id><title type="html">Michiel Wind</title><subtitle>Personal website for engineering projects
</subtitle><author><name>Michiel</name></author><entry><title type="html">Model reduction of 2D heat diffusion using Galerkin projection and POD</title><link href="https://wind-m05.github.io/2023/02/11/2D-heat-diffusion-model-reduction.html" rel="alternate" type="text/html" title="Model reduction of 2D heat diffusion using Galerkin projection and POD" /><published>2023-02-11T00:00:00+00:00</published><updated>2023-02-11T00:00:00+00:00</updated><id>https://wind-m05.github.io/2023/02/11/2D-heat-diffusion-model-reduction</id><content type="html" xml:base="https://wind-m05.github.io/2023/02/11/2D-heat-diffusion-model-reduction.html"><![CDATA[<p>This project focusses on describing the temperature distribution evolution of a 2D plate with as few equations as possible. In particular we will be interested in the <a href="https://en.wikipedia.org/wiki/Heat_equation">heat diffusion</a> process that takes place as a result of individual heat loads $u_1$ and $u_2$ applied on the surface. The equations will be approximated by using a <a href="https://en.wikipedia.org/wiki/Galerkin_method">Galerkin projection</a> onto the heat diffusion equation. The fourier basis will be exploited to utilize its orthogonality properties. After which the <a href="https://en.wikipedia.org/wiki/Proper_orthogonal_decomposition">POD</a> basis will be defined from a high dimensional data set to represent the heat diffusion equations in only a few ordinary differential equations. Before we start with the derivations, in the video below you can see the main results.</p>

<div><div class="extensions extensions--video">
  <iframe src="https://www.youtube.com/embed/GkOMrms2zYk?rel=0&amp;showinfo=0" frameborder="0" scrolling="no" allowfullscreen=""></iframe>
</div>
</div>

<h1 id="heat-diffusion-equation">Heat diffusion equation</h1>
<p>Firstly, the given heat diffusion equation is stated as follows.
\(\rho(x, y) c(x, y) \frac{\partial T}{\partial t}(x, y, t)=\left[\begin{array}{ll}
\frac{\partial}{\partial x} &amp; \frac{\partial}{\partial y}
\end{array}\right] K(x, y)\left[\begin{array}{l}
\frac{\partial T(x, y, t)}{\partial x} \\
\frac{\partial T(x, y , t)}{\partial y}
\end{array}\right]+u(x, y, t)\)
Where,</p>
<ul>
  <li>$T(x,y,t)$ ~ $[K]$ is the temperature in Kelvin.</li>
  <li>$\rho(x,y)$ ~ $[kg/m^3]$ is the density of the plate.</li>
  <li>$c(x,y)$ ~ $[J/(kg K)]$ is the heat capacity.</li>
  <li>$K(x,y)$ ~ $[W/(mK)]$ is the location dependent thermal conductivity matrix, which is positive definite.</li>
  <li>$u(x,y,t)$ ~ $[W/m^2]$ is the input heat flux into the plate.</li>
</ul>

<p>For the rest of the project the plate is assumed to be isotropic and homogeneous.</p>

<h2 id="fourier-basis">Fourier basis</h2>
<p>The inner product will be defined as follows:</p>

\[\left\langle \varphi_i,\varphi_j \right\rangle:=\int_0^{L_x}\int_0^{L_y}\varphi_i(x,y)\varphi_j(x,y)dydx.\]

<p>$\varphi_{k,l}$ represents the product of $\varphi_k(x)$ and $\varphi_l(y)$, which are defined as follows for non-negative integers $k$ and $l$</p>

\[\varphi_k(x) = 
    \begin{cases}
      \;\frac{1}{\sqrt{L_x}}\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\text{if}\;k=0\\
      \sqrt{\frac{2}{L_x}}\cos\left(\frac{k\pi x}{L_x}\right)\;\;\;\;\;\text{if}\;k&gt;0
    \end{cases}
    \;\;\;\;\;\;\;\;\;\;
\varphi_l(y) = 
    \begin{cases}
      \;\frac{1}{\sqrt{L_y}}\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\text{if}\;l=0\\
      \sqrt{\frac{2}{L_y}}\cos\left(\frac{l\pi y}{L_y}\right)\;\;\;\;\;\text{if}\;l&gt;0
    \end{cases}\]

<h2 id="galerkin-projection">Galerkin projection</h2>
<p>We use the Galerkin projection to derive, for arbitrary $k$ and $l$, an explicit ordinary differential equation for the coefficient function $a_{k,l}(t)$ in the spectral expansion given as:</p>

\[T(x,y,t)=\sum_{k=0}^\infty\sum_{l=0}^\infty a_{k,l}(t)\varphi_{k,l}(x,y)\]

<p>The Galerkin projection of the homogeneous model is taken as follows</p>

\[\left\langle \rho c \frac{\partial}{\partial t}T(x,y,t),\varphi_{k,l} \right\rangle=
    \left\langle \kappa \left(\frac{\partial^2}{\partial x^2}T(x,y,t)+\frac{\partial^2}{\partial y^2}T(x,y,t)\right) + u(x,y,t),\varphi_{k,l} \right\rangle\]

<p>Now, the spectral expansion is substituted into the Galerkin projection. However, the non-negative integers $k$ and $l$ of this spectral expansion are changed to $p$ and $q$ respectively, since $k$ and $l$ in are different. Doing this results in</p>

\[\begin{aligned}
    &amp;\left\langle \rho c \frac{\partial}{\partial t}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}(t)\varphi_{p,q}(x,y),\varphi_{k,l}(x,y) \right\rangle=\\
    &amp;\left\langle \kappa \left(\frac{\partial^2}{\partial x^2}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}(t)\varphi_{p,q}(x,y)+\frac{\partial^2}{\partial y^2}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}(t)\varphi_{p,q}(x,y)\right) + u(x,y,t),\varphi_{k,l}(x,y) \right\rangle
    \end{aligned}\]

<p>Due to linearity of the inner product, the summations can be taken out of the inner product, moreover the $x$, $y$ and $t$ dependencies are dropped for ease of notation, this gives</p>

\[\rho c \frac{\partial}{\partial t}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \varphi_{p,q},\varphi_{k,l} \right\rangle=
    \kappa\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \frac{\partial^2}{\partial x^2}\varphi_{p,q}+\frac{\partial^2}{\partial y^2}\varphi_{p,q},\varphi_{k,l}\right\rangle + \left\langle u,\varphi_{k,l} \right\rangle\]

<p>Since $\varphi_{p,q}$ is a product of the two basis functions, the two double partial derivatives with respect to $x$ and $y$ position can be rewritten to $\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q$. Additionally, the partial derivative with respect to time of $a_{p,q}$ is rewritten as $\dot a_{p,q}$. The expression therefore becomes:</p>

\[\rho c \sum_{p=0}^\infty\sum_{q=0}^\infty \dot a_{p,q} \left\langle \varphi_{p,q},\varphi_{k,l} \right\rangle=
    \kappa\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q,\varphi_{k,l}\right\rangle + \left\langle u,\varphi_{k,l} \right\rangle\]

<p>Due to the <a href="https://en.wikipedia.org/wiki/Orthonormality">orthonormality</a> of $\varphi_{k,l}$, the summation on the left-hand side of the equation will only result in non-zero values when  $p$ and $q$ are equal to $k$ and $l$ respectively. This gives the following expression for $\dot a_{k,l}$.</p>

\[\dot a_{k,l} =\frac{\kappa}{\rho c}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q,\varphi_{k,l}\right\rangle + \frac{1}{\rho c}\left\langle u,\varphi_{k,l} \right\rangle\]

<p>Next up, evaluation of $\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q$ for different values of $p$ and $q$. This is done through fourier basis functions, keep in mind that $\ddot\varphi_p$ is the second partial derivative with respect to $x$ position and $\ddot\varphi_q$ is the second partial derivative with respect to $y$ position.</p>

\[\begin{aligned}
    &amp;\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q = 0\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \text{if}\;\;p = 0\;\;\text{and}\;\;q = 0\\
    &amp;\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q = -\frac{p^2\pi^2}{L_x^2}\varphi_{p,q}\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \text{if}\;\;p &gt; 0\;\;\text{and}\;\;q = 0\\
    &amp;\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q = -\frac{q^2\pi^2}{L_y^2}\varphi_{p,q}\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \text{if}\;\;p = 0\;\;\text{and}\;\;q &gt; 0\\
    &amp;\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q = -\left(\frac{p^2\pi^2}{L_x^2}+\frac{q^2\pi^2}{L_y^2}\right)\varphi_{p,q}\;\;\;\;\;\; \text{if}\;\;p &gt; 0\;\;\text{and}\;\;q &gt; 0\\
    \end{aligned}\]

<p>By looking at the expressions above, it can be concluded that the fourth expression, describing the case in which both $p$ and $q$ are greater than zero, also holds for the cases when $p$ or $q$ are equal to zero. Thus, all cases converge towards.</p>

\[\dot a_{k,l} =-\frac{\kappa}{\rho c}\left(\frac{p^2\pi^2}{L_x^2}+\frac{q^2\pi^2}{L_y^2}\right)\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \varphi_{p,q},\varphi_{k,l}\right\rangle + \frac{1}{\rho c}\left\langle u,\varphi_{k,l} \right\rangle\]

<p>Through the property of orthonormality of $\varphi_{k,l}$, this expression can again be rewritten</p>

\[\dot a_{k,l}=-\frac{\kappa}{\rho c}\left( \frac{k^2\pi^2}{L_x^2}+\frac{l^2\pi^2}{L_y^2}\right) a_{k,l}+ \frac{1}{\rho c}\left\langle u,\varphi_{k,l} \right\rangle\]

<p>Writing out the standard inner product of $u$ and $\varphi_{k,l}$, and 
Substituting back in the time and position dependencies gives</p>

\[\dot a_{k,l}(t)=-\frac{\kappa}{\rho c}\left( \frac{k^2\pi^2}{L_x^2}+\frac{l^2\pi^2}{L_y^2}\right) a_{k,l}(t)+ \frac{1}{\rho c}\left\langle u(x,y,t),\varphi_{k,l}(x,y) \right\rangle\]

<p>Therefore, a system of completely decoupled ordinary differential equations is found for the coefficient function $a_{k,l}(t)$.</p>

<h1 id="simulation">Simulation</h1>

<h2 id="initial-conditions">Initial conditions</h2>
<p>Three different initial temperature profiles will be experimented with.</p>
<ul>
  <li>Gaussian distributed temperature profile</li>
  <li>Block profile</li>
  <li>Fourier basis profile</li>
</ul>

<h2 id="simulation-without-input">Simulation without input</h2>
<p>For animations I would like to refer you to the youtube link on the top of the post.</p>

<p><img src="/assets/images/model-reduction/fourier_noinput.png" alt="fouriernoinput" /></p>

<p>The temperature distributions on the plate, simulated at different points in time, for different values of $K$ and $L$, are given in the figure above. From this figure, it becomes clear that adding additional basis functions, through higher values of $K$ and $L$, has diminishing returns. This is at least the case for the initial temperature profile that was chosen. The figure where both $K$ and $L$ are equal to $3$, shows that approximation of the initial temperature profile is poor due to each direction of the plane being only approximated by a few cosine functions. Creating a disparity between $K$ and $L$ results in one axis being approximated by more distinct basis functions than the other axis.</p>

<h2 id="simulation-with-input">Simulation with input</h2>

<p><img src="/assets/images/model-reduction/fourier_input.png" alt="fourierinput" />
The temperature evolution of the same Gaussian initial heat profile from previous section is depicted for varying $K$ and $L$. The input heat sources are given as the absolute values of time-depending sinusoidal functions, with an amplitude of $0.4$ Kelvin. A phase difference of $90^o$ was chosen between $u_1(t)$ and $u_2(t)$. From these figures, it be becomes clear that the lowest order truncation ($K=L=3$) performs poorly, whilst the higher order truncations ($K=L=10$ and $K=L=50$) perform nearly identical. In contrast to previous section, the heated plate can never settle at a homogeneous temperature as long as the input is nonzero. After most of the dynamics of the initial condition have settled, a ridge shaped temperature profile can be seen. This shape is the result of the plate being longer in the $y$-direction, as well as the imposed boundary conditions. Additionally, the continuous input of heat, without any heat loss to the environment, causes the average plate temperature to rise over time.</p>

<p><img src="/assets/images/model-reduction/fourier_block.png" alt="fourierblock" />
The temperature evolution for the heated-plate with an initial block-shaped temperature profile is shown above. In contrast to the Gaussian initial temperature profile, this shape is a lot harder to be approximated through the cosine basis functions. Higher truncation orders have significantly better performance in this case.</p>

<p><img src="/assets/images/model-reduction/fourier_initial.png" alt="fourierinitial" />
The figure above depicts the initial temperature profile that was described through the basis functions. This is not a realistic temperature distribution, and therefore not of much interest. However, it is still interesting to see that the approximation of the initial profile would be very accurate when $K$ and $L$ are greater than, or equal to the order used for the initial temperature profile, and not accurate if $K$ and $L$ would be smaller.</p>

<p>It can be concluded that the shape of the initial temperature profile has a large effect on the accuracy of the truncated model. Truncation of order 3 have very limited performance, whereas truncations of order 10 are able to capture the main dynamics of interest. Order 50 truncations are able to approximate square shapes with reasonably high precision.</p>

<h1 id="pod-basis">POD basis</h1>
<p>To even further reduce the number of ordinary differential equations that are required to represent the diffusion behavior, the <a href="https://en.wikipedia.org/wiki/Proper_orthogonal_decomposition">POD</a> basis will be used. POD stands for proper orthogonal decomposition, which is a technique to reduce the order of complexity by looking at the <a href="https://en.wikipedia.org/wiki/Singular_value_decomposition">singular value decomposition</a> of high dimensional data and only focussing on dominant behavior. In this project the fourier basis with $K=50$ and $L=50$ is used to create a matrix of snapshots. Where the rows represent the spatial data and the columns represent temporal data. Since this problem is 2-dimensional the spatial data is stacked. The SVD is defined as follows.
\(W_{snap}=U \Sigma V^T\)
Where U and V are unitary rotation matrices and sigma is a diagonal matrix that contains the ordered singular values. After using the singular value decomposition and scaling it by the proper discretization constants, the new basis functions in the most dominant directions will reveal inside of the U matrix. These vectors are also ordered in dominance from left to right. Where the most left vector is the most dominant. The first three dominant shapes are shown below.
<img src="/assets/images/model-reduction/POD_basis_functions.png" alt="PODbasis" />
To see the advantage of the POD basis compared to the Fourier basis, I took the block shape as the initial condition. The POD basis can represent block like behavior with $R = 13$ modes compared to $K=50$ and $L=50$ that was required for the fourier basis, because it is not constrained by only sinusoidal functions. Comparison for $R = 5$ and $R = 13$ is shown below.</p>

<p><img src="/assets/images/model-reduction/POD.png" alt="POD" /></p>

<p>To test the performance of the POD basis compared to the fourier basis when we simulate it with different initial conditions and different input behavior than was shown in the snapshot matrix. This implies that the POD basis must simulate the diffusion behavior with modes that it has not seen before. This is clearly visible in the figure below. In a) the initial condition is the same as in the snapshot matrix, but the input now is constant instead of sinusoidal, which causes the residual compared to b) in the figure above to grow over time. Since the input is different this is to be expected. In the case of b) the input is the same as the snapshot matrix, but the initial condition is forced to be Gaussian. This shows that the POD basis poorly describes the gaussian initial condition, because this mode is not contained in the snapshot matrix. However, after the initial behavior has died out, this scenario resembles the snapshot matrix again since the input is the same.</p>

<p><img src="/assets/images/model-reduction/POD_different.png" alt="PODdifferent" /></p>

<h2 id="conclusion--recommendations">Conclusion &amp; Recommendations</h2>
<p>To conclude, the POD basis can drastically reduce the model order. However, care should be taken with models that vary in time, since the POD basis is only as good as the data that represents the actual spatial-temporal behavior of the true system.</p>

<p>The code can be extended to also work for non-homogeneous heat diffusion. The $\rho(x,y)c(x,y)$ as well as $\kappa(x,y)$ must be continuous and differentiable in $x$ and $y$ in order to model it as a linear PDE.</p>

<h1 id="resources">Resources</h1>

<h3 id="software">Software</h3>
<p>If you are interested in the source code from this project, I invite you to take a look at my <a href="https://github.com/wind-m05/model-reduction-project">Repository</a>.</p>

<h3 id="tutorials">Tutorials</h3>
<p>All video lectures of <a href="https://www.youtube.com/@Eigensteve">Steve Brunton</a> and <a href="https://www.youtube.com/@NathanKutzAMATH/videos">Nathan Kutz</a> were very helpful in order to understand the basics of POD and model reduction in general.</p>]]></content><author><name>Michiel</name></author><category term="Model-reduction" /><category term="Galerkin-projection" /><category term="POD" /><summary type="html"><![CDATA[This project focusses on describing the temperature distribution evolution of a 2D plate with as few equations as possible. In particular we will be interested in the heat diffusion process that takes place as a result of individual heat loads $u_1$ and $u_2$ applied on the surface. The equations will be approximated by using a Galerkin projection onto the heat diffusion equation. The fourier basis will be exploited to utilize its orthogonality properties. After which the POD basis will be defined from a high dimensional data set to represent the heat diffusion equations in only a few ordinary differential equations. Before we start with the derivations, in the video below you can see the main results. Heat diffusion equation Firstly, the given heat diffusion equation is stated as follows. \(\rho(x, y) c(x, y) \frac{\partial T}{\partial t}(x, y, t)=\left[\begin{array}{ll} \frac{\partial}{\partial x} &amp; \frac{\partial}{\partial y} \end{array}\right] K(x, y)\left[\begin{array}{l} \frac{\partial T(x, y, t)}{\partial x} \\ \frac{\partial T(x, y , t)}{\partial y} \end{array}\right]+u(x, y, t)\) Where, $T(x,y,t)$ ~ $[K]$ is the temperature in Kelvin. $\rho(x,y)$ ~ $[kg/m^3]$ is the density of the plate. $c(x,y)$ ~ $[J/(kg K)]$ is the heat capacity. $K(x,y)$ ~ $[W/(mK)]$ is the location dependent thermal conductivity matrix, which is positive definite. $u(x,y,t)$ ~ $[W/m^2]$ is the input heat flux into the plate. For the rest of the project the plate is assumed to be isotropic and homogeneous. Fourier basis The inner product will be defined as follows: \[\left\langle \varphi_i,\varphi_j \right\rangle:=\int_0^{L_x}\int_0^{L_y}\varphi_i(x,y)\varphi_j(x,y)dydx.\] $\varphi_{k,l}$ represents the product of $\varphi_k(x)$ and $\varphi_l(y)$, which are defined as follows for non-negative integers $k$ and $l$ \[\varphi_k(x) = \begin{cases} \;\frac{1}{\sqrt{L_x}}\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\text{if}\;k=0\\ \sqrt{\frac{2}{L_x}}\cos\left(\frac{k\pi x}{L_x}\right)\;\;\;\;\;\text{if}\;k&gt;0 \end{cases} \;\;\;\;\;\;\;\;\;\; \varphi_l(y) = \begin{cases} \;\frac{1}{\sqrt{L_y}}\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\text{if}\;l=0\\ \sqrt{\frac{2}{L_y}}\cos\left(\frac{l\pi y}{L_y}\right)\;\;\;\;\;\text{if}\;l&gt;0 \end{cases}\] Galerkin projection We use the Galerkin projection to derive, for arbitrary $k$ and $l$, an explicit ordinary differential equation for the coefficient function $a_{k,l}(t)$ in the spectral expansion given as: \[T(x,y,t)=\sum_{k=0}^\infty\sum_{l=0}^\infty a_{k,l}(t)\varphi_{k,l}(x,y)\] The Galerkin projection of the homogeneous model is taken as follows \[\left\langle \rho c \frac{\partial}{\partial t}T(x,y,t),\varphi_{k,l} \right\rangle= \left\langle \kappa \left(\frac{\partial^2}{\partial x^2}T(x,y,t)+\frac{\partial^2}{\partial y^2}T(x,y,t)\right) + u(x,y,t),\varphi_{k,l} \right\rangle\] Now, the spectral expansion is substituted into the Galerkin projection. However, the non-negative integers $k$ and $l$ of this spectral expansion are changed to $p$ and $q$ respectively, since $k$ and $l$ in are different. Doing this results in \[\begin{aligned} &amp;\left\langle \rho c \frac{\partial}{\partial t}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}(t)\varphi_{p,q}(x,y),\varphi_{k,l}(x,y) \right\rangle=\\ &amp;\left\langle \kappa \left(\frac{\partial^2}{\partial x^2}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}(t)\varphi_{p,q}(x,y)+\frac{\partial^2}{\partial y^2}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}(t)\varphi_{p,q}(x,y)\right) + u(x,y,t),\varphi_{k,l}(x,y) \right\rangle \end{aligned}\] Due to linearity of the inner product, the summations can be taken out of the inner product, moreover the $x$, $y$ and $t$ dependencies are dropped for ease of notation, this gives \[\rho c \frac{\partial}{\partial t}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \varphi_{p,q},\varphi_{k,l} \right\rangle= \kappa\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \frac{\partial^2}{\partial x^2}\varphi_{p,q}+\frac{\partial^2}{\partial y^2}\varphi_{p,q},\varphi_{k,l}\right\rangle + \left\langle u,\varphi_{k,l} \right\rangle\] Since $\varphi_{p,q}$ is a product of the two basis functions, the two double partial derivatives with respect to $x$ and $y$ position can be rewritten to $\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q$. Additionally, the partial derivative with respect to time of $a_{p,q}$ is rewritten as $\dot a_{p,q}$. The expression therefore becomes: \[\rho c \sum_{p=0}^\infty\sum_{q=0}^\infty \dot a_{p,q} \left\langle \varphi_{p,q},\varphi_{k,l} \right\rangle= \kappa\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q,\varphi_{k,l}\right\rangle + \left\langle u,\varphi_{k,l} \right\rangle\] Due to the orthonormality of $\varphi_{k,l}$, the summation on the left-hand side of the equation will only result in non-zero values when $p$ and $q$ are equal to $k$ and $l$ respectively. This gives the following expression for $\dot a_{k,l}$. \[\dot a_{k,l} =\frac{\kappa}{\rho c}\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q,\varphi_{k,l}\right\rangle + \frac{1}{\rho c}\left\langle u,\varphi_{k,l} \right\rangle\] Next up, evaluation of $\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q$ for different values of $p$ and $q$. This is done through fourier basis functions, keep in mind that $\ddot\varphi_p$ is the second partial derivative with respect to $x$ position and $\ddot\varphi_q$ is the second partial derivative with respect to $y$ position. \[\begin{aligned} &amp;\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q = 0\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \text{if}\;\;p = 0\;\;\text{and}\;\;q = 0\\ &amp;\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q = -\frac{p^2\pi^2}{L_x^2}\varphi_{p,q}\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \text{if}\;\;p &gt; 0\;\;\text{and}\;\;q = 0\\ &amp;\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q = -\frac{q^2\pi^2}{L_y^2}\varphi_{p,q}\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \text{if}\;\;p = 0\;\;\text{and}\;\;q &gt; 0\\ &amp;\ddot\varphi_p\varphi_q + \varphi_p \ddot\varphi_q = -\left(\frac{p^2\pi^2}{L_x^2}+\frac{q^2\pi^2}{L_y^2}\right)\varphi_{p,q}\;\;\;\;\;\; \text{if}\;\;p &gt; 0\;\;\text{and}\;\;q &gt; 0\\ \end{aligned}\] By looking at the expressions above, it can be concluded that the fourth expression, describing the case in which both $p$ and $q$ are greater than zero, also holds for the cases when $p$ or $q$ are equal to zero. Thus, all cases converge towards. \[\dot a_{k,l} =-\frac{\kappa}{\rho c}\left(\frac{p^2\pi^2}{L_x^2}+\frac{q^2\pi^2}{L_y^2}\right)\sum_{p=0}^\infty\sum_{q=0}^\infty a_{p,q}\left\langle \varphi_{p,q},\varphi_{k,l}\right\rangle + \frac{1}{\rho c}\left\langle u,\varphi_{k,l} \right\rangle\] Through the property of orthonormality of $\varphi_{k,l}$, this expression can again be rewritten \[\dot a_{k,l}=-\frac{\kappa}{\rho c}\left( \frac{k^2\pi^2}{L_x^2}+\frac{l^2\pi^2}{L_y^2}\right) a_{k,l}+ \frac{1}{\rho c}\left\langle u,\varphi_{k,l} \right\rangle\] Writing out the standard inner product of $u$ and $\varphi_{k,l}$, and Substituting back in the time and position dependencies gives \[\dot a_{k,l}(t)=-\frac{\kappa}{\rho c}\left( \frac{k^2\pi^2}{L_x^2}+\frac{l^2\pi^2}{L_y^2}\right) a_{k,l}(t)+ \frac{1}{\rho c}\left\langle u(x,y,t),\varphi_{k,l}(x,y) \right\rangle\] Therefore, a system of completely decoupled ordinary differential equations is found for the coefficient function $a_{k,l}(t)$. Simulation Initial conditions Three different initial temperature profiles will be experimented with. Gaussian distributed temperature profile Block profile Fourier basis profile Simulation without input For animations I would like to refer you to the youtube link on the top of the post. The temperature distributions on the plate, simulated at different points in time, for different values of $K$ and $L$, are given in the figure above. From this figure, it becomes clear that adding additional basis functions, through higher values of $K$ and $L$, has diminishing returns. This is at least the case for the initial temperature profile that was chosen. The figure where both $K$ and $L$ are equal to $3$, shows that approximation of the initial temperature profile is poor due to each direction of the plane being only approximated by a few cosine functions. Creating a disparity between $K$ and $L$ results in one axis being approximated by more distinct basis functions than the other axis. Simulation with input The temperature evolution of the same Gaussian initial heat profile from previous section is depicted for varying $K$ and $L$. The input heat sources are given as the absolute values of time-depending sinusoidal functions, with an amplitude of $0.4$ Kelvin. A phase difference of $90^o$ was chosen between $u_1(t)$ and $u_2(t)$. From these figures, it be becomes clear that the lowest order truncation ($K=L=3$) performs poorly, whilst the higher order truncations ($K=L=10$ and $K=L=50$) perform nearly identical. In contrast to previous section, the heated plate can never settle at a homogeneous temperature as long as the input is nonzero. After most of the dynamics of the initial condition have settled, a ridge shaped temperature profile can be seen. This shape is the result of the plate being longer in the $y$-direction, as well as the imposed boundary conditions. Additionally, the continuous input of heat, without any heat loss to the environment, causes the average plate temperature to rise over time. The temperature evolution for the heated-plate with an initial block-shaped temperature profile is shown above. In contrast to the Gaussian initial temperature profile, this shape is a lot harder to be approximated through the cosine basis functions. Higher truncation orders have significantly better performance in this case. The figure above depicts the initial temperature profile that was described through the basis functions. This is not a realistic temperature distribution, and therefore not of much interest. However, it is still interesting to see that the approximation of the initial profile would be very accurate when $K$ and $L$ are greater than, or equal to the order used for the initial temperature profile, and not accurate if $K$ and $L$ would be smaller. It can be concluded that the shape of the initial temperature profile has a large effect on the accuracy of the truncated model. Truncation of order 3 have very limited performance, whereas truncations of order 10 are able to capture the main dynamics of interest. Order 50 truncations are able to approximate square shapes with reasonably high precision. POD basis To even further reduce the number of ordinary differential equations that are required to represent the diffusion behavior, the POD basis will be used. POD stands for proper orthogonal decomposition, which is a technique to reduce the order of complexity by looking at the singular value decomposition of high dimensional data and only focussing on dominant behavior. In this project the fourier basis with $K=50$ and $L=50$ is used to create a matrix of snapshots. Where the rows represent the spatial data and the columns represent temporal data. Since this problem is 2-dimensional the spatial data is stacked. The SVD is defined as follows. \(W_{snap}=U \Sigma V^T\) Where U and V are unitary rotation matrices and sigma is a diagonal matrix that contains the ordered singular values. After using the singular value decomposition and scaling it by the proper discretization constants, the new basis functions in the most dominant directions will reveal inside of the U matrix. These vectors are also ordered in dominance from left to right. Where the most left vector is the most dominant. The first three dominant shapes are shown below. To see the advantage of the POD basis compared to the Fourier basis, I took the block shape as the initial condition. The POD basis can represent block like behavior with $R = 13$ modes compared to $K=50$ and $L=50$ that was required for the fourier basis, because it is not constrained by only sinusoidal functions. Comparison for $R = 5$ and $R = 13$ is shown below. To test the performance of the POD basis compared to the fourier basis when we simulate it with different initial conditions and different input behavior than was shown in the snapshot matrix. This implies that the POD basis must simulate the diffusion behavior with modes that it has not seen before. This is clearly visible in the figure below. In a) the initial condition is the same as in the snapshot matrix, but the input now is constant instead of sinusoidal, which causes the residual compared to b) in the figure above to grow over time. Since the input is different this is to be expected. In the case of b) the input is the same as the snapshot matrix, but the initial condition is forced to be Gaussian. This shows that the POD basis poorly describes the gaussian initial condition, because this mode is not contained in the snapshot matrix. However, after the initial behavior has died out, this scenario resembles the snapshot matrix again since the input is the same. Conclusion &amp; Recommendations To conclude, the POD basis can drastically reduce the model order. However, care should be taken with models that vary in time, since the POD basis is only as good as the data that represents the actual spatial-temporal behavior of the true system. The code can be extended to also work for non-homogeneous heat diffusion. The $\rho(x,y)c(x,y)$ as well as $\kappa(x,y)$ must be continuous and differentiable in $x$ and $y$ in order to model it as a linear PDE. Resources Software If you are interested in the source code from this project, I invite you to take a look at my Repository. Tutorials All video lectures of Steve Brunton and Nathan Kutz were very helpful in order to understand the basics of POD and model reduction in general.]]></summary></entry><entry><title type="html">Nonlinear dynamics of the Immune system</title><link href="https://wind-m05.github.io/2022/11/14/Nonlinear-dynamics-Analysis-Immune-system.html" rel="alternate" type="text/html" title="Nonlinear dynamics of the Immune system" /><published>2022-11-14T00:00:00+00:00</published><updated>2022-11-14T00:00:00+00:00</updated><id>https://wind-m05.github.io/2022/11/14/Nonlinear-dynamics-Analysis-Immune-system</id><content type="html" xml:base="https://wind-m05.github.io/2022/11/14/Nonlinear-dynamics-Analysis-Immune-system.html"><![CDATA[<p>The goal of this project is to gain insight into the dynamic behavior of the immune system. Check stability of fixed points via Lyapunov stability analysis. Simulations with the use of MATLAB have been performed for the original system as well as the time-delayed system. Three clinical conditions are given and the response of the immune system is determined for each of them. For condition 3 also a time-delayed system is simulated for which a Hopf bifurcation is determined. All feasible fixed points in all clinical conditions where asymptotically stable.</p>
<h1 id="introduction">Introduction</h1>
<!-- This project was part of the course 'Modeling dynamics' (5SCA0) which was the first modeling course that focussed on non-linear systems. The content of the course was mainly mathematical and therefore not specific towards any engineering field. Examples in mechanical/electrical and chemical engineering have been proposed by defining the dynamic behavior with use of [port-Hamiltonian models](https://www.math.rug.nl/arjan/DownloadPublicaties/ICMvanderSchaft.pdf). -->
<p>Understanding the interaction between malignant cells and the immune system of an organism deserves quite some attention. A system theoretic perspective on this understanding proves quite useful for studying the dynamic features of tumor growth and to understand the implication on specific treatments. In fact, such a perspective brings valuable insight in some key mechanisms that are used in immunology, oncology and cancer biology. This will be the purpose of this project. The system of nonlinear coupled differential equations are given by:</p>

\[\begin{aligned}
&amp;\dot{M}=1+a_1 M(1-M)-a_2 M H \\ 
&amp;\dot{H}=a_3 H R-a_4 H  \\ 
&amp;\dot{R}=a_5 R(1-R)-a_6 H R-a_7 R 
\end{aligned}\]

<p>Where</p>

<ul>
  <li>$M$ is the density of the malignant or tumor cells,</li>
  <li>$H$ is the density of the active hunting cells,</li>
  <li>$R$ is the density of the resting cells in the immune system.</li>
</ul>

<p>All indicated parameters $a_i$ are non-negative numbers. In particular,</p>

<ul>
  <li>$a_1$ is the growth rate of tumor cells,</li>
  <li>$a_2$ is the rate of destruction of tumor cells by the hunting cells,</li>
  <li>$a_3$ is the conversion rate from resting cells to hunting cells,</li>
  <li>$a_4$ is the natural death rate of hunting cells,</li>
  <li>$a_5$ is the growth rate of resting cells,</li>
  <li>$a_6$ is the conversion rate from hunting to resting cells,</li>
  <li>$a_7$ is the natural natural death rate of resting cells.</li>
</ul>

<h1 id="fixed-points-and-biological-feasibility">Fixed points and biological feasibility</h1>
<p>The fixed points can be determined by solving the system of equations when the derivatives are zero, because the fixed points in a system are the solutions when there is no time dependency. Firstly, the rate of change of density of the hunting cells $\dot{H}=0$ is solved.</p>

\[\begin{aligned}
&amp; a_3 H R-a_4 H=0 \\ 
&amp; H(a_3 R-a_4)=0 \\ 
&amp; H^{\ast}_1=0 \vee R^{\ast}_1=\frac{a_4}{a_3}
\end{aligned}\]

<p>Where \(H^{\ast}_1\) will be substituted such that \(M^{\ast}_{1,\ 2}\) can be computed by solving $\dot{M}=0$.</p>

\[\begin{aligned}
&amp; 1+a_1M(1-M)-a_2 M H=0 \\ 
&amp; H^{\ast}_1=0\\
&amp; 1+a_1 M-a_1 M^2=0 \\ 
&amp; M^2-M -\frac{1}{a_1}\\ 
&amp; M^{\ast}_{1,2}=\frac{1}{2}\pm\sqrt{\frac{1}{4}+\frac{1}{a_1}} 
\end{aligned}\]

<p>Next, $H^{\ast}_1$ is substituted while $\dot{R}=0$ to obtain  $R_2$ and $R_3$:</p>

\[\begin{aligned}
&amp; H^{\ast}_1=0\\
&amp; a_5 R(1-R) - a_7 R=0 \\
&amp; R(a_5-a_5 R -a_7)=0 \\
&amp; R^{\ast}_2=0 \vee R^{\ast}_3=1-\frac{a_7}{a_5}
\end{aligned}\]

<p>$H^{\ast}_2$ can be computed by implementing $R^{\ast}_1$ in $\dot{R}=0$:</p>

\[\begin{aligned}
&amp; R^{\ast}_1=\frac{a_4}{a_3}\\
&amp; a_5 R(1-R)-a_6 H R-a_7 R=0 \\
&amp; \frac{a_4}{a_3}\cdot(a_5-a_5\cdot \frac{a_4}{a_3}-a_6\cdot H -a_7)=0 \\
&amp; H^{\ast}_2=\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6}
\end{aligned}\]

<p>Finally, \(H^{\ast}_2\) can be substituted in the equation $\dot{M}=0$ to obtain $M_{3,4}$:</p>

\[\begin{aligned}
&amp; H^{\ast}_2=\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6}\\
&amp; 1+a_1 M(1-M)-a_2 M H_2=0 \\
&amp; M^{\ast}_{3,4}=(\frac{1}{2}-\frac{a_2}{2a_1}H_2^{\ast})\pm \sqrt{(\frac{a_2}{a_1}H_2-1)^2+\frac{1}{a_1}}
\end{aligned}\]

<p>The resulting fixed points are stated below:</p>

\[\begin{aligned}
&amp; (M^{\ast}_1,H^{\ast}_1,R^{\ast}_2)=(\frac{1}{2}+\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,0)\\
&amp; (M^{\ast}_2,H^{\ast}_1,R^{\ast}_2)=(\frac{1}{2}-\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,0)\\
&amp; (M^{\ast}_1,H^{\ast}_1,R^{\ast}_3)=(\frac{1}{2}+\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,1-\frac{a_7}{a_5})\\
&amp; (M^{\ast}_2,H^{\ast}_1,R^{\ast}_3)=(\frac{1}{2}-\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,1-\frac{a_7}{a_5})\\
&amp; (M^{\ast}_3,H^{\ast}_2,R^{\ast}_1)=\frac{1}{2}(\frac{a_2}{a_1}H_2^{\ast}-1)+ \sqrt{\frac{1}{4}\cdot(\frac{a_2}{a_1}H_2^{\ast}-1)^2+\frac{1}{a_1}},\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6},\frac{a_4}{a_3})\\
&amp; (M^{\ast}_4,H^{\ast}_2,R^{\ast}_1)=\frac{1}{2}(\frac{a_2}{a_1}H_2^{\ast}-1)- \sqrt{\frac{1}{4}\cdot(\frac{a_2}{a_1}H_2^{\ast}-1)^2+\frac{1}{a_1}},\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6},\frac{a_4}{a_3})\\
\end{aligned}\]

<h2 id="feasibility">Feasibility</h2>

<p>The <a href="https://en.wikipedia.org/wiki/Positively_invariant_set">positive invariant</a> set \(\mathcal{P}\) is defined to be the following:</p>

\[\mathcal{P} := \{(M,H,R) \in \mathbb{R}^3 \;|\; M \geq 0, H \geq 0, R \geq 0 \}\]

<p>Positive invariance implies here that if the initial condition is inside the set, it will never leave the set. Since we are dealing with a physical system, densities cannot be negative, therefore all fixed points must lie within the set \(\mathcal{P}\) to be feasible.</p>

<p>In other words, the values of the fixed points require to be in set $\mathcal{P}$. The only fixed points which lie within this set for all coefficients $a_i&gt;0$ are the following:</p>

\[\begin{align}
    &amp; (M^{\ast}_1,H^{\ast}_1,R^{\ast}_2)=(\frac{1}{2}+\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,0)\\
    &amp; (M^{\ast}_1,H^{\ast}_1,R^{\ast}_3)=(\frac{1}{2}+\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,1-\frac{a_7}{a_5})\\
    &amp; (M^{\ast}_3,H^{\ast}_2,R^{\ast}_1)=\frac{1}{2}(\frac{a_2}{a_1}H_2^{\ast}-1)+ \sqrt{\frac{1}{4}\cdot(\frac{a_2}{a_1}H_2^{\ast}-1)^2+\frac{1}{a_1}},\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6},\frac{a_4}{a_3})
\end{align}\]

<p>This is the case under the following conditions:</p>

<ul>
  <li>Fixed point \((M^{*}_1,H^{*}_1,R^{*}_2)\) is feasible for every \(a_i&gt;0\).</li>
  <li>Fixed point \((M^{*}_1,H^{*}_1,R^{*}_3)\) is feasible for \(a_i&gt;0\) , except for \(R_2\) which is only feasible if \(\frac{a_7}{a_5} \leq 1\).</li>
  <li>Fixed point \((M^{*}_3,H^{*}_2,R^{*}_1)\), parameters \(a_3,a_4,a_5\) and \(a_7\) in \(H_2\) needs to be in a certain ratio to be feasible since \(H_2\) must be positive. Consequently, the following sufficient condition can be derived:</li>
</ul>

\[H^*_2=\frac{a_5 a_3-a_4 a_5-a_3 a_7}{a_3 a_6}\]

<p>For \(H^*_2\) to be $&gt;0$, the numerator of \(H^*_2\) must be positive:</p>

\[\begin{align}
    &amp; a_5 a_3-a_4 a_5-a_3 a_7&gt;0 \\
    &amp; a_4 a_5+a_3 a_7&lt;a_5 a_3 \\
    &amp; \frac{a_4 a_5+a_3 a_7}{a_5 a_3}&lt;1 \\
    &amp; \frac{a_4}{a_3}+\frac{a_7}{a_5}&lt;1
\end{align}
\notag\]

<p>Given that $\frac{a_4}{a_3}+\frac{a_7}{a_5}&lt;1$ is a sufficient condition to guarantee the feasibility of \(H^*_2\), it will automatically be a sufficient condition for \(R^*_2\) because \(\frac{a_7}{a_5}\leq 1\) is true if \(\frac{a_4}{a_3}+\frac{a_7}{a_5}&lt;1\) is true. Consequently, all the fixed points stated in \autoref{eq:feasible_fixed} are feasible due to this sufficient condition.<br />
Hence, the dynamical system has three fixed points which can be described in the following form:</p>

\[E_1^*=\left(M_1^*, 0,0\right), \quad E_2^*=\left(M_2^*, 0, R_2^*\right), \quad E_3^*=\left(M_3^*, H_3^*, R_3^*\right)\]

<p>These fixed points can be biological interpreted as follows.</p>

<ul>
  <li>
    <p>\(E_1^*\) The density of Malignant cells $\dot{M}$ can only be externally influenced by the Hunting cells due to the rate of destruction of tumor cells $a_2$. This specific fixed point occurs when H and R are zero, meaning that this is the maximum malignant cell density that is constant and since $M=0.1$ is considered reasonable, this fixed point can therefore be labeled as dangerous. In this case the organism does not have an active immune system.</p>
  </li>
  <li>
    <p>\(E_2^*\) The dynamical behavior of the Resting cells and the behavior of the Malignant cells do not directly depend on each other. they are coupled via the density of hunting cells. At the fixed point, the resting cells grow as quickly as they die. The conversion rate $a_6$ from hunting cells to resting cells and resting cells to hunting cells $a_3$ will only be activated when the organism has hunting cells to begin with.</p>
  </li>
  <li>
    <p>\(E_3^*\) Every point in set $\mathcal{P}$ which has a density bigger than zero, will converge to this fixed point. The hunting cells hunt down the Malignant cells with a destruction rate of $a_2$. The hunting cells will grow because of the conversion rate from resting cells to hunting cells $a_3$. Depending on the amount of initial resting cells, the hunting cells will increase rapidly until they reach a maximum due to the natural death rate $a_4$. At this point the Hunting cells will be converted back into Resting cells due to the conversion rate from Hunting cells to Resting cells $a_6$. After this process, a steady state is reached where the malignant cells are within a reasonable amount. Biologically, this is the fixed point that occurs in a healthy immune system that naturally declines malignant cell growth.</p>
  </li>
</ul>

<h1 id="simulation">Simulation</h1>
<p>The three conditions provided determine the numerical values of the coefficients \(a_1,...,a_7\). These numerical values are as follows.</p>

<table>
  <thead>
    <tr>
      <th> </th>
      <th>\(a_1\)</th>
      <th>\(a_2\)</th>
      <th>\(a_3\)</th>
      <th>\(a_4\)</th>
      <th>\(a_5\)</th>
      <th>\(a_6\)</th>
      <th>\(a_7\)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Condition 1</td>
      <td>$4.5$</td>
      <td>$1$</td>
      <td>$0.5$</td>
      <td>$0.4$</td>
      <td>$0.7$</td>
      <td>$0.2$</td>
      <td>$0.4$</td>
    </tr>
    <tr>
      <td>Condition 2</td>
      <td>$4.5$</td>
      <td>$1$</td>
      <td>$5.0$</td>
      <td>$0.4$</td>
      <td>$0.7$</td>
      <td>$0.1$</td>
      <td>$0.1$</td>
    </tr>
    <tr>
      <td>Condition 3</td>
      <td>$4.5$</td>
      <td>$1$</td>
      <td>$4.8$</td>
      <td>$0.4$</td>
      <td>$3.7$</td>
      <td>$1.9$</td>
      <td>$0.1$</td>
    </tr>
  </tbody>
</table>

<p>For every condition, time simulations will be shown within the range of $t = (0,100) \;[s]$ and the initial conditions $(M_0,H_0,R_0)$ are chosen to be a linear spacing between 0 and 2 with a step of 0.5 and extra points of interest around fixed points.</p>

<table>
  <thead>
    <tr>
      <th> </th>
      <th>\(x_1\)</th>
      <th>\(x_2\)</th>
      <th>\(x_3\)</th>
      <th>\(x_4\)</th>
      <th>\(x_5\)</th>
      <th>\(x_6\)</th>
      <th>\(x_7\)</th>
      <th>\(x_8\)</th>
      <th>\(x_9\)</th>
      <th>\(x_{10}\)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Malignant cell density</td>
      <td>$0$</td>
      <td>$0.5$</td>
      <td>$1$</td>
      <td>$1.5$</td>
      <td>$2.0$</td>
      <td>$1.0$</td>
      <td>$4.0$</td>
      <td>$4.0$</td>
      <td>$4.0$</td>
      <td>$4.0$</td>
    </tr>
    <tr>
      <td>Hunting cell density</td>
      <td>$0$</td>
      <td>$0.5$</td>
      <td>$1$</td>
      <td>$1.5$</td>
      <td>$2.0$</td>
      <td>$0$</td>
      <td>$0$</td>
      <td>$0.1$</td>
      <td>$0.1$</td>
      <td>$0.1$</td>
    </tr>
    <tr>
      <td>Resting cell density</td>
      <td>$0$</td>
      <td>$0.5$</td>
      <td>$1$</td>
      <td>$1.5$</td>
      <td>$2.0$</td>
      <td>$2.0$</td>
      <td>$0$</td>
      <td>$0.1$</td>
      <td>$0.5$</td>
      <td>$2.0$</td>
    </tr>
  </tbody>
</table>

<p>These initial conditions yield a solid exploration of the vector field, without cluttering the output. For every condition, the same initial conditions are being used to be able to conclude differences</p>

<h2 id="condition-1">Condition 1</h2>

<p><img src="/assets/images/modeling-dynamics/condition1.png" alt="condition13d" /></p>

<p><img src="/assets/images/modeling-dynamics/condition1_2d.png" alt="condition12d" /></p>

<p>Condition 1 inherent coefficients that are by definition not biologically feasible, because the \(\frac{a_4}{a_3}+\frac{a_7}{a_5} &lt; 1\) inequality does not hold. This implies that condition 1 is not possible and it therefore also causes a fixed point to be excluded from the set $\mathcal{P}$. If the set of conditions were possible, tumor dormancy can be concluded, because a steady state is reached where the malignant, hunting and resting cells remain constant and where the malignant cells are $&gt;0.1$.</p>

<h2 id="condition-2">Condition 2</h2>

<p><img src="/assets/images/modeling-dynamics/condition2.png" alt="condition23d" /></p>

<p><img src="/assets/images/modeling-dynamics/condition2_2d.png" alt="condition22d" /></p>

<p>The time trajectories of initial condition \(x_0(1)\) and \(x_0(7)\) converge towards fixed point \(E^*_1\) and the trajectories of initial conditions \(x_0(6)\) converges towards fixed point \(E^*_2\), these are special cases and for that reason also hand picked, because it shows that once the tuple \((M,H,R)\) starts on the M axis, it stays on the M axis. Similarly, once $(M,H,R)$ starts on the $(M,0,R)$ plane it converges towards fixed point \(E^*_2\). Every other initial condition spirals towards the fixed point \(E^*_3\), which implies tumor remission in a cyclic pattern.</p>

<h2 id="condition-3">Condition 3</h2>

<p><img src="/assets/images/modeling-dynamics/condition3.png" alt="condition33d" /></p>

<p><img src="/assets/images/modeling-dynamics/condition3_2d.png" alt="condition32d" /></p>

<p>For condition 3 the coefficients $a_5$ and $a_6$ are considerably larger which implies that the growth rate of resting cells have increased as well as the conversion rate from hunting to resting cells. With condition 3, less hunting cells will be available due to the $HR$ term that acts as a balance. The rate of change of hunting cells will therefore overall be smaller that means that the density of hunting cells will overall reach smaller values compared to condition 2. The time of convergence is approximately 6 times faster than condition 2, but the malignant cells reach steady state at a higher value. For condition 3, tumor remission with a cyclic pattern can be concluded.</p>

<h1 id="stability-and-contractive-properties">Stability and contractive properties</h1>

<p>For stability analysis, the dynamical system will be linearised. Therefore the <a href="https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant">Jacobian matrix</a> must be computed:</p>

\[(M, H, R)=(x_1, x_2, x_3)\]

\[\dot{x_1}=f_1\left(x_1, x_2, x_3\right) \\
\dot{x_2}=f_2\left(x_1, x_2, x_3\right)\\
\dot{x_3}=f_3\left(x_1, x_1, x_3\right)\]

\[\left.
A=
\begin{bmatrix}

   \frac{\partial f_1}{\partial x_1} &amp; \frac{\partial f_1}{\partial x_2}  &amp; \frac{\partial f_1}{\partial x_3} \\
   \frac{\partial f_2}{\partial x_1}  &amp; \frac{\partial f_2}{\partial x_2}  &amp; \frac{\partial f_2}{\partial x_3} \\
   \frac{\partial f_3}{\partial x_1}  &amp; \frac{\partial f_3}{\partial x_2}  &amp; \frac{\partial f_3}{\partial x_3} 
\end{bmatrix} \right\vert_{(x_1^{*},x_2^{*},x_3^{*})}\]

\[A=
\begin{bmatrix}

   a_1-2a_1M-a_2H &amp; -a2M  &amp; 0 \\
   0 &amp; a3R-a_4  &amp; a_3H \\
   0 &amp; -a_6R  &amp; a_5-2a_5R-a_6H-a_7 \\
\end{bmatrix}\]

<p><img src="/assets/images/modeling-dynamics/stability_fp.png" alt="stability_fp" /></p>

<h2 id="lyapunov-stability">Lyapunov stability</h2>

<p>The goal is to find a suitable matrix \(P=P^{\top}\) such that the quadratic function</p>

\[V(M(t), H(t), R(t))=\left(\begin{array}{c}
M(t)-M_3^* \\
H(t)-H_3^* \\
R(t)-R_3^*
\end{array}\right)^{\top} P\left(\begin{array}{c}
M(t)-M_3^* \\
H(t)-H_3^* \\
R(t)-R_3^*
\end{array}\right)\]

<p>serves as a Lyapanov function to prove stability of the fixed point \(E^*_3\) under condition 3.The suitable value of P can now be solved using convex optimization techniques. For this reason, a mathematical optimization toolbox in Matlab called <a href="https://yalmip.github.io/">Yalmip</a> is used to solve semidefinite programming problems using <a href="https://en.wikipedia.org/wiki/MOSEK">Mosek</a> as a mathematical solver. To following constraints are considered:</p>

<ul>
  <li>$P=P^{\top}\succ 0$</li>
  <li>$A^{\top} P + P A \prec 0$</li>
</ul>

<p>Implementing these constraints gave the following result:</p>

\[P=\begin{bmatrix}
0.0855 &amp; 0.0150 &amp; 0.0016\\
0.0150 &amp; 0.0214 &amp; 0.0170\\
0.0016 &amp; 0.0170 &amp; 0.8931
\end{bmatrix}\]

<p>The <a href="https://en.wikipedia.org/wiki/Lyapunov_function">Lyapanov function</a> for this specific $P$ holds since $V(x^*)=0$ and $V(x)’&lt;0$. Now that the Lyapanov function is computed, the maximum $\gamma$ in set $\mathcal{V_\gamma}$ can be determined.</p>

\[\mathcal{V}_\gamma:=\left\{(M, H, R) \in \mathbb{R}^3 \mid V(M, H, R) \leq \gamma\right\}\]

<p>The maximum level set $\gamma$ can be found via optimization. Here the objective is to maximize $\gamma$ using the following constraints:</p>

<ul>
  <li>$\left(\begin{array}{c}
M(t)-M_3^* <br />
H(t)-H_3^* <br />
R(t)-R_3^*
\end{array}\right)^{\top} P\left(\begin{array}{c}
M(t)-M_3^* <br />
H(t)-H_3^* <br />
R(t)-R_3^*
\end{array}\right) \leq \gamma$</li>
  <li>$M(t),H(t),R(t) &gt; 0$</li>
  <li>$\gamma&gt;0$</li>
</ul>

<p>Level sets of the 4-D Lyapunov function can be interpreted as spheres where the maximum $\gamma$ will barely touch the boundaries of set $\mathcal{P}$, since initial conditions outside of the set $\mathcal{P}$ or on the $M,R$ plane won’t converge to $E_3$. The maximum value for the level set of $V(M(t),H(t),R(t))$ and for whom all initial trajectories on this level set still converge is $\gamma_{max}=3.42$.</p>

<h1 id="time-delayed-system">Time delayed system</h1>

<p>The locations of the fixed points themselves do not change due to the time delay, because these fixed points are solely dependent on the ratio between coefficients $(a_1, … ,a_7)$ and assume all time dependant behavior have died out i.e. the system is not moving $(\dot{M},\dot{H},\dot{R}) = (0,0,0)$. However, the behavior around these fixed points do change with respect to the time delay.</p>

<p>One <a href="https://en.wikipedia.org/wiki/Bifurcation_theory">bifurcation</a> appears between $\tau = 0.2$ and $\tau = 0.3$ for which the fixed point is not an attractive fixed point since the trajectory turns into a <a href="https://en.wikipedia.org/wiki/Limit_cycle">limit cycle</a> which orbits around a specific circumference. The higher the time delay the larger this circumference. This bifurcation can be classified by the <a href="https://en.wikipedia.org/wiki/Hopf_bifurcation">Hopf bifurcation</a>.</p>

<p><img src="/assets/images/modeling-dynamics/time_delayed_3D.png" alt="timedelayed3d" />
<img src="/assets/images/modeling-dynamics/time_delayed.png" alt="timedelayed2d" /></p>

<h1 id="conclusion--discussion">Conclusion &amp; Discussion</h1>
<h2 id="conclusion">Conclusion</h2>
<p>Considerable insight into the dynamic behavior of the immune system has been obtained. Depending on the clinical conditions $a_1,…a_7$ biological feasibility can be ensured. For all conditions, the biologically feasible fixed points where asymptotically stable. The only case where asymptotic stability was not ensured is for the time delayed system. In that case, the response is always delayed which results in a limit cycle. The behavior change from asymptotic stability into a limit cycle can be classified as a Hopf bifurcation.</p>

<h2 id="discussion">Discussion</h2>
<p>Stabilization of the system is not necessary, because the system itself is already asymptotically stable. However, if the dynamics change over time the complete system can be stabilized by linearising around an operating point. Converting the dynamic model into state-space format and check whether the state space is <a href="https://en.wikipedia.org/wiki/Controllability#:~:text=admissible%20system%20behavior.-,Stabilizability,made%20to%20have%20stable%20dynamics.">stabilizable</a>. Define a control law $\omega = -K\xi$ for which $A-BK$ is <a href="https://en.wikipedia.org/wiki/Hurwitz_matrix">Hurwitz</a> to obtain asymptotic stability.</p>

<h1 id="software">Software</h1>
<h2 id="main-code">Main code</h2>
<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">%% 5CSA0 Tumor growth assignment</span>
<span class="nb">clc</span><span class="p">,</span> <span class="nb">clear</span> <span class="nb">all</span><span class="p">,</span> <span class="nb">close</span> <span class="nb">all</span>

<span class="c1">%% Fixed points</span>
<span class="n">syms</span> <span class="n">M</span> <span class="n">H</span> <span class="n">R</span> <span class="n">a1</span> <span class="n">a2</span> <span class="n">a3</span> <span class="n">a4</span> <span class="n">a5</span> <span class="n">a6</span> <span class="n">a7</span>

<span class="n">f1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">+</span><span class="n">a1</span><span class="o">*</span><span class="n">M</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">M</span><span class="p">)</span><span class="o">-</span><span class="n">a2</span><span class="o">*</span><span class="n">M</span><span class="o">*</span><span class="n">H</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">f2</span> <span class="o">=</span> <span class="n">a3</span><span class="o">*</span><span class="n">H</span><span class="o">*</span><span class="n">R</span><span class="o">-</span><span class="n">a4</span><span class="o">*</span><span class="n">H</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">f3</span> <span class="o">=</span> <span class="n">a5</span> <span class="o">*</span> <span class="n">R</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">R</span><span class="p">)</span> <span class="o">-</span><span class="n">a6</span><span class="o">*</span><span class="n">H</span><span class="o">*</span><span class="n">R</span><span class="o">-</span><span class="n">a7</span><span class="o">*</span><span class="n">R</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">solved</span> <span class="o">=</span> <span class="n">solve</span><span class="p">([</span><span class="n">f1</span><span class="p">,</span><span class="n">f2</span><span class="p">,</span><span class="n">f3</span><span class="p">],[</span><span class="n">M</span><span class="p">,</span><span class="n">H</span><span class="p">,</span><span class="n">R</span><span class="p">]);</span>
<span class="c1">% Solve system of equations to find fixed points</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">solved</span><span class="o">.</span><span class="n">H</span><span class="p">)</span>
<span class="n">xstar</span> <span class="o">=</span> <span class="p">[</span><span class="n">solved</span><span class="o">.</span><span class="n">M</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="n">solved</span><span class="o">.</span><span class="n">H</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="n">solved</span><span class="o">.</span><span class="n">R</span><span class="p">(</span><span class="n">i</span><span class="p">)];</span>      
<span class="n">xstar_mat</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="mi">1</span><span class="p">}</span> <span class="o">=</span> <span class="n">xstar</span><span class="p">;</span>
<span class="k">end</span>
<span class="c1">% Reorder to be the same as in the report</span>
<span class="n">xstar_mat</span><span class="p">([</span><span class="mi">1</span> <span class="mi">2</span><span class="p">],:)</span> <span class="o">=</span> <span class="n">xstar_mat</span><span class="p">([</span><span class="mi">2</span> <span class="mi">1</span><span class="p">],:);</span>
<span class="n">xstar_mat</span><span class="p">([</span><span class="mi">3</span> <span class="mi">5</span><span class="p">],:)</span> <span class="o">=</span> <span class="n">xstar_mat</span><span class="p">([</span><span class="mi">5</span> <span class="mi">3</span><span class="p">],:);</span>

<span class="c1">%% Check biological feasibility</span>
<span class="c1">% For all conditions, positive valued states must be achieved at the fixed points</span>
<span class="n">cd1</span> <span class="o">=</span> <span class="p">[</span><span class="mf">4.5</span> <span class="mi">1</span> <span class="mf">0.5</span> <span class="mf">0.4</span> <span class="mf">0.7</span> <span class="mf">0.2</span> <span class="mf">0.4</span><span class="p">];</span>
<span class="n">cd2</span> <span class="o">=</span> <span class="p">[</span><span class="mf">3.5</span> <span class="mi">1</span> <span class="mf">5.0</span> <span class="mf">0.4</span> <span class="mf">0.7</span> <span class="mf">0.1</span> <span class="mf">0.1</span><span class="p">];</span>
<span class="n">cd3</span> <span class="o">=</span> <span class="p">[</span><span class="mf">3.0</span> <span class="mi">1</span> <span class="mf">4.8</span> <span class="mf">0.4</span> <span class="mf">3.7</span> <span class="mf">1.9</span> <span class="mf">0.1</span><span class="p">];</span>
<span class="nb">cd</span> <span class="o">=</span> <span class="p">[</span><span class="n">cd1</span><span class="p">;</span><span class="n">cd2</span><span class="p">;</span><span class="n">cd3</span><span class="p">];</span>
<span class="n">coef</span> <span class="o">=</span> <span class="p">[</span><span class="n">a1</span> <span class="n">a2</span> <span class="n">a3</span> <span class="n">a4</span> <span class="n">a5</span> <span class="n">a6</span> <span class="n">a7</span><span class="p">];</span>

<span class="c1">% Check biological feasibility of the conditions</span>
<span class="n">f4</span> <span class="o">=</span> <span class="p">(</span><span class="n">a4</span><span class="p">/</span><span class="n">a3</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">a7</span><span class="p">/</span><span class="n">a5</span><span class="p">);</span> <span class="c1">% Constraint to be biologically feasible</span>
<span class="k">for</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="nb">cd</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">subs</span><span class="p">(</span><span class="n">f4</span><span class="p">,</span><span class="n">coef</span><span class="p">,</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,:))</span> <span class="o">&lt;</span> <span class="mi">1</span>
    <span class="n">feasiblity</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="k">else</span>
    <span class="n">feasiblity</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">end</span>
<span class="k">end</span>

<span class="c1">% Check all fixed point values with conditions</span>
<span class="k">for</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="nb">cd</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">xstar_mat</span><span class="p">)</span>
    <span class="n">sol_xstar</span> <span class="o">=</span> <span class="nb">double</span><span class="p">(</span><span class="n">subs</span><span class="p">(</span><span class="n">xstar_mat</span><span class="p">{</span><span class="n">i</span><span class="p">},</span><span class="n">coef</span><span class="p">,</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,:)));</span>
    <span class="n">sol_xstar_mat</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}</span> <span class="o">=</span> <span class="n">sol_xstar</span><span class="p">;</span>
    <span class="k">end</span>
<span class="k">end</span>

<span class="c1">% Remove negative fixed points</span>
<span class="n">sol_xstar_mat_reduced</span> <span class="o">=</span> <span class="n">sol_xstar_mat</span><span class="p">([</span><span class="mi">1</span> <span class="mi">3</span> <span class="mi">5</span><span class="p">],:);</span>

<span class="c1">% Stability check of the biological feasible fixed points</span>
<span class="n">syms</span> <span class="n">f1_lin</span> <span class="n">f2_lin</span> <span class="n">f3_lin</span> 
<span class="n">f1_lin</span> <span class="o">=</span> <span class="mi">1</span><span class="o">+</span><span class="n">a1</span><span class="o">*</span><span class="n">M</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">M</span><span class="p">)</span><span class="o">-</span><span class="n">a2</span><span class="o">*</span><span class="n">M</span><span class="o">*</span><span class="n">H</span><span class="p">;</span> 
<span class="n">f2_lin</span> <span class="o">=</span> <span class="n">a3</span><span class="o">*</span><span class="n">H</span><span class="o">*</span><span class="n">R</span><span class="o">-</span><span class="n">a4</span><span class="o">*</span><span class="n">H</span><span class="p">;</span> 
<span class="n">f3_lin</span> <span class="o">=</span> <span class="n">a5</span> <span class="o">*</span> <span class="n">R</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">R</span><span class="p">)</span> <span class="o">-</span><span class="n">a6</span><span class="o">*</span><span class="n">H</span><span class="o">*</span><span class="n">R</span><span class="o">-</span><span class="n">a7</span><span class="o">*</span><span class="n">R</span><span class="p">;</span>
<span class="n">A</span> <span class="o">=</span> <span class="n">jacobian</span><span class="p">([</span><span class="n">f1_lin</span><span class="p">,</span><span class="n">f2_lin</span><span class="p">,</span><span class="n">f3_lin</span><span class="p">],[</span><span class="n">M</span><span class="p">,</span><span class="n">H</span><span class="p">,</span><span class="n">R</span><span class="p">]);</span>

<span class="c1">% Loop over the coditions (k) and loop over the biologically feasible fixed</span>
<span class="c1">% points (i)</span>
<span class="k">for</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="nb">cd</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">sol_xstar_mat_reduced</span><span class="p">(</span><span class="mi">1</span><span class="p">,:))</span>
    <span class="n">A_stab</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}</span> <span class="o">=</span> <span class="nb">eig</span><span class="p">(</span><span class="n">subs</span><span class="p">(</span><span class="n">A</span><span class="p">,[</span><span class="n">M</span> <span class="n">H</span> <span class="n">R</span><span class="p">],[</span><span class="n">sol_xstar_mat_reduced</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}]));</span>
    <span class="n">A_stab_numeric</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}</span> <span class="o">=</span> <span class="nb">double</span><span class="p">(</span><span class="nb">eig</span><span class="p">(</span><span class="n">subs</span><span class="p">(</span><span class="n">A</span><span class="p">,[</span><span class="n">M</span><span class="p">,</span><span class="n">H</span><span class="p">,</span><span class="n">R</span><span class="p">,</span><span class="n">coef</span><span class="p">],[</span><span class="n">sol_xstar_mat_reduced</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">},</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,:)])));</span>
    <span class="k">end</span>
<span class="k">end</span>
<span class="c1">% All the eigen values per fixed point per condition in (i_(3x1),k) format</span>
<span class="n">A_eig</span> <span class="o">=</span> <span class="nb">cell2mat</span><span class="p">(</span><span class="n">A_stab_numeric</span><span class="p">);</span> 
<span class="n">A_num_cd3</span> <span class="o">=</span> <span class="nb">double</span><span class="p">(</span><span class="n">subs</span><span class="p">(</span><span class="n">A</span><span class="p">,[</span><span class="n">M</span> <span class="n">H</span> <span class="n">R</span> <span class="n">coef</span><span class="p">],[</span><span class="n">sol_xstar_mat_reduced</span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">}</span> <span class="nb">cd</span><span class="p">(</span><span class="mi">3</span><span class="p">,:)]));</span>

<span class="c1">%% Simulation</span>
<span class="n">t</span> <span class="o">=</span> <span class="mi">0</span><span class="p">:</span><span class="mf">0.01</span><span class="p">:</span><span class="mi">100</span><span class="p">;</span>
<span class="c1">% Define initial conditions with first a linear spacing and then some</span>
<span class="c1">% arbitrary points of interest</span>
<span class="n">x0_1</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">0</span><span class="p">:</span><span class="mf">0.5</span><span class="p">:</span><span class="mi">2</span><span class="p">)</span><span class="s1">';1;4;4  ;4;4]'</span><span class="p">;</span>
<span class="n">x0_2</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">0</span><span class="p">:</span><span class="mf">0.5</span><span class="p">:</span><span class="mi">2</span><span class="p">)</span><span class="s1">';0;0;0.1;0.1;0.1]'</span><span class="p">;</span>
<span class="n">x0_3</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">0</span><span class="p">:</span><span class="mf">0.5</span><span class="p">:</span><span class="mi">2</span><span class="p">)</span><span class="s1">';2;0;0.1;0.5;2]'</span><span class="p">;</span>
<span class="n">scale_factor</span> <span class="o">=</span> <span class="mf">0.02</span><span class="p">;</span> 
<span class="n">x0</span> <span class="o">=</span> <span class="p">[</span><span class="n">x0_1</span><span class="p">;</span><span class="n">x0_2</span><span class="p">;</span><span class="n">x0_3</span><span class="p">];</span>
<span class="p">[</span><span class="n">x1</span><span class="p">,</span><span class="n">x2</span><span class="p">,</span><span class="n">x3</span><span class="p">]</span> <span class="o">=</span> <span class="nb">meshgrid</span><span class="p">(</span><span class="mi">0</span><span class="p">:</span><span class="mf">0.25</span><span class="p">:</span><span class="mi">2</span><span class="p">);</span>

<span class="c1">%%% Phase plot %%%</span>

<span class="k">for</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="nb">cd</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">p</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">x0</span><span class="p">)</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">cell_interaction</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">x0</span><span class="p">(:,</span><span class="n">p</span><span class="p">),</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,:));</span>
        <span class="n">solx</span><span class="p">{</span><span class="n">p</span><span class="p">,</span><span class="n">k</span><span class="p">}</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">(:,</span><span class="mi">1</span><span class="p">)</span> <span class="n">x</span><span class="p">(:,</span><span class="mi">2</span><span class="p">)</span> <span class="n">x</span><span class="p">(:,</span><span class="mi">3</span><span class="p">)];</span>
    <span class="k">end</span>
    <span class="c1">% 3D phase plot</span>
    <span class="n">x1dot</span> <span class="o">=</span> <span class="mi">1</span><span class="o">+</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span><span class="o">.*</span><span class="n">x1</span><span class="o">.*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">x1</span><span class="p">)</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span><span class="o">.*</span><span class="n">x1</span><span class="o">.*</span><span class="n">x2</span><span class="p">;</span> 
    <span class="n">x2dot</span><span class="o">=</span> <span class="p">(</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span><span class="o">.*</span><span class="n">x2</span><span class="o">.*</span><span class="n">x3</span><span class="p">)</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span><span class="o">.*</span><span class="n">x2</span><span class="p">;</span> 
    <span class="n">x3dot</span><span class="o">=</span> <span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span><span class="o">.*</span><span class="n">x3</span><span class="o">.*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">x3</span><span class="p">)</span><span class="o">-</span><span class="p">(</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="mi">6</span><span class="p">)</span><span class="o">.*</span><span class="n">x2</span><span class="o">.*</span><span class="n">x3</span><span class="p">)</span><span class="o">-</span><span class="p">(</span><span class="nb">cd</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="mi">7</span><span class="p">)</span><span class="o">.*</span><span class="n">x3</span><span class="p">);</span> 
    <span class="nb">figure</span><span class="p">()</span>
    <span class="nb">quiver3</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span><span class="n">x2</span><span class="p">,</span><span class="n">x3</span><span class="p">,</span><span class="n">x1dot</span><span class="o">*</span><span class="n">scale_factor</span><span class="p">,</span><span class="n">x2dot</span><span class="o">*</span><span class="n">scale_factor</span><span class="p">,</span><span class="n">x3dot</span><span class="o">*</span><span class="n">scale_factor</span><span class="p">,</span><span class="s1">'autoscale'</span><span class="p">,</span><span class="s1">'off'</span><span class="p">)</span>
    <span class="nb">grid</span> <span class="n">on</span><span class="p">;</span> <span class="nb">axis</span> <span class="nb">image</span><span class="p">;</span>
    <span class="nb">xlabel</span><span class="p">(</span><span class="s1">'Malignant cells density'</span><span class="p">)</span>
    <span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Hunting cells density'</span><span class="p">)</span>
    <span class="nb">zlabel</span><span class="p">(</span><span class="s1">'Resting cells density'</span><span class="p">)</span>
<span class="k">end</span>

<span class="c1">%%% Time plot %%%</span>

<span class="k">for</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="nb">cd</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
<span class="nb">figure</span><span class="p">()</span>
<span class="nb">subplot</span><span class="p">(</span><span class="mi">311</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">solx</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="nb">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">solx</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="nb">hold</span> <span class="n">on</span><span class="p">;</span> <span class="nb">grid</span> <span class="n">on</span>
    <span class="k">end</span>
    <span class="n">str</span> <span class="o">=</span> <span class="nb">sprintf</span><span class="p">(</span><span class="s2">"States for condition %d and for all intital conditions"</span><span class="p">,</span><span class="n">k</span><span class="p">);</span>
    <span class="nb">title</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
    <span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Density of malignant cells'</span><span class="p">)</span>
    <span class="nb">subplot</span><span class="p">(</span><span class="mi">312</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">solx</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="nb">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">solx</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}(:,</span><span class="mi">2</span><span class="p">))</span>
    <span class="nb">hold</span> <span class="n">on</span><span class="p">;</span> <span class="nb">grid</span> <span class="n">on</span>
    <span class="k">end</span>

    <span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Density of Hunting cells'</span><span class="p">)</span>
    <span class="nb">subplot</span><span class="p">(</span><span class="mi">313</span><span class="p">)</span>
    
    <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">solx</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="nb">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">solx</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}(:,</span><span class="mi">3</span><span class="p">))</span>
    <span class="nb">hold</span> <span class="n">on</span><span class="p">;</span> <span class="nb">grid</span> <span class="n">on</span>
    <span class="n">legends</span><span class="p">{</span><span class="n">i</span><span class="p">}</span> <span class="o">=</span> <span class="nb">sprintf</span><span class="p">(</span><span class="s1">'X0(%d)'</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span>
    <span class="k">end</span>

    <span class="nb">xlabel</span><span class="p">(</span><span class="s1">'Time [s]'</span><span class="p">)</span>
    <span class="nb">legend</span><span class="p">(</span><span class="n">legends</span><span class="p">)</span>
    <span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Density of Resting cells'</span><span class="p">)</span>
    <span class="n">plotTweak</span><span class="p">();</span>
<span class="k">end</span>

<span class="c1">%%% 3D trajetory plot %%%</span>

<span class="k">for</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="nb">cd</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="nb">figure</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">solx</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
    <span class="nb">plot3</span><span class="p">(</span><span class="n">solx</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}(:,</span><span class="mi">1</span><span class="p">),</span><span class="n">solx</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}(:,</span><span class="mi">2</span><span class="p">),</span><span class="n">solx</span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="n">k</span><span class="p">}(:,</span><span class="mi">3</span><span class="p">))</span>
    <span class="nb">hold</span> <span class="n">on</span>
    <span class="k">end</span>
    <span class="nb">plot3</span><span class="p">(</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">1</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">2</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">3</span><span class="p">),</span><span class="s1">'r.'</span><span class="p">,</span> <span class="s1">'MarkerSize'</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
    <span class="nb">plot3</span><span class="p">(</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">1</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">2</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">3</span><span class="p">),</span><span class="s1">'r.'</span><span class="p">,</span> <span class="s1">'MarkerSize'</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
    <span class="nb">plot3</span><span class="p">(</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">1</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">2</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="n">k</span><span class="p">}(</span><span class="mi">3</span><span class="p">),</span><span class="s1">'r.'</span><span class="p">,</span> <span class="s1">'MarkerSize'</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
    <span class="nb">grid</span> <span class="n">on</span>
    <span class="nb">xlabel</span><span class="p">(</span><span class="s1">'Malignant cells density'</span><span class="p">)</span>
    <span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Hunting cells density'</span><span class="p">)</span>
    <span class="nb">zlabel</span><span class="p">(</span><span class="s1">'Resting cells density'</span><span class="p">)</span>
    <span class="n">plotTweak</span><span class="p">();</span>
<span class="k">end</span>


<span class="c1">%% Time delayed system Q8</span>
<span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">:</span><span class="mf">0.01</span><span class="p">:</span><span class="mi">100</span><span class="p">);</span>
<span class="c1">% delay_bif = 0.84932; % Include to see crash bifurcation</span>
<span class="n">delay_span</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.1</span><span class="p">:</span><span class="mf">0.1</span><span class="p">:</span><span class="mf">0.8</span><span class="p">)</span><span class="o">'</span><span class="p">;</span>
<span class="n">delay_span</span> <span class="o">=</span> <span class="p">[</span><span class="n">delay_span</span><span class="p">];</span> <span class="c1">%[delay_span;delay_bif];</span>
<span class="n">delay</span> <span class="o">=</span> <span class="p">[</span><span class="n">delay_span</span> <span class="n">delay_span</span> <span class="n">delay_span</span><span class="p">];</span>
<span class="c1">% delay = [0.01 0.01 0.01]'</span>
<span class="n">x0</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span><span class="p">]</span><span class="o">'</span><span class="p">;</span>
<span class="c1">% delay = [0.1 0.1 0.1];</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">delay</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
<span class="n">sol</span> <span class="o">=</span> <span class="n">d_cell_interaction</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">delay</span><span class="p">(</span><span class="n">i</span><span class="p">,:),</span><span class="n">x0</span><span class="p">,</span><span class="nb">cd</span><span class="p">(</span><span class="mi">3</span><span class="p">,:));</span>
<span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}</span> <span class="o">=</span> <span class="p">[</span><span class="n">sol</span><span class="o">.</span><span class="n">x</span><span class="p">;</span><span class="n">sol</span><span class="o">.</span><span class="n">y</span><span class="p">];</span>
<span class="k">end</span>

<span class="nb">figure</span><span class="p">;</span> <span class="nb">clear</span> <span class="n">legends</span>
<span class="k">for</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">delay</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
<span class="nb">plot3</span><span class="p">(</span><span class="n">sol_data</span><span class="p">{</span><span class="n">k</span><span class="p">}(</span><span class="mi">2</span><span class="p">,:),</span><span class="n">sol_data</span><span class="p">{</span><span class="n">k</span><span class="p">}(</span><span class="mi">3</span><span class="p">,:),</span><span class="n">sol_data</span><span class="p">{</span><span class="n">k</span><span class="p">}(</span><span class="mi">4</span><span class="p">,:))</span>
<span class="nb">xlabel</span><span class="p">(</span><span class="s1">'Malignant cells density'</span><span class="p">)</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Hunting cells density'</span><span class="p">)</span>
<span class="nb">zlabel</span><span class="p">(</span><span class="s1">'Resting cells density'</span><span class="p">)</span>
<span class="nb">hold</span> <span class="n">on</span>
<span class="k">end</span>
<span class="nb">plot3</span><span class="p">(</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">1</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">2</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">3</span><span class="p">),</span><span class="s1">'r.'</span><span class="p">,</span> <span class="s1">'MarkerSize'</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
<span class="nb">plot3</span><span class="p">(</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">1</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">2</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">3</span><span class="p">),</span><span class="s1">'r.'</span><span class="p">,</span> <span class="s1">'MarkerSize'</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
<span class="nb">plot3</span><span class="p">(</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">1</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">2</span><span class="p">),</span><span class="n">sol_xstar_mat</span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">}(</span><span class="mi">3</span><span class="p">),</span><span class="s1">'r.'</span><span class="p">,</span> <span class="s1">'MarkerSize'</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>  
<span class="nb">legend</span><span class="p">(</span><span class="s1">'\tau = 0.1'</span><span class="p">,</span><span class="s1">'\tau = 0.2'</span><span class="p">,</span><span class="s1">'\tau = 0.3'</span><span class="p">,</span><span class="s1">'\tau = 0.4'</span><span class="p">,</span><span class="s1">'\tau = 0.5'</span><span class="p">,</span><span class="s1">'\tau = 0.6'</span><span class="p">,</span><span class="s1">'\tau = 0.7'</span><span class="p">,</span><span class="s1">'\tau = 0.8'</span><span class="p">,</span><span class="s1">'\tau bif.'</span><span class="p">)</span>
<span class="nb">grid</span> <span class="n">on</span>

<span class="nb">figure</span><span class="p">;</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">delay</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
<span class="nb">plot</span><span class="p">(</span><span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}(</span><span class="mi">1</span><span class="p">,:),[</span><span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}([</span><span class="mi">2</span> <span class="mi">4</span><span class="p">],:)])</span>
<span class="nb">title</span><span class="p">(</span><span class="s1">'Time response of \tau = 0.1.'</span><span class="p">);</span>
<span class="nb">xlabel</span><span class="p">(</span><span class="s1">'time t'</span><span class="p">);</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'solution y'</span><span class="p">);</span>
<span class="nb">legend</span><span class="p">(</span><span class="s1">'M'</span><span class="p">,</span><span class="s1">'H'</span><span class="p">,</span><span class="s1">'R'</span><span class="p">)</span>
<span class="nb">hold</span> <span class="n">on</span>
<span class="k">end</span>

<span class="nb">figure</span><span class="p">;</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">delay</span><span class="p">(:,</span><span class="mi">1</span><span class="p">))</span>
<span class="nb">subplot</span><span class="p">(</span><span class="mi">311</span><span class="p">)</span>
<span class="nb">plot</span><span class="p">(</span><span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}(</span><span class="mi">1</span><span class="p">,:),</span><span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}(</span><span class="mi">2</span><span class="p">,:))</span>
<span class="c1">% title('Time responses of (M,H,R) for different \tau');</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Malignant cell density'</span><span class="p">);</span>
<span class="nb">hold</span> <span class="n">on</span>
<span class="nb">grid</span> <span class="n">on</span>
<span class="nb">subplot</span><span class="p">(</span><span class="mi">312</span><span class="p">)</span>
<span class="nb">plot</span><span class="p">(</span><span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}(</span><span class="mi">1</span><span class="p">,:),</span><span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}(</span><span class="mi">3</span><span class="p">,:))</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Hunting  cell density'</span><span class="p">);</span>
<span class="nb">grid</span> <span class="n">on</span>
<span class="nb">hold</span> <span class="n">on</span>
<span class="nb">subplot</span><span class="p">(</span><span class="mi">313</span><span class="p">)</span>
<span class="nb">plot</span><span class="p">(</span><span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}(</span><span class="mi">1</span><span class="p">,:),</span><span class="n">sol_data</span><span class="p">{</span><span class="n">i</span><span class="p">}(</span><span class="mi">4</span><span class="p">,:))</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Resting cell density'</span><span class="p">);</span>
<span class="nb">grid</span> <span class="n">on</span>
<span class="nb">hold</span> <span class="n">on</span>
<span class="nb">xlabel</span><span class="p">(</span><span class="s1">'Time [s]'</span><span class="p">);</span>
<span class="k">end</span>
<span class="nb">legend</span><span class="p">(</span><span class="s1">'\tau = 0.1'</span><span class="p">,</span><span class="s1">'\tau = 0.2'</span><span class="p">,</span><span class="s1">'\tau = 0.3'</span><span class="p">,</span><span class="s1">'\tau = 0.4'</span><span class="p">,</span><span class="s1">'\tau = 0.5'</span><span class="p">,</span><span class="s1">'\tau = 0.6'</span><span class="p">,</span><span class="s1">'\tau = 0.7'</span><span class="p">,</span><span class="s1">'\tau = 0.8'</span><span class="p">)</span>

<span class="c1">%% Lyapunov function</span>
<span class="n">syms</span> <span class="n">x1</span> <span class="n">x2</span> <span class="n">x3</span> <span class="n">p11</span> <span class="n">p12</span> <span class="n">p13</span> <span class="n">p22</span> <span class="n">p23</span> <span class="n">p33</span>
<span class="n">Q</span> <span class="o">=</span> <span class="nb">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
<span class="n">x1_star</span> <span class="o">=</span> <span class="mf">0.8260</span><span class="p">;</span>
<span class="n">x2_star</span> <span class="o">=</span> <span class="mf">1.7325</span><span class="p">;</span>
<span class="n">x3_star</span> <span class="o">=</span> <span class="mf">0.0833</span><span class="p">;</span>
<span class="n">xi</span> <span class="o">=</span> <span class="p">[</span><span class="n">x1</span><span class="o">-</span><span class="n">x1_star</span><span class="p">;</span><span class="n">x2</span><span class="o">-</span><span class="n">x2_star</span><span class="p">;</span><span class="n">x3</span><span class="o">-</span><span class="n">x3_star</span><span class="p">];</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="n">x1</span><span class="p">;</span><span class="n">x2</span><span class="p">;</span><span class="n">x3</span><span class="p">];</span>
<span class="n">P</span> <span class="o">=</span> <span class="p">[</span><span class="n">p11</span> <span class="n">p12</span> <span class="n">p13</span><span class="p">;</span>
     <span class="n">p12</span> <span class="n">p22</span> <span class="n">p23</span>
     <span class="n">p13</span> <span class="n">p23</span> <span class="n">p33</span><span class="p">];</span>

<span class="n">f</span> <span class="o">=</span> <span class="nb">transpose</span><span class="p">(</span><span class="n">A_num_cd3</span><span class="p">)</span><span class="o">*</span><span class="n">P</span><span class="o">+</span><span class="n">P</span><span class="o">*</span><span class="n">A_num_cd3</span><span class="p">;</span>
<span class="n">solveforP</span> <span class="o">=</span> <span class="n">f</span><span class="o">+</span><span class="n">Q</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">solved_p</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="n">solveforP</span><span class="p">,</span> <span class="p">[</span><span class="n">p11</span> <span class="n">p12</span> <span class="n">p13</span> <span class="n">p22</span> <span class="n">p23</span> <span class="n">p33</span><span class="p">]);</span>

<span class="n">P</span> <span class="o">=</span> <span class="nb">double</span><span class="p">([</span><span class="n">solved_p</span><span class="o">.</span><span class="n">p11</span> <span class="n">solved_p</span><span class="o">.</span><span class="n">p12</span> <span class="n">solved_p</span><span class="o">.</span><span class="n">p13</span><span class="p">;</span>
     <span class="n">solved_p</span><span class="o">.</span><span class="n">p12</span> <span class="n">solved_p</span><span class="o">.</span><span class="n">p22</span> <span class="n">solved_p</span><span class="o">.</span><span class="n">p23</span><span class="p">;</span>
     <span class="n">solved_p</span><span class="o">.</span><span class="n">p13</span> <span class="n">solved_p</span><span class="o">.</span><span class="n">p23</span> <span class="n">solved_p</span><span class="o">.</span><span class="n">p33</span><span class="p">]);</span>
<span class="n">V</span> <span class="o">=</span> <span class="nb">transpose</span><span class="p">(</span><span class="n">xi</span><span class="p">)</span><span class="o">*</span><span class="n">P</span><span class="o">*</span><span class="p">(</span><span class="n">xi</span><span class="p">);</span>

<span class="c1">%% Stabilizing controller</span>
<span class="c1">% Find fixed point including u_star </span>
<span class="n">syms</span> <span class="n">M_star</span> <span class="n">H_star</span> <span class="n">R_star</span> <span class="n">a1</span> <span class="n">a2</span> <span class="n">a3</span> <span class="n">a4</span> <span class="n">a5</span> <span class="n">a6</span> <span class="n">a7</span> <span class="n">u_star</span> 
<span class="n">M_star_num</span> <span class="o">=</span> <span class="mf">0.1</span><span class="p">;</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">subs</span><span class="p">(</span><span class="n">coef</span><span class="p">,</span><span class="n">coef</span><span class="p">,</span><span class="n">cd3</span><span class="p">);</span>
<span class="n">f1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">+</span><span class="n">c</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">M_star_num</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">M_star_num</span><span class="p">)</span><span class="o">-</span><span class="n">c</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">M_star_num</span><span class="o">*</span><span class="n">H_star</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">f2</span> <span class="o">=</span> <span class="n">c</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">H_star</span><span class="o">*</span><span class="n">R_star</span><span class="o">-</span><span class="n">c</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">*</span><span class="n">H_star</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">f3</span> <span class="o">=</span> <span class="n">c</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="o">*</span><span class="n">R_star</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">R_star</span><span class="p">)</span><span class="o">-</span><span class="n">c</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">*</span><span class="n">H_star</span><span class="o">*</span><span class="n">R_star</span><span class="o">-</span><span class="n">c</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="o">*</span><span class="n">R_star</span><span class="o">+</span><span class="n">u_star</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>

<span class="p">[</span><span class="n">H_star_num</span><span class="p">,</span><span class="n">u_star_num</span><span class="p">,</span><span class="n">R_star_num</span><span class="p">]</span> <span class="o">=</span> <span class="n">solve</span><span class="p">([</span><span class="n">f1</span><span class="p">,</span><span class="n">f2</span><span class="p">,</span><span class="n">f3</span><span class="p">],[</span><span class="n">H_star</span><span class="p">,</span><span class="n">u_star</span><span class="p">,</span><span class="n">R_star</span><span class="p">]);</span>
<span class="n">fixed_stab</span> <span class="o">=</span> <span class="nb">double</span><span class="p">([</span><span class="n">M_star_num</span><span class="p">,</span><span class="n">H_star_num</span><span class="p">,</span><span class="n">R_star_num</span><span class="p">,</span><span class="n">u_star_num</span><span class="p">]);</span>

<span class="c1">% Linearize</span>
<span class="n">f1_lin</span> <span class="o">=</span> <span class="mi">1</span><span class="o">+</span><span class="n">c</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">M_star</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">M_star</span><span class="p">)</span><span class="o">-</span><span class="n">c</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">M_star</span><span class="o">*</span><span class="n">H_star</span><span class="p">;</span>
<span class="n">f2_lin</span> <span class="o">=</span> <span class="n">c</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">H_star</span><span class="o">*</span><span class="n">R_star</span><span class="o">-</span><span class="n">c</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">*</span><span class="n">H_star</span><span class="p">;</span>
<span class="n">f3_lin</span> <span class="o">=</span> <span class="n">c</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="o">*</span><span class="n">R_star</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">R_star</span><span class="p">)</span><span class="o">-</span><span class="n">c</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">*</span><span class="n">H_star</span><span class="o">*</span><span class="n">R_star</span><span class="o">-</span><span class="p">(</span><span class="n">c</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="o">*</span><span class="n">R_star</span><span class="p">)</span><span class="o">+</span><span class="n">u_star</span><span class="p">;</span>

<span class="n">A</span> <span class="o">=</span> <span class="n">jacobian</span><span class="p">([</span><span class="n">f1_lin</span><span class="p">,</span><span class="n">f2_lin</span><span class="p">,</span><span class="n">f3_lin</span><span class="p">],[</span><span class="n">M_star</span><span class="p">,</span><span class="n">H_star</span><span class="p">,</span><span class="n">R_star</span><span class="p">]);</span>
<span class="n">B</span> <span class="o">=</span> <span class="n">jacobian</span><span class="p">([</span><span class="n">f1_lin</span><span class="p">,</span><span class="n">f2_lin</span><span class="p">,</span><span class="n">f3_lin</span><span class="p">],</span><span class="n">u_star</span><span class="p">);</span>

<span class="n">A_num</span> <span class="o">=</span> <span class="nb">double</span><span class="p">(</span><span class="n">subs</span><span class="p">(</span><span class="n">A</span><span class="p">,[</span><span class="n">M_star</span> <span class="n">H_star</span> <span class="n">R_star</span> <span class="n">u_star</span><span class="p">],</span><span class="n">fixed_stab</span><span class="p">));</span>
<span class="n">B_num</span> <span class="o">=</span> <span class="nb">double</span><span class="p">(</span><span class="n">B</span><span class="p">);</span>

<span class="c1">% Check controllability</span>
<span class="n">Contr_mat</span> <span class="o">=</span> <span class="n">ctrb</span><span class="p">(</span><span class="n">A_num</span><span class="p">,</span><span class="n">B_num</span><span class="p">);</span>

<span class="c1">% Design K such that A-BK is Hurwitz</span>
<span class="c1">% pole_loc = [-20 -21 -22];</span>
<span class="c1">% pole_loc = [-1 -2 -10]; </span>
<span class="n">pole_loc</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span> <span class="o">-</span><span class="mi">10</span> <span class="o">-</span><span class="mf">0.1</span><span class="p">];</span>
<span class="n">K</span> <span class="o">=</span> <span class="n">place</span><span class="p">(</span><span class="n">A_num</span><span class="p">,</span><span class="n">B_num</span><span class="p">,</span><span class="n">pole_loc</span><span class="p">);</span>
<span class="n">hurwitz</span> <span class="o">=</span> <span class="nb">eig</span><span class="p">(</span><span class="n">A_num</span><span class="o">-</span><span class="n">B_num</span><span class="o">*</span><span class="n">K</span><span class="p">);</span>
<span class="n">F</span> <span class="o">=</span> <span class="o">-</span><span class="n">K</span><span class="p">;</span>
<span class="c1">% Check whether the controlled system stays in positive invariant set P</span>
<span class="n">states_star</span> <span class="o">=</span> <span class="n">fixed_stab</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="k">end</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="n">input_star</span> <span class="o">=</span> <span class="n">fixed_stab</span><span class="p">(</span><span class="k">end</span><span class="p">);</span>

<span class="n">syms</span> <span class="n">k1</span> <span class="n">k2</span> <span class="n">k3</span> <span class="n">M</span> <span class="n">H</span> <span class="n">R</span>
<span class="n">k</span> <span class="o">=</span> <span class="p">[</span><span class="n">k1</span> <span class="n">k2</span> <span class="n">k3</span><span class="p">];</span>
<span class="n">f1</span> <span class="o">=</span> <span class="o">-</span><span class="n">k</span> <span class="o">*</span> <span class="p">[</span><span class="n">M</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">3</span><span class="p">)]</span><span class="o">+</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">f2</span> <span class="o">=</span> <span class="o">-</span><span class="n">k</span> <span class="o">*</span> <span class="p">[</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="n">H</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">3</span><span class="p">)]</span><span class="o">+</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">f3</span> <span class="o">=</span> <span class="o">-</span><span class="n">k</span> <span class="o">*</span> <span class="p">[</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span><span class="n">R</span><span class="o">-</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">3</span><span class="p">)]</span><span class="o">+</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">;</span>

<span class="c1">%% Simulation controlled system</span>
<span class="n">t</span> <span class="o">=</span> <span class="mi">0</span><span class="p">:</span><span class="mf">0.001</span><span class="p">:</span><span class="mi">100</span><span class="p">;</span>
<span class="c1">% Define initial conditions with first a linear spacing and then some</span>
<span class="c1">% arbitrary points of interest</span>
<span class="n">xi0_1</span><span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="mf">0.05</span><span class="p">:</span><span class="mf">0.025</span><span class="p">:</span><span class="mf">0.05</span><span class="p">);</span> <span class="c1">% Perturbation of Malignant cells</span>
<span class="n">xi0_2</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="mf">0.05</span><span class="p">:</span><span class="mf">0.025</span><span class="p">:</span><span class="mf">0.05</span><span class="p">);</span> <span class="c1">% Perturbation of Hunting cells</span>
<span class="n">xi0_3</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="mf">0.05</span><span class="p">:</span><span class="mf">0.025</span><span class="p">:</span><span class="mf">0.05</span><span class="p">);</span> <span class="c1">% Perturbation of Resting cells</span>

<span class="n">xi0</span> <span class="o">=</span> <span class="p">[</span><span class="n">xi0_1</span><span class="p">;</span><span class="n">xi0_2</span><span class="p">;</span><span class="n">xi0_3</span><span class="p">];</span>

<span class="c1">%%% solver %%%</span>

<span class="k">for</span> <span class="n">p</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">xi0</span><span class="p">)</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">stab_cell_interaction</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">xi0</span><span class="p">(:,</span><span class="n">p</span><span class="p">),</span><span class="n">A_num</span><span class="p">,</span><span class="n">B_num</span><span class="p">,</span><span class="n">K</span><span class="p">);</span>
    <span class="n">solx_stab</span><span class="p">{</span><span class="n">p</span><span class="p">}</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">(:,</span><span class="mi">1</span><span class="p">)</span> <span class="n">x</span><span class="p">(:,</span><span class="mi">2</span><span class="p">)</span> <span class="n">x</span><span class="p">(:,</span><span class="mi">3</span><span class="p">)</span> <span class="p">];</span>
<span class="k">end</span>

<span class="c1">%%% 3D trajetory plot %%%</span>

<span class="nb">figure</span><span class="p">()</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">solx_stab</span><span class="p">)</span>
<span class="nb">plot3</span><span class="p">(</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">1</span><span class="p">),</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">2</span><span class="p">),</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">3</span><span class="p">))</span>
<span class="nb">hold</span> <span class="n">on</span>
<span class="k">end</span>
<span class="nb">grid</span> <span class="n">on</span>
<span class="nb">xlabel</span><span class="p">(</span><span class="s1">'Malignant cells density perturbation'</span><span class="p">)</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Hunting cells density perturbation'</span><span class="p">)</span>
<span class="nb">zlabel</span><span class="p">(</span><span class="s1">'Resting cells density perturbation'</span><span class="p">)</span>
<span class="n">plotTweak</span><span class="p">();</span>

<span class="c1">%% From perturbation to actual state simulation plot 3D</span>

<span class="nb">figure</span><span class="p">()</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">solx_stab</span><span class="p">)</span>
<span class="nb">plot3</span><span class="p">(</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">1</span><span class="p">)</span><span class="o">+</span><span class="n">M_star_num</span><span class="p">,</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">2</span><span class="p">)</span><span class="o">+</span><span class="nb">double</span><span class="p">(</span><span class="n">H_star_num</span><span class="p">),</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">3</span><span class="p">)</span><span class="o">+</span><span class="nb">double</span><span class="p">(</span><span class="n">R_star_num</span><span class="p">))</span>
<span class="nb">hold</span> <span class="n">on</span>
<span class="k">end</span>
<span class="nb">plot3</span><span class="p">(</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span><span class="n">fixed_stab</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span><span class="s1">'r.'</span><span class="p">,</span> <span class="s1">'MarkerSize'</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
<span class="nb">grid</span> <span class="n">on</span>
<span class="nb">xlabel</span><span class="p">(</span><span class="s1">'Malignant cells density'</span><span class="p">)</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Hunting cells density'</span><span class="p">)</span>
<span class="nb">zlabel</span><span class="p">(</span><span class="s1">'Resting cells density'</span><span class="p">)</span>
<span class="nb">legend</span><span class="p">(</span><span class="s1">'\xi = -0.05'</span><span class="p">,</span><span class="s1">'\xi=-0.025'</span><span class="p">,</span><span class="s1">'\xi=0'</span><span class="p">,</span><span class="s1">'\xi=0.025'</span><span class="p">,</span><span class="s1">'\xi=0.05'</span><span class="p">)</span>
<span class="n">plotTweak</span><span class="p">();</span>

<span class="n">F</span> <span class="o">=</span> <span class="o">-</span><span class="n">K</span><span class="p">;</span>

<span class="c1">% Plot input</span>
<span class="nb">figure</span><span class="p">()</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">solx_stab</span><span class="p">)</span>
<span class="n">u_traj</span> <span class="o">=</span>  <span class="n">fixed_stab</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="o">-</span> <span class="n">K</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="p">[</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">1</span><span class="p">)]</span><span class="o">-</span><span class="n">K</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">[</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">2</span><span class="p">)]</span><span class="o">-</span> <span class="n">K</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="p">[</span><span class="n">solx_stab</span><span class="p">{</span><span class="n">i</span><span class="p">}(:,</span><span class="mi">3</span><span class="p">)];</span>
<span class="nb">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">u_traj</span><span class="p">)</span>
<span class="nb">hold</span> <span class="n">on</span>
<span class="nb">grid</span> <span class="n">on</span>
<span class="k">end</span>
<span class="nb">xlabel</span><span class="p">(</span><span class="s1">'Time [s]'</span><span class="p">)</span>
<span class="nb">ylabel</span><span class="p">(</span><span class="s1">'Control input u(t)'</span><span class="p">)</span>
<span class="nb">legend</span><span class="p">(</span><span class="s1">'\xi = -0.05'</span><span class="p">,</span><span class="s1">'\xi=-0.025'</span><span class="p">,</span><span class="s1">'\xi=0'</span><span class="p">,</span><span class="s1">'\xi=0.025'</span><span class="p">,</span><span class="s1">'\xi=0.05'</span><span class="p">)</span>
<span class="n">plotTweak</span><span class="p">();</span>

<span class="c1">%% Commands</span>
<span class="c1">% set(fig,'renderer','Painters')</span>
<span class="c1">% saveas(fig,'figName','epsc')</span>
</code></pre></div></div>
<h2 id="functions">Functions</h2>

<h3 id="cell_interactionm">cell_interaction.m</h3>
<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">function</span> <span class="n">x</span> <span class="o">=</span> <span class="n">cell_interaction</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">x0</span><span class="p">,</span><span class="nb">cd</span><span class="p">)</span>
<span class="p">[</span><span class="n">t</span><span class="p">,</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="nb">ode45</span><span class="p">(</span><span class="o">@</span><span class="n">coupledfun</span><span class="p">,</span><span class="n">t</span><span class="p">,</span><span class="n">x0</span><span class="p">,[],</span><span class="nb">cd</span><span class="p">);</span>
<span class="k">end</span>
</code></pre></div></div>

<h3 id="coupledfunm">coupledfun.m</h3>
<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">function</span> <span class="n">dxdt</span> <span class="o">=</span> <span class="n">coupledfun</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">x0</span><span class="p">,</span><span class="nb">cd</span><span class="p">)</span>
<span class="n">x1</span> <span class="o">=</span> <span class="n">x0</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="n">x2</span> <span class="o">=</span> <span class="n">x0</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="n">x3</span> <span class="o">=</span> <span class="n">x0</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
<span class="n">dx1dt</span> <span class="o">=</span> <span class="mi">1</span><span class="o">+</span><span class="nb">cd</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">x1</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">x1</span><span class="p">)</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">x1</span><span class="o">*</span><span class="n">x2</span><span class="p">;</span>
<span class="n">dx2dt</span> <span class="o">=</span> <span class="nb">cd</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">x2</span><span class="o">*</span><span class="n">x3</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">*</span><span class="n">x2</span><span class="p">;</span>
<span class="n">dx3dt</span> <span class="o">=</span> <span class="nb">cd</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="o">*</span><span class="n">x3</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">x3</span><span class="p">)</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">*</span><span class="n">x2</span><span class="o">*</span><span class="n">x3</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="o">*</span><span class="n">x3</span><span class="p">;</span>
<span class="n">dxdt</span> <span class="o">=</span> <span class="p">[</span><span class="n">dx1dt</span><span class="p">;</span><span class="n">dx2dt</span><span class="p">;</span><span class="n">dx3dt</span><span class="p">];</span>
<span class="k">end</span>
</code></pre></div></div>

<h3 id="d_cell_interactionm">d_cell_interaction.m</h3>
<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">function</span> <span class="n">y</span> <span class="o">=</span> <span class="n">d_cell_interaction</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">delay</span><span class="p">,</span><span class="n">x0</span><span class="p">,</span><span class="nb">cd</span><span class="p">)</span>
<span class="c1">%D_CELL_INTERACTION Summary of this function goes here</span>
<span class="c1">%   Detailed explanation goes here</span>
<span class="n">y</span> <span class="o">=</span> <span class="nb">dde23</span><span class="p">(</span><span class="o">@</span><span class="n">d_coupledfun</span><span class="p">,</span><span class="n">delay</span><span class="p">,</span><span class="n">x0</span><span class="p">,[</span><span class="n">t</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">t</span><span class="p">(</span><span class="k">end</span><span class="p">)],[],</span><span class="nb">cd</span><span class="p">);</span>
<span class="k">end</span>
</code></pre></div></div>

<h3 id="d_historym">d_history.m</h3>
<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">function</span> <span class="n">s</span> <span class="o">=</span> <span class="n">d_history</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">x0</span><span class="p">)</span>
<span class="c1">% Constant history function for DDEX1.</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">x0</span><span class="p">;</span>
<span class="k">end</span>
</code></pre></div></div>

<h3 id="d_coupledfunm">d_coupledfun.m</h3>
<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">function</span> <span class="n">dydt</span> <span class="o">=</span> <span class="n">d_coupledfun</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">Z</span><span class="p">,</span><span class="nb">cd</span><span class="p">)</span>
<span class="c1">% Differential equations function for DDEX1.</span>
<span class="n">x_lagM</span> <span class="o">=</span> <span class="n">Z</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="n">x_lagH</span> <span class="o">=</span> <span class="n">Z</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="n">x_lagR</span> <span class="o">=</span> <span class="n">Z</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
<span class="n">x1</span> <span class="o">=</span> <span class="n">y</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="n">x2</span> <span class="o">=</span> <span class="n">y</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="n">x3</span> <span class="o">=</span> <span class="n">y</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
<span class="n">dx1dt</span> <span class="o">=</span> <span class="mi">1</span><span class="o">+</span><span class="nb">cd</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">x1</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">x1</span><span class="p">)</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">x1</span><span class="o">*</span><span class="n">x2</span><span class="p">;</span>
<span class="n">dx2dt</span> <span class="o">=</span> <span class="nb">cd</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">x_lagH</span><span class="o">*</span><span class="n">x_lagR</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">*</span><span class="n">x2</span><span class="p">;</span>
<span class="n">dx3dt</span> <span class="o">=</span> <span class="nb">cd</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="o">*</span><span class="n">x3</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">x3</span><span class="p">)</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">*</span><span class="n">x2</span><span class="o">*</span><span class="n">x3</span><span class="o">-</span><span class="nb">cd</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="o">*</span><span class="n">x3</span><span class="p">;</span>
<span class="n">dydt</span> <span class="o">=</span> <span class="p">[</span><span class="n">dx1dt</span><span class="p">;</span><span class="n">dx2dt</span><span class="p">;</span><span class="n">dx3dt</span><span class="p">];</span>
</code></pre></div></div>

<h3 id="stab_cell_interactionm">stab_cell_interaction.m</h3>
<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">function</span> <span class="n">x</span> <span class="o">=</span> <span class="n">stab_cell_interaction</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">x0</span><span class="p">,</span><span class="n">A_num</span><span class="p">,</span><span class="n">B_num</span><span class="p">,</span><span class="n">K</span><span class="p">)</span>
<span class="p">[</span><span class="n">t</span><span class="p">,</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="nb">ode45</span><span class="p">(</span><span class="o">@</span><span class="n">stab_coupledfun</span><span class="p">,</span><span class="n">t</span><span class="p">,</span><span class="n">x0</span><span class="p">,[],</span><span class="n">A_num</span><span class="p">,</span><span class="n">B_num</span><span class="p">,</span><span class="n">K</span><span class="p">);</span>
<span class="k">end</span>
</code></pre></div></div>

<h3 id="stab_coupledfunm">stab_coupledfun.m</h3>
<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">function</span> <span class="n">dxdt</span> <span class="o">=</span> <span class="n">stab_coupledfun</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">x0</span><span class="p">,</span><span class="n">A_num</span><span class="p">,</span><span class="n">B_num</span><span class="p">,</span><span class="n">K</span><span class="p">)</span>
<span class="n">x1</span> <span class="o">=</span> <span class="n">x0</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="n">x2</span> <span class="o">=</span> <span class="n">x0</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="n">x3</span> <span class="o">=</span> <span class="n">x0</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
<span class="n">dx1dt</span> <span class="o">=</span> <span class="n">A_num</span><span class="p">(</span><span class="mi">1</span><span class="p">,:)</span><span class="o">*</span><span class="p">[</span><span class="n">x1</span><span class="p">;</span><span class="n">x2</span><span class="p">;</span><span class="n">x3</span><span class="p">]</span> <span class="o">+</span> <span class="n">B_num</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="o">-</span><span class="n">K</span><span class="o">*</span><span class="p">[</span><span class="n">x1</span><span class="p">;</span><span class="n">x2</span><span class="p">;</span><span class="n">x3</span><span class="p">]);</span>
<span class="n">dx2dt</span> <span class="o">=</span> <span class="n">A_num</span><span class="p">(</span><span class="mi">2</span><span class="p">,:)</span><span class="o">*</span><span class="p">[</span><span class="n">x1</span><span class="p">;</span><span class="n">x2</span><span class="p">;</span><span class="n">x3</span><span class="p">]</span> <span class="o">+</span> <span class="n">B_num</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="o">-</span><span class="n">K</span><span class="o">*</span><span class="p">[</span><span class="n">x1</span><span class="p">;</span><span class="n">x2</span><span class="p">;</span><span class="n">x3</span><span class="p">]);</span>
<span class="n">dx3dt</span> <span class="o">=</span> <span class="n">A_num</span><span class="p">(</span><span class="mi">3</span><span class="p">,:)</span><span class="o">*</span><span class="p">[</span><span class="n">x1</span><span class="p">;</span><span class="n">x2</span><span class="p">;</span><span class="n">x3</span><span class="p">]</span> <span class="o">+</span> <span class="n">B_num</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="o">-</span><span class="n">K</span><span class="o">*</span><span class="p">[</span><span class="n">x1</span><span class="p">;</span><span class="n">x2</span><span class="p">;</span><span class="n">x3</span><span class="p">]);</span>
<span class="n">dxdt</span> <span class="o">=</span> <span class="p">[</span><span class="n">dx1dt</span><span class="p">;</span><span class="n">dx2dt</span><span class="p">;</span><span class="n">dx3dt</span><span class="p">];</span>
<span class="k">end</span>
</code></pre></div></div>

<h1 id="resources">Resources</h1>
<h2 id="books">Books</h2>
<p>[1.] Khalil, H.K. (2014) Nonlinear systems. Upper Saddle River, NJ: Prentice Hall.</p>
<h2 id="websites">Websites</h2>
<p><a href="http://matlab.imm.uran.ru/mirrors/www.cs.runet.edu/~thompson/webddes/tutorial.html">Tutorial on delayed system simulation</a></p>]]></content><author><name>Michiel</name></author><category term="Non-linear" /><category term="Lyapunov-stability" /><category term="Bifurcations" /><category term="Delayed-systems" /><summary type="html"><![CDATA[The goal of this project is to gain insight into the dynamic behavior of the immune system. Check stability of fixed points via Lyapunov stability analysis. Simulations with the use of MATLAB have been performed for the original system as well as the time-delayed system. Three clinical conditions are given and the response of the immune system is determined for each of them. For condition 3 also a time-delayed system is simulated for which a Hopf bifurcation is determined. All feasible fixed points in all clinical conditions where asymptotically stable. Introduction Understanding the interaction between malignant cells and the immune system of an organism deserves quite some attention. A system theoretic perspective on this understanding proves quite useful for studying the dynamic features of tumor growth and to understand the implication on specific treatments. In fact, such a perspective brings valuable insight in some key mechanisms that are used in immunology, oncology and cancer biology. This will be the purpose of this project. The system of nonlinear coupled differential equations are given by: \[\begin{aligned} &amp;\dot{M}=1+a_1 M(1-M)-a_2 M H \\ &amp;\dot{H}=a_3 H R-a_4 H \\ &amp;\dot{R}=a_5 R(1-R)-a_6 H R-a_7 R \end{aligned}\] Where $M$ is the density of the malignant or tumor cells, $H$ is the density of the active hunting cells, $R$ is the density of the resting cells in the immune system. All indicated parameters $a_i$ are non-negative numbers. In particular, $a_1$ is the growth rate of tumor cells, $a_2$ is the rate of destruction of tumor cells by the hunting cells, $a_3$ is the conversion rate from resting cells to hunting cells, $a_4$ is the natural death rate of hunting cells, $a_5$ is the growth rate of resting cells, $a_6$ is the conversion rate from hunting to resting cells, $a_7$ is the natural natural death rate of resting cells. Fixed points and biological feasibility The fixed points can be determined by solving the system of equations when the derivatives are zero, because the fixed points in a system are the solutions when there is no time dependency. Firstly, the rate of change of density of the hunting cells $\dot{H}=0$ is solved. \[\begin{aligned} &amp; a_3 H R-a_4 H=0 \\ &amp; H(a_3 R-a_4)=0 \\ &amp; H^{\ast}_1=0 \vee R^{\ast}_1=\frac{a_4}{a_3} \end{aligned}\] Where \(H^{\ast}_1\) will be substituted such that \(M^{\ast}_{1,\ 2}\) can be computed by solving $\dot{M}=0$. \[\begin{aligned} &amp; 1+a_1M(1-M)-a_2 M H=0 \\ &amp; H^{\ast}_1=0\\ &amp; 1+a_1 M-a_1 M^2=0 \\ &amp; M^2-M -\frac{1}{a_1}\\ &amp; M^{\ast}_{1,2}=\frac{1}{2}\pm\sqrt{\frac{1}{4}+\frac{1}{a_1}} \end{aligned}\] Next, $H^{\ast}_1$ is substituted while $\dot{R}=0$ to obtain $R_2$ and $R_3$: \[\begin{aligned} &amp; H^{\ast}_1=0\\ &amp; a_5 R(1-R) - a_7 R=0 \\ &amp; R(a_5-a_5 R -a_7)=0 \\ &amp; R^{\ast}_2=0 \vee R^{\ast}_3=1-\frac{a_7}{a_5} \end{aligned}\] $H^{\ast}_2$ can be computed by implementing $R^{\ast}_1$ in $\dot{R}=0$: \[\begin{aligned} &amp; R^{\ast}_1=\frac{a_4}{a_3}\\ &amp; a_5 R(1-R)-a_6 H R-a_7 R=0 \\ &amp; \frac{a_4}{a_3}\cdot(a_5-a_5\cdot \frac{a_4}{a_3}-a_6\cdot H -a_7)=0 \\ &amp; H^{\ast}_2=\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6} \end{aligned}\] Finally, \(H^{\ast}_2\) can be substituted in the equation $\dot{M}=0$ to obtain $M_{3,4}$: \[\begin{aligned} &amp; H^{\ast}_2=\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6}\\ &amp; 1+a_1 M(1-M)-a_2 M H_2=0 \\ &amp; M^{\ast}_{3,4}=(\frac{1}{2}-\frac{a_2}{2a_1}H_2^{\ast})\pm \sqrt{(\frac{a_2}{a_1}H_2-1)^2+\frac{1}{a_1}} \end{aligned}\] The resulting fixed points are stated below: \[\begin{aligned} &amp; (M^{\ast}_1,H^{\ast}_1,R^{\ast}_2)=(\frac{1}{2}+\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,0)\\ &amp; (M^{\ast}_2,H^{\ast}_1,R^{\ast}_2)=(\frac{1}{2}-\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,0)\\ &amp; (M^{\ast}_1,H^{\ast}_1,R^{\ast}_3)=(\frac{1}{2}+\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,1-\frac{a_7}{a_5})\\ &amp; (M^{\ast}_2,H^{\ast}_1,R^{\ast}_3)=(\frac{1}{2}-\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,1-\frac{a_7}{a_5})\\ &amp; (M^{\ast}_3,H^{\ast}_2,R^{\ast}_1)=\frac{1}{2}(\frac{a_2}{a_1}H_2^{\ast}-1)+ \sqrt{\frac{1}{4}\cdot(\frac{a_2}{a_1}H_2^{\ast}-1)^2+\frac{1}{a_1}},\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6},\frac{a_4}{a_3})\\ &amp; (M^{\ast}_4,H^{\ast}_2,R^{\ast}_1)=\frac{1}{2}(\frac{a_2}{a_1}H_2^{\ast}-1)- \sqrt{\frac{1}{4}\cdot(\frac{a_2}{a_1}H_2^{\ast}-1)^2+\frac{1}{a_1}},\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6},\frac{a_4}{a_3})\\ \end{aligned}\] Feasibility The positive invariant set \(\mathcal{P}\) is defined to be the following: \[\mathcal{P} := \{(M,H,R) \in \mathbb{R}^3 \;|\; M \geq 0, H \geq 0, R \geq 0 \}\] Positive invariance implies here that if the initial condition is inside the set, it will never leave the set. Since we are dealing with a physical system, densities cannot be negative, therefore all fixed points must lie within the set \(\mathcal{P}\) to be feasible. In other words, the values of the fixed points require to be in set $\mathcal{P}$. The only fixed points which lie within this set for all coefficients $a_i&gt;0$ are the following: \[\begin{align} &amp; (M^{\ast}_1,H^{\ast}_1,R^{\ast}_2)=(\frac{1}{2}+\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,0)\\ &amp; (M^{\ast}_1,H^{\ast}_1,R^{\ast}_3)=(\frac{1}{2}+\sqrt{\frac{1}{4}+\frac{1}{a_1}},0,1-\frac{a_7}{a_5})\\ &amp; (M^{\ast}_3,H^{\ast}_2,R^{\ast}_1)=\frac{1}{2}(\frac{a_2}{a_1}H_2^{\ast}-1)+ \sqrt{\frac{1}{4}\cdot(\frac{a_2}{a_1}H_2^{\ast}-1)^2+\frac{1}{a_1}},\frac{a_5 a_3 -a_4 a_5 -a_3 a_7}{a_3 a_6},\frac{a_4}{a_3}) \end{align}\] This is the case under the following conditions: Fixed point \((M^{*}_1,H^{*}_1,R^{*}_2)\) is feasible for every \(a_i&gt;0\). Fixed point \((M^{*}_1,H^{*}_1,R^{*}_3)\) is feasible for \(a_i&gt;0\) , except for \(R_2\) which is only feasible if \(\frac{a_7}{a_5} \leq 1\). Fixed point \((M^{*}_3,H^{*}_2,R^{*}_1)\), parameters \(a_3,a_4,a_5\) and \(a_7\) in \(H_2\) needs to be in a certain ratio to be feasible since \(H_2\) must be positive. Consequently, the following sufficient condition can be derived: \[H^*_2=\frac{a_5 a_3-a_4 a_5-a_3 a_7}{a_3 a_6}\] For \(H^*_2\) to be $&gt;0$, the numerator of \(H^*_2\) must be positive: \[\begin{align} &amp; a_5 a_3-a_4 a_5-a_3 a_7&gt;0 \\ &amp; a_4 a_5+a_3 a_7&lt;a_5 a_3 \\ &amp; \frac{a_4 a_5+a_3 a_7}{a_5 a_3}&lt;1 \\ &amp; \frac{a_4}{a_3}+\frac{a_7}{a_5}&lt;1 \end{align} \notag\] Given that $\frac{a_4}{a_3}+\frac{a_7}{a_5}&lt;1$ is a sufficient condition to guarantee the feasibility of \(H^*_2\), it will automatically be a sufficient condition for \(R^*_2\) because \(\frac{a_7}{a_5}\leq 1\) is true if \(\frac{a_4}{a_3}+\frac{a_7}{a_5}&lt;1\) is true. Consequently, all the fixed points stated in \autoref{eq:feasible_fixed} are feasible due to this sufficient condition. Hence, the dynamical system has three fixed points which can be described in the following form: \[E_1^*=\left(M_1^*, 0,0\right), \quad E_2^*=\left(M_2^*, 0, R_2^*\right), \quad E_3^*=\left(M_3^*, H_3^*, R_3^*\right)\] These fixed points can be biological interpreted as follows. \(E_1^*\) The density of Malignant cells $\dot{M}$ can only be externally influenced by the Hunting cells due to the rate of destruction of tumor cells $a_2$. This specific fixed point occurs when H and R are zero, meaning that this is the maximum malignant cell density that is constant and since $M=0.1$ is considered reasonable, this fixed point can therefore be labeled as dangerous. In this case the organism does not have an active immune system. \(E_2^*\) The dynamical behavior of the Resting cells and the behavior of the Malignant cells do not directly depend on each other. they are coupled via the density of hunting cells. At the fixed point, the resting cells grow as quickly as they die. The conversion rate $a_6$ from hunting cells to resting cells and resting cells to hunting cells $a_3$ will only be activated when the organism has hunting cells to begin with. \(E_3^*\) Every point in set $\mathcal{P}$ which has a density bigger than zero, will converge to this fixed point. The hunting cells hunt down the Malignant cells with a destruction rate of $a_2$. The hunting cells will grow because of the conversion rate from resting cells to hunting cells $a_3$. Depending on the amount of initial resting cells, the hunting cells will increase rapidly until they reach a maximum due to the natural death rate $a_4$. At this point the Hunting cells will be converted back into Resting cells due to the conversion rate from Hunting cells to Resting cells $a_6$. After this process, a steady state is reached where the malignant cells are within a reasonable amount. Biologically, this is the fixed point that occurs in a healthy immune system that naturally declines malignant cell growth. Simulation The three conditions provided determine the numerical values of the coefficients \(a_1,...,a_7\). These numerical values are as follows.   \(a_1\) \(a_2\) \(a_3\) \(a_4\) \(a_5\) \(a_6\) \(a_7\) Condition 1 $4.5$ $1$ $0.5$ $0.4$ $0.7$ $0.2$ $0.4$ Condition 2 $4.5$ $1$ $5.0$ $0.4$ $0.7$ $0.1$ $0.1$ Condition 3 $4.5$ $1$ $4.8$ $0.4$ $3.7$ $1.9$ $0.1$ For every condition, time simulations will be shown within the range of $t = (0,100) \;[s]$ and the initial conditions $(M_0,H_0,R_0)$ are chosen to be a linear spacing between 0 and 2 with a step of 0.5 and extra points of interest around fixed points.   \(x_1\) \(x_2\) \(x_3\) \(x_4\) \(x_5\) \(x_6\) \(x_7\) \(x_8\) \(x_9\) \(x_{10}\) Malignant cell density $0$ $0.5$ $1$ $1.5$ $2.0$ $1.0$ $4.0$ $4.0$ $4.0$ $4.0$ Hunting cell density $0$ $0.5$ $1$ $1.5$ $2.0$ $0$ $0$ $0.1$ $0.1$ $0.1$ Resting cell density $0$ $0.5$ $1$ $1.5$ $2.0$ $2.0$ $0$ $0.1$ $0.5$ $2.0$ These initial conditions yield a solid exploration of the vector field, without cluttering the output. For every condition, the same initial conditions are being used to be able to conclude differences Condition 1 Condition 1 inherent coefficients that are by definition not biologically feasible, because the \(\frac{a_4}{a_3}+\frac{a_7}{a_5} &lt; 1\) inequality does not hold. This implies that condition 1 is not possible and it therefore also causes a fixed point to be excluded from the set $\mathcal{P}$. If the set of conditions were possible, tumor dormancy can be concluded, because a steady state is reached where the malignant, hunting and resting cells remain constant and where the malignant cells are $&gt;0.1$. Condition 2 The time trajectories of initial condition \(x_0(1)\) and \(x_0(7)\) converge towards fixed point \(E^*_1\) and the trajectories of initial conditions \(x_0(6)\) converges towards fixed point \(E^*_2\), these are special cases and for that reason also hand picked, because it shows that once the tuple \((M,H,R)\) starts on the M axis, it stays on the M axis. Similarly, once $(M,H,R)$ starts on the $(M,0,R)$ plane it converges towards fixed point \(E^*_2\). Every other initial condition spirals towards the fixed point \(E^*_3\), which implies tumor remission in a cyclic pattern. Condition 3 For condition 3 the coefficients $a_5$ and $a_6$ are considerably larger which implies that the growth rate of resting cells have increased as well as the conversion rate from hunting to resting cells. With condition 3, less hunting cells will be available due to the $HR$ term that acts as a balance. The rate of change of hunting cells will therefore overall be smaller that means that the density of hunting cells will overall reach smaller values compared to condition 2. The time of convergence is approximately 6 times faster than condition 2, but the malignant cells reach steady state at a higher value. For condition 3, tumor remission with a cyclic pattern can be concluded. Stability and contractive properties For stability analysis, the dynamical system will be linearised. Therefore the Jacobian matrix must be computed: \[(M, H, R)=(x_1, x_2, x_3)\] \[\dot{x_1}=f_1\left(x_1, x_2, x_3\right) \\ \dot{x_2}=f_2\left(x_1, x_2, x_3\right)\\ \dot{x_3}=f_3\left(x_1, x_1, x_3\right)\] \[\left. A= \begin{bmatrix} \frac{\partial f_1}{\partial x_1} &amp; \frac{\partial f_1}{\partial x_2} &amp; \frac{\partial f_1}{\partial x_3} \\ \frac{\partial f_2}{\partial x_1} &amp; \frac{\partial f_2}{\partial x_2} &amp; \frac{\partial f_2}{\partial x_3} \\ \frac{\partial f_3}{\partial x_1} &amp; \frac{\partial f_3}{\partial x_2} &amp; \frac{\partial f_3}{\partial x_3} \end{bmatrix} \right\vert_{(x_1^{*},x_2^{*},x_3^{*})}\] \[A= \begin{bmatrix} a_1-2a_1M-a_2H &amp; -a2M &amp; 0 \\ 0 &amp; a3R-a_4 &amp; a_3H \\ 0 &amp; -a_6R &amp; a_5-2a_5R-a_6H-a_7 \\ \end{bmatrix}\] Lyapunov stability The goal is to find a suitable matrix \(P=P^{\top}\) such that the quadratic function \[V(M(t), H(t), R(t))=\left(\begin{array}{c} M(t)-M_3^* \\ H(t)-H_3^* \\ R(t)-R_3^* \end{array}\right)^{\top} P\left(\begin{array}{c} M(t)-M_3^* \\ H(t)-H_3^* \\ R(t)-R_3^* \end{array}\right)\] serves as a Lyapanov function to prove stability of the fixed point \(E^*_3\) under condition 3.The suitable value of P can now be solved using convex optimization techniques. For this reason, a mathematical optimization toolbox in Matlab called Yalmip is used to solve semidefinite programming problems using Mosek as a mathematical solver. To following constraints are considered: $P=P^{\top}\succ 0$ $A^{\top} P + P A \prec 0$ Implementing these constraints gave the following result: \[P=\begin{bmatrix} 0.0855 &amp; 0.0150 &amp; 0.0016\\ 0.0150 &amp; 0.0214 &amp; 0.0170\\ 0.0016 &amp; 0.0170 &amp; 0.8931 \end{bmatrix}\] The Lyapanov function for this specific $P$ holds since $V(x^*)=0$ and $V(x)’&lt;0$. Now that the Lyapanov function is computed, the maximum $\gamma$ in set $\mathcal{V_\gamma}$ can be determined. \[\mathcal{V}_\gamma:=\left\{(M, H, R) \in \mathbb{R}^3 \mid V(M, H, R) \leq \gamma\right\}\] The maximum level set $\gamma$ can be found via optimization. Here the objective is to maximize $\gamma$ using the following constraints: $\left(\begin{array}{c} M(t)-M_3^* H(t)-H_3^* R(t)-R_3^* \end{array}\right)^{\top} P\left(\begin{array}{c} M(t)-M_3^* H(t)-H_3^* R(t)-R_3^* \end{array}\right) \leq \gamma$ $M(t),H(t),R(t) &gt; 0$ $\gamma&gt;0$ Level sets of the 4-D Lyapunov function can be interpreted as spheres where the maximum $\gamma$ will barely touch the boundaries of set $\mathcal{P}$, since initial conditions outside of the set $\mathcal{P}$ or on the $M,R$ plane won’t converge to $E_3$. The maximum value for the level set of $V(M(t),H(t),R(t))$ and for whom all initial trajectories on this level set still converge is $\gamma_{max}=3.42$. Time delayed system The locations of the fixed points themselves do not change due to the time delay, because these fixed points are solely dependent on the ratio between coefficients $(a_1, … ,a_7)$ and assume all time dependant behavior have died out i.e. the system is not moving $(\dot{M},\dot{H},\dot{R}) = (0,0,0)$. However, the behavior around these fixed points do change with respect to the time delay. One bifurcation appears between $\tau = 0.2$ and $\tau = 0.3$ for which the fixed point is not an attractive fixed point since the trajectory turns into a limit cycle which orbits around a specific circumference. The higher the time delay the larger this circumference. This bifurcation can be classified by the Hopf bifurcation. Conclusion &amp; Discussion Conclusion Considerable insight into the dynamic behavior of the immune system has been obtained. Depending on the clinical conditions $a_1,…a_7$ biological feasibility can be ensured. For all conditions, the biologically feasible fixed points where asymptotically stable. The only case where asymptotic stability was not ensured is for the time delayed system. In that case, the response is always delayed which results in a limit cycle. The behavior change from asymptotic stability into a limit cycle can be classified as a Hopf bifurcation. Discussion Stabilization of the system is not necessary, because the system itself is already asymptotically stable. However, if the dynamics change over time the complete system can be stabilized by linearising around an operating point. Converting the dynamic model into state-space format and check whether the state space is stabilizable. Define a control law $\omega = -K\xi$ for which $A-BK$ is Hurwitz to obtain asymptotic stability. Software Main code %% 5CSA0 Tumor growth assignment clc, clear all, close all %% Fixed points syms M H R a1 a2 a3 a4 a5 a6 a7 f1 = 1+a1*M*(1-M)-a2*M*H == 0; f2 = a3*H*R-a4*H == 0; f3 = a5 * R * (1-R) -a6*H*R-a7*R == 0; solved = solve([f1,f2,f3],[M,H,R]); % Solve system of equations to find fixed points for i = 1:length(solved.H) xstar = [solved.M(i),solved.H(i),solved.R(i)]; xstar_mat{i,1} = xstar; end % Reorder to be the same as in the report xstar_mat([1 2],:) = xstar_mat([2 1],:); xstar_mat([3 5],:) = xstar_mat([5 3],:); %% Check biological feasibility % For all conditions, positive valued states must be achieved at the fixed points cd1 = [4.5 1 0.5 0.4 0.7 0.2 0.4]; cd2 = [3.5 1 5.0 0.4 0.7 0.1 0.1]; cd3 = [3.0 1 4.8 0.4 3.7 1.9 0.1]; cd = [cd1;cd2;cd3]; coef = [a1 a2 a3 a4 a5 a6 a7]; % Check biological feasibility of the conditions f4 = (a4/a3) + (a7/a5); % Constraint to be biologically feasible for k = 1:length(cd(:,1)) if subs(f4,coef,cd(k,:)) &lt; 1 feasiblity(k) = 1; else feasiblity(k) = 0; end end % Check all fixed point values with conditions for k = 1:length(cd(:,1)) for i = 1:length(xstar_mat) sol_xstar = double(subs(xstar_mat{i},coef,cd(k,:))); sol_xstar_mat{i,k} = sol_xstar; end end % Remove negative fixed points sol_xstar_mat_reduced = sol_xstar_mat([1 3 5],:); % Stability check of the biological feasible fixed points syms f1_lin f2_lin f3_lin f1_lin = 1+a1*M*(1-M)-a2*M*H; f2_lin = a3*H*R-a4*H; f3_lin = a5 * R * (1-R) -a6*H*R-a7*R; A = jacobian([f1_lin,f2_lin,f3_lin],[M,H,R]); % Loop over the coditions (k) and loop over the biologically feasible fixed % points (i) for k = 1:length(cd(:,1)) for i = 1:length(sol_xstar_mat_reduced(1,:)) A_stab{i,k} = eig(subs(A,[M H R],[sol_xstar_mat_reduced{i,k}])); A_stab_numeric{i,k} = double(eig(subs(A,[M,H,R,coef],[sol_xstar_mat_reduced{i,k},cd(k,:)]))); end end % All the eigen values per fixed point per condition in (i_(3x1),k) format A_eig = cell2mat(A_stab_numeric); A_num_cd3 = double(subs(A,[M H R coef],[sol_xstar_mat_reduced{3,3} cd(3,:)])); %% Simulation t = 0:0.01:100; % Define initial conditions with first a linear spacing and then some % arbitrary points of interest x0_1 = [(0:0.5:2)';1;4;4 ;4;4]'; x0_2 = [(0:0.5:2)';0;0;0.1;0.1;0.1]'; x0_3 = [(0:0.5:2)';2;0;0.1;0.5;2]'; scale_factor = 0.02; x0 = [x0_1;x0_2;x0_3]; [x1,x2,x3] = meshgrid(0:0.25:2); %%% Phase plot %%% for k = 1:length(cd(:,1)) for p = 1:length(x0) x = cell_interaction(t,x0(:,p),cd(k,:)); solx{p,k} = [x(:,1) x(:,2) x(:,3)]; end % 3D phase plot x1dot = 1+cd(k,1).*x1.*(1-x1)-cd(k,2).*x1.*x2; x2dot= (cd(k,3).*x2.*x3)-cd(k,4).*x2; x3dot= cd(k,5).*x3.*(1-x3)-(cd(k,6).*x2.*x3)-(cd(k,7).*x3); figure() quiver3(x1,x2,x3,x1dot*scale_factor,x2dot*scale_factor,x3dot*scale_factor,'autoscale','off') grid on; axis image; xlabel('Malignant cells density') ylabel('Hunting cells density') zlabel('Resting cells density') end %%% Time plot %%% for k = 1:length(cd(:,1)) figure() subplot(311) for i = 1:length(solx(:,1)) plot(t,solx{i,k}(:,1)) hold on; grid on end str = sprintf("States for condition %d and for all intital conditions",k); title(str) ylabel('Density of malignant cells') subplot(312) for i = 1:length(solx(:,1)) plot(t,solx{i,k}(:,2)) hold on; grid on end ylabel('Density of Hunting cells') subplot(313) for i = 1:length(solx(:,1)) plot(t,solx{i,k}(:,3)) hold on; grid on legends{i} = sprintf('X0(%d)', i); end xlabel('Time [s]') legend(legends) ylabel('Density of Resting cells') plotTweak(); end %%% 3D trajetory plot %%% for k = 1:length(cd(:,1)) figure() for i = 1:length(solx(:,1)) plot3(solx{i,k}(:,1),solx{i,k}(:,2),solx{i,k}(:,3)) hold on end plot3(sol_xstar_mat{1,k}(1),sol_xstar_mat{1,k}(2),sol_xstar_mat{1,k}(3),'r.', 'MarkerSize', 15) plot3(sol_xstar_mat{3,k}(1),sol_xstar_mat{3,k}(2),sol_xstar_mat{3,k}(3),'r.', 'MarkerSize', 15) plot3(sol_xstar_mat{5,k}(1),sol_xstar_mat{5,k}(2),sol_xstar_mat{5,k}(3),'r.', 'MarkerSize', 15) grid on xlabel('Malignant cells density') ylabel('Hunting cells density') zlabel('Resting cells density') plotTweak(); end %% Time delayed system Q8 t = (0:0.01:100); % delay_bif = 0.84932; % Include to see crash bifurcation delay_span = (0.1:0.1:0.8)'; delay_span = [delay_span]; %[delay_span;delay_bif]; delay = [delay_span delay_span delay_span]; % delay = [0.01 0.01 0.01]' x0 = [1 1 1]'; % delay = [0.1 0.1 0.1]; for i = 1:length(delay(:,1)) sol = d_cell_interaction(t,delay(i,:),x0,cd(3,:)); sol_data{i} = [sol.x;sol.y]; end figure; clear legends for k = 1:length(delay(:,1)) plot3(sol_data{k}(2,:),sol_data{k}(3,:),sol_data{k}(4,:)) xlabel('Malignant cells density') ylabel('Hunting cells density') zlabel('Resting cells density') hold on end plot3(sol_xstar_mat{1,3}(1),sol_xstar_mat{1,3}(2),sol_xstar_mat{1,3}(3),'r.', 'MarkerSize', 15) plot3(sol_xstar_mat{3,3}(1),sol_xstar_mat{3,3}(2),sol_xstar_mat{3,3}(3),'r.', 'MarkerSize', 15) plot3(sol_xstar_mat{5,3}(1),sol_xstar_mat{5,3}(2),sol_xstar_mat{5,3}(3),'r.', 'MarkerSize', 15) legend('\tau = 0.1','\tau = 0.2','\tau = 0.3','\tau = 0.4','\tau = 0.5','\tau = 0.6','\tau = 0.7','\tau = 0.8','\tau bif.') grid on figure; for i = 1:length(delay(:,1)) plot(sol_data{i}(1,:),[sol_data{i}([2 4],:)]) title('Time response of \tau = 0.1.'); xlabel('time t'); ylabel('solution y'); legend('M','H','R') hold on end figure; for i = 1:length(delay(:,1)) subplot(311) plot(sol_data{i}(1,:),sol_data{i}(2,:)) % title('Time responses of (M,H,R) for different \tau'); ylabel('Malignant cell density'); hold on grid on subplot(312) plot(sol_data{i}(1,:),sol_data{i}(3,:)) ylabel('Hunting cell density'); grid on hold on subplot(313) plot(sol_data{i}(1,:),sol_data{i}(4,:)) ylabel('Resting cell density'); grid on hold on xlabel('Time [s]'); end legend('\tau = 0.1','\tau = 0.2','\tau = 0.3','\tau = 0.4','\tau = 0.5','\tau = 0.6','\tau = 0.7','\tau = 0.8') %% Lyapunov function syms x1 x2 x3 p11 p12 p13 p22 p23 p33 Q = eye(3); x1_star = 0.8260; x2_star = 1.7325; x3_star = 0.0833; xi = [x1-x1_star;x2-x2_star;x3-x3_star]; x = [x1;x2;x3]; P = [p11 p12 p13; p12 p22 p23 p13 p23 p33]; f = transpose(A_num_cd3)*P+P*A_num_cd3; solveforP = f+Q == 0; solved_p = solve(solveforP, [p11 p12 p13 p22 p23 p33]); P = double([solved_p.p11 solved_p.p12 solved_p.p13; solved_p.p12 solved_p.p22 solved_p.p23; solved_p.p13 solved_p.p23 solved_p.p33]); V = transpose(xi)*P*(xi); %% Stabilizing controller % Find fixed point including u_star syms M_star H_star R_star a1 a2 a3 a4 a5 a6 a7 u_star M_star_num = 0.1; c = subs(coef,coef,cd3); f1 = 1+c(1)*M_star_num*(1-M_star_num)-c(2)*M_star_num*H_star == 0; f2 = c(3)*H_star*R_star-c(4)*H_star == 0; f3 = c(5)*R_star*(1-R_star)-c(6)*H_star*R_star-c(7)*R_star+u_star == 0; [H_star_num,u_star_num,R_star_num] = solve([f1,f2,f3],[H_star,u_star,R_star]); fixed_stab = double([M_star_num,H_star_num,R_star_num,u_star_num]); % Linearize f1_lin = 1+c(1)*M_star*(1-M_star)-c(2)*M_star*H_star; f2_lin = c(3)*H_star*R_star-c(4)*H_star; f3_lin = c(5)*R_star*(1-R_star)-c(6)*H_star*R_star-(c(7)*R_star)+u_star; A = jacobian([f1_lin,f2_lin,f3_lin],[M_star,H_star,R_star]); B = jacobian([f1_lin,f2_lin,f3_lin],u_star); A_num = double(subs(A,[M_star H_star R_star u_star],fixed_stab)); B_num = double(B); % Check controllability Contr_mat = ctrb(A_num,B_num); % Design K such that A-BK is Hurwitz % pole_loc = [-20 -21 -22]; % pole_loc = [-1 -2 -10]; pole_loc = [-1 -10 -0.1]; K = place(A_num,B_num,pole_loc); hurwitz = eig(A_num-B_num*K); F = -K; % Check whether the controlled system stays in positive invariant set P states_star = fixed_stab(1:end-1); input_star = fixed_stab(end); syms k1 k2 k3 M H R k = [k1 k2 k3]; f1 = -k * [M-fixed_stab(1);-fixed_stab(2);-fixed_stab(3)]+fixed_stab(4) &gt; 0; f2 = -k * [-fixed_stab(1);H-fixed_stab(2);-fixed_stab(3)]+fixed_stab(4) &gt; 0; f3 = -k * [-fixed_stab(1);-fixed_stab(2);R-fixed_stab(3)]+fixed_stab(4) &gt; 0; %% Simulation controlled system t = 0:0.001:100; % Define initial conditions with first a linear spacing and then some % arbitrary points of interest xi0_1= (-0.05:0.025:0.05); % Perturbation of Malignant cells xi0_2 = (-0.05:0.025:0.05); % Perturbation of Hunting cells xi0_3 = (-0.05:0.025:0.05); % Perturbation of Resting cells xi0 = [xi0_1;xi0_2;xi0_3]; %%% solver %%% for p = 1:length(xi0) x = stab_cell_interaction(t,xi0(:,p),A_num,B_num,K); solx_stab{p} = [x(:,1) x(:,2) x(:,3) ]; end %%% 3D trajetory plot %%% figure() for i = 1:length(solx_stab) plot3(solx_stab{i}(:,1),solx_stab{i}(:,2),solx_stab{i}(:,3)) hold on end grid on xlabel('Malignant cells density perturbation') ylabel('Hunting cells density perturbation') zlabel('Resting cells density perturbation') plotTweak(); %% From perturbation to actual state simulation plot 3D figure() for i = 1:length(solx_stab) plot3(solx_stab{i}(:,1)+M_star_num,solx_stab{i}(:,2)+double(H_star_num),solx_stab{i}(:,3)+double(R_star_num)) hold on end plot3(fixed_stab(1),fixed_stab(2),fixed_stab(3),'r.', 'MarkerSize', 15) grid on xlabel('Malignant cells density') ylabel('Hunting cells density') zlabel('Resting cells density') legend('\xi = -0.05','\xi=-0.025','\xi=0','\xi=0.025','\xi=0.05') plotTweak(); F = -K; % Plot input figure() for i = 1:length(solx_stab) u_traj = fixed_stab(4) - K(1)*[solx_stab{i}(:,1)]-K(2)*[solx_stab{i}(:,2)]- K(3)*[solx_stab{i}(:,3)]; plot(t,u_traj) hold on grid on end xlabel('Time [s]') ylabel('Control input u(t)') legend('\xi = -0.05','\xi=-0.025','\xi=0','\xi=0.025','\xi=0.05') plotTweak(); %% Commands % set(fig,'renderer','Painters') % saveas(fig,'figName','epsc') Functions cell_interaction.m function x = cell_interaction(t,x0,cd) [t,x] = ode45(@coupledfun,t,x0,[],cd); end coupledfun.m function dxdt = coupledfun(t,x0,cd) x1 = x0(1); x2 = x0(2); x3 = x0(3); dx1dt = 1+cd(1)*x1*(1-x1)-cd(2)*x1*x2; dx2dt = cd(3)*x2*x3-cd(4)*x2; dx3dt = cd(5)*x3*(1-x3)-cd(6)*x2*x3-cd(7)*x3; dxdt = [dx1dt;dx2dt;dx3dt]; end d_cell_interaction.m function y = d_cell_interaction(t,delay,x0,cd) %D_CELL_INTERACTION Summary of this function goes here % Detailed explanation goes here y = dde23(@d_coupledfun,delay,x0,[t(1), t(end)],[],cd); end d_history.m function s = d_history(t,x0) % Constant history function for DDEX1. s = x0; end d_coupledfun.m function dydt = d_coupledfun(t,y,Z,cd) % Differential equations function for DDEX1. x_lagM = Z(1); x_lagH = Z(2); x_lagR = Z(3); x1 = y(1); x2 = y(2); x3 = y(3); dx1dt = 1+cd(1)*x1*(1-x1)-cd(2)*x1*x2; dx2dt = cd(3)*x_lagH*x_lagR-cd(4)*x2; dx3dt = cd(5)*x3*(1-x3)-cd(6)*x2*x3-cd(7)*x3; dydt = [dx1dt;dx2dt;dx3dt]; stab_cell_interaction.m function x = stab_cell_interaction(t,x0,A_num,B_num,K) [t,x] = ode45(@stab_coupledfun,t,x0,[],A_num,B_num,K); end stab_coupledfun.m function dxdt = stab_coupledfun(t,x0,A_num,B_num,K) x1 = x0(1); x2 = x0(2); x3 = x0(3); dx1dt = A_num(1,:)*[x1;x2;x3] + B_num(1)*(-K*[x1;x2;x3]); dx2dt = A_num(2,:)*[x1;x2;x3] + B_num(2)*(-K*[x1;x2;x3]); dx3dt = A_num(3,:)*[x1;x2;x3] + B_num(3)*(-K*[x1;x2;x3]); dxdt = [dx1dt;dx2dt;dx3dt]; end Resources Books [1.] Khalil, H.K. (2014) Nonlinear systems. Upper Saddle River, NJ: Prentice Hall. Websites Tutorial on delayed system simulation]]></summary></entry><entry><title type="html">Bayesian Optimization for discrete hyperparameters in NARX model</title><link href="https://wind-m05.github.io/2022/08/26/Bayesian-Optimization-for-Deep-Neural-Networks.html" rel="alternate" type="text/html" title="Bayesian Optimization for discrete hyperparameters in NARX model" /><published>2022-08-26T00:00:00+00:00</published><updated>2022-08-26T00:00:00+00:00</updated><id>https://wind-m05.github.io/2022/08/26/Bayesian-Optimization-for-Deep-Neural-Networks</id><content type="html" xml:base="https://wind-m05.github.io/2022/08/26/Bayesian-Optimization-for-Deep-Neural-Networks.html"><![CDATA[<p>Still an ongoing project for discrete hyperparameter tuning of an Artificial Neural Network (ANN) with Bayesian Optimization (BO), where the number of nodes and number of input and output samples are being optimized for prediction and simulation error.</p>

<h3 id="why-this-project">Why this project?</h3>
<p>This project is purely for educational purposes and to learn how to use BO for discrete hyperparameters. The ANN will consist of 1 hidden layer and a varying amount of nodes to be optimized.</p>

<!-- ## Personal objective
During my study I did the subject "Machine Learning for systems and control" which involved many different Machine Learning techniques like Gaussian processes, Artificial Neural networks, Reinforcement learning techniques like Q-learning and Actor-Critic based method all inside one large group project. During the group assignment I mainly focussed on Gaussian processes and Actor-critic reinforcement learning. For that reason I would like to continue learning more about the implementations of Deep Neural Networks and the benefits of using Bayesian optimization for hyperparameter tuning. -->

<h2 id="background">Background</h2>
<p>Using machine learning techniques date as far back as 1943, but implementations were often put aside due to lack of computational power. Nowadays, this computational power has been drastically increasing and therefore things are suddenly much more accessible than before. The idea with this project is to perform data-driven modeling of an actuated pendulum also called the “Unbalanced disk”. More information about the setup and the datasets used you can check out <a href="https://github.com/GerbenBeintema/gym-unbalanced-disk">@GerbenBeintema</a>. The dynamics of the pendulum will be encapsulated by an <a href="https://en.wikipedia.org/wiki/Artificial_neural_network">Artificial Neural Network</a> (ANN) that optimizes its neurons such that it can predict and simulate how the pendulum behaves. Designing such an ANN comes with a lot of guessing and hand tuning what the amount of hidden layers and the amount of neurons should be. This project is about trying to give an educated guess what the optimal amount of nodes are for an ANN with 1 hidden layer. <a href="https://en.wikipedia.org/wiki/Bayesian_optimization#:~:text=Bayesian%20optimization%20is%20a%20sequential,expensive%2Dto%2Devaluate%20functions.">Bayesian Optimization</a> (BO) will be used to optimize the amount of nodes. This optimization technique uses <a href="https://en.wikipedia.org/wiki/Gaussian_process">Gaussian Processes</a> (GP), because they are able to track uncertainties, which you need to be able to guess good query points based upon the exploration/exploitation trade-off. The benefit of using BO instead of for example <a href="https://en.wikipedia.org/wiki/Hyperparameter_optimization">Grid Search</a> is that you need less trials to get to the same or even better result. The problem is that normal bayesian optimization relies on your hyperparameters to be continuous (which is normally a good thing), but for hyperparameters that are discrete in nature like the number of nodes or hidden layers of an ANN it is much more challenging.</p>

<h2 id="goal">Goal</h2>
<p>Show that by using discrete BO on #nodes and #input and output samples yield better results within 1 hour of optimizing on a GTX 1080 GPU than using random search. This all will be done with a standard ANN with 1 hidden layer.</p>

<!-- ### First principle modeling
$$
\begin{aligned}
\dot{\theta}(t) &=\omega(t) \\
\dot{\omega}(t) &=-\frac{M g l}{J} \sin (\theta(t))-\frac{1}{\tau} \omega(t)+\frac{K_{m}}{\tau} u(t)
\end{aligned}
$$ -->

<h3 id="narx-model">NARX model</h3>
<p>A <a href="https://en.wikipedia.org/wiki/Nonlinear_autoregressive_exogenous_model#:~:text=In%20time%20series%20modeling%2C%20a,of%20the%20same%20series%3B%20and">Nonlinear Autoregressive Exogenous</a> (NARX) model is used to describe the dynamics of the system.</p>

<p><img src="/assets/images/bayes-opt-discrete/narx_jpg.jpg" alt="NARX_model" /></p>

\[y_k = f(u_{k-1},u_{k-2},...,u_{k-n_b},y_{k-1},y_{k-2},...,y_{k-n_a}) + e_k\]

<p>Where $y_k$ is the output of the model and $u_k$ is the input. The e_k term is the white-noise that influences $y_k$ and therefore also all its past values. The amount of past inputs that are taken into account are denoted by $n_b$ and the amount of past outputs that are taken into account to determine the current output are denoted by $n_a$. Both $n_a$ and $n_b$ are considered hyperparameters, because the amount of information needed to accurately describe the dynamics are both system and data dependent.</p>
<h3 id="artificial-neural-network-structure">Artificial neural network structure</h3>

<p><img src="/assets/images/bayes-opt-discrete/ann_struct.jpg" alt="NARX_model" /></p>

<h3 id="prediction-and-simulation">Prediction and simulation</h3>
<p>Coming soon</p>

<h1 id="implementation">Implementation</h1>
<p>Currently the different libraries, <a href="https://github.com/wind-m05/BayesianOptimization">bayes_opt</a> , <a href="https://botorch.org/">botorch</a> and <a href="https://ax.dev/">Ax</a> are experimented with and checked for suitability for discrete hyperparameter tuning.</p>

<h3 id="hyper-parameters">Hyper parameters</h3>
<p><code class="language-plaintext warning highlighter-rouge">Work in progress</code></p>
<h3 id="acquisition-function">Acquisition function</h3>
<p><code class="language-plaintext warning highlighter-rouge">Work in progress</code></p>
<h3 id="kernel-selection">Kernel selection</h3>
<p><code class="language-plaintext warning highlighter-rouge">Work in progress</code></p>
<h3 id="activation-function-selection">Activation function selection</h3>
<p><code class="language-plaintext warning highlighter-rouge">Work in progress</code></p>]]></content><author><name>Michiel</name></author><category term="Bayesian-Optimization" /><category term="Gaussian-Processes" /><category term="Neural-Networks" /><category term="Machine-Learning" /><summary type="html"><![CDATA[Still an ongoing project for discrete hyperparameter tuning of an Artificial Neural Network (ANN) with Bayesian Optimization (BO), where the number of nodes and number of input and output samples are being optimized for prediction and simulation error. Why this project? This project is purely for educational purposes and to learn how to use BO for discrete hyperparameters. The ANN will consist of 1 hidden layer and a varying amount of nodes to be optimized. Background Using machine learning techniques date as far back as 1943, but implementations were often put aside due to lack of computational power. Nowadays, this computational power has been drastically increasing and therefore things are suddenly much more accessible than before. The idea with this project is to perform data-driven modeling of an actuated pendulum also called the “Unbalanced disk”. More information about the setup and the datasets used you can check out @GerbenBeintema. The dynamics of the pendulum will be encapsulated by an Artificial Neural Network (ANN) that optimizes its neurons such that it can predict and simulate how the pendulum behaves. Designing such an ANN comes with a lot of guessing and hand tuning what the amount of hidden layers and the amount of neurons should be. This project is about trying to give an educated guess what the optimal amount of nodes are for an ANN with 1 hidden layer. Bayesian Optimization (BO) will be used to optimize the amount of nodes. This optimization technique uses Gaussian Processes (GP), because they are able to track uncertainties, which you need to be able to guess good query points based upon the exploration/exploitation trade-off. The benefit of using BO instead of for example Grid Search is that you need less trials to get to the same or even better result. The problem is that normal bayesian optimization relies on your hyperparameters to be continuous (which is normally a good thing), but for hyperparameters that are discrete in nature like the number of nodes or hidden layers of an ANN it is much more challenging. Goal Show that by using discrete BO on #nodes and #input and output samples yield better results within 1 hour of optimizing on a GTX 1080 GPU than using random search. This all will be done with a standard ANN with 1 hidden layer. NARX model A Nonlinear Autoregressive Exogenous (NARX) model is used to describe the dynamics of the system. \[y_k = f(u_{k-1},u_{k-2},...,u_{k-n_b},y_{k-1},y_{k-2},...,y_{k-n_a}) + e_k\] Where $y_k$ is the output of the model and $u_k$ is the input. The e_k term is the white-noise that influences $y_k$ and therefore also all its past values. The amount of past inputs that are taken into account are denoted by $n_b$ and the amount of past outputs that are taken into account to determine the current output are denoted by $n_a$. Both $n_a$ and $n_b$ are considered hyperparameters, because the amount of information needed to accurately describe the dynamics are both system and data dependent. Artificial neural network structure Prediction and simulation Coming soon Implementation Currently the different libraries, bayes_opt , botorch and Ax are experimented with and checked for suitability for discrete hyperparameter tuning. Hyper parameters Work in progress Acquisition function Work in progress Kernel selection Work in progress Activation function selection Work in progress]]></summary></entry></feed>