---
layout: post
title: Model Reference Adaptive Control
comments: true
permalink: mrac
---
<hr />
<p>
Consider the following first-order linear system:
</p>
\begin{equation}
\dot{x} =  a x + bu,
\label{eq:truesystem}
\end{equation}
<p>
where \(a,\,b\) are unknown parameters. However, the sign of \(b\) is known.
</p>
<p>
Suppose that we want the linear system \eqref{eq:truesystem} to track the following reference system:
</p>
\begin{equation}
\dot{x_m} =  a_m x_m + b_m r,
\label{eq:refsystem}
\end{equation}
<p>
where \(a_m < 0\) and \(b_m\) are known and \(r(t)\) is a known reference signal.
</p>
<!--break-->
<p>
Suppose we use the following control law:
</p>
\begin{equation}
u = \hat k_x x  + \hat k_r r.
\end{equation}
<p>
Then the dynamic of system \eqref{eq:truesystem} will be 
</p>
\begin{equation}
\dot{x} = (a + b \hat k_x) x + b \hat k_r r.
\end{equation}
<p>
Therefore, if we can make
</p>
\begin{equation}
\hat k_x = k_x \triangleq \frac{a_m-a}{b}\text{ and }\hat k_r = k_r \triangleq \frac{b_m}{b},
\end{equation}
<p>
then the dynamic of system \eqref{eq:truesystem} will follow the reference system. Let us denote \(\Delta k_x \triangleq k_x - \hat k_x\) and \(\Delta k_r \triangleq k_r - \hat k_r\).
</p>
<p>
Now consider the dynamic of the error \(e \triangleq x - x_m\), we have
</p>
\begin{align*}
\dot{e}& =  (a + b \hat k_x) x + b \hat k_r r - a_m x_m - b_m r\\
& = a_m(x - x_m) - b \Delta k_x x - b \Delta k_r r \\
& = a_m e - b \Delta k_x x - b \Delta k_r r .
\end{align*}
<p>
Therefore, if we consider the following candidate Lyapunov function
</p>
\begin{equation}
V = \frac{1}{2}\left(e^2 + |b|(\gamma_x^{-1} \Delta k_x^2 + \gamma_r^{-1} \Delta k_r^2)\right),
\end{equation}
<p>
where \(\gamma_x,\,\gamma_r > 0\). Then we have 
</p>
\begin{align*}
\dot{V} = a_m e^2 &- |b|\Delta k_x \left(\text{sgn}(b)ex + \gamma_x^{-1}\dot{\hat k_x}\right) \\
&-  |b|\Delta k_r \left(\text{sgn}(b)er + \gamma_r^{-1}\dot{\hat k_r}\right).
\end{align*}
<p>
Therefore, if we use the following update rule on \(\hat k_x\) and \(\hat k_r\):
</p>
\begin{equation}
\dot{\hat k_x} = -\gamma_x \text{sgn}(b) ex\text{ and }\dot{\hat k_r} = -\gamma_r \text{sgn}(b) er,\\
\end{equation}
<p>
then \(\dot{V} = a_m e^2 \leq 0\). One can then prove that \(\ddot{V} =2 a_m e \dot{e}\) is bounded and thus \(\lim_{t\rightarrow\infty} \dot{V}  = 0\) and hence \(e \rightarrow 0\).
</p>
<div id="outline-container-orgd5024ad" class="outline-2">
<h2 id="orgd5024ad">Simulation</h2>
<div class="outline-text-2" id="text-orgd5024ad">
<p>
Assume that the system is given as
</p>
\begin{equation}
\dot{x} = x + u,
\end{equation}
<p>
while we want the system to tract the following system:
</p>
\begin{equation}
\dot{x_m} = - x_m + r = - x_m + \sin(t).
\end{equation}
<div class="org-src-container">
<pre class="src src-python"><span style="color: #F0DFAF; font-weight: bold;">import</span> numpy <span style="color: #F0DFAF; font-weight: bold;">as</span> np
<span style="color: #F0DFAF; font-weight: bold;">import</span> matplotlib
<span style="color: #F0DFAF; font-weight: bold;">import</span> matplotlib.pyplot <span style="color: #F0DFAF; font-weight: bold;">as</span> plt
<span style="color: #F0DFAF; font-weight: bold;">import</span> seaborn <span style="color: #F0DFAF; font-weight: bold;">as</span> sns <span style="color: #5F7F5F;"># </span><span style="color: #7F9F7F;">for the sake of appreances</span>
<span style="color: #F0DFAF; font-weight: bold;">from</span> scipy.integrate <span style="color: #F0DFAF; font-weight: bold;">import</span> odeint
<span style="color: #DFAF8F;">a</span> = 1.
<span style="color: #DFAF8F;">b</span> = 1.
<span style="color: #DFAF8F;">am</span> = -1.
<span style="color: #DFAF8F;">bm</span> = 1.
<span style="color: #DFAF8F;">gammax</span> = 10.
<span style="color: #DFAF8F;">gammar</span> = 10.
<span style="color: #5F7F5F;"># </span><span style="color: #7F9F7F;">y is a vector of [x ,x_m, \hat k_x, \hat k_r]</span>
<span style="color: #F0DFAF; font-weight: bold;">def</span> <span style="color: #93E0E3;">f</span>(y, t):
    <span style="color: #DFAF8F;">x</span> = y[0]
    <span style="color: #DFAF8F;">xm</span> = y[1]
    <span style="color: #DFAF8F;">hatkx</span> = y[2]
    <span style="color: #DFAF8F;">hatkr</span> = y[3]
    <span style="color: #DFAF8F;">r</span> = np.sin(t)
    <span style="color: #DFAF8F;">e</span> = x - xm
    <span style="color: #DFAF8F;">u</span> = hatkx * x + hatkr * r
    <span style="color: #DFAF8F;">f0</span> = a * x + b * u
    <span style="color: #DFAF8F;">f1</span> = am * xm + bm * r
    <span style="color: #DFAF8F;">f2</span> = -1 * gammax * e * x
    <span style="color: #DFAF8F;">f3</span> = -1 * gammar * e * r
    <span style="color: #F0DFAF; font-weight: bold;">return</span> [f0, f1, f2, f3]
<span style="color: #DFAF8F;">t</span> = np.linspace(0, 20., 10000) 
<span style="color: #DFAF8F;">x0</span> = 0.
<span style="color: #DFAF8F;">xm0</span> = 0.
<span style="color: #DFAF8F;">hatkx0</span> = 0.
<span style="color: #DFAF8F;">hatkr0</span> = 0.
<span style="color: #DFAF8F;">y0</span> = [x0, xm0, hatkx0, hatkr0]
<span style="color: #DFAF8F;">solution</span> = odeint(f, y0, t)
<span style="color: #DFAF8F;">x</span> = solution[:, 0]
<span style="color: #DFAF8F;">xm</span> = solution[:, 1]
<span style="color: #DFAF8F;">hatkx</span> = solution[:, 2]
<span style="color: #DFAF8F;">hatkr</span> = solution[:, 3]
<span style="color: #DFAF8F;">fig</span> = plt.figure()
plt.rc(<span style="color: #CC9393;">'text'</span>, usetex=<span style="color: #BFEBBF;">True</span>)
plt.plot(t, np.sin(t), label=r<span style="color: #CC9393;">'$r(t)$'</span>)
plt.plot(t, x, label=r<span style="color: #CC9393;">'$x(t)$'</span>)
plt.plot(t, xm, label=r<span style="color: #CC9393;">'$x_m(t)$'</span>)
plt.plot(t, hatkx, label=r<span style="color: #CC9393;">'$\hat k_x(t)$'</span>)
plt.plot(t, hatkr, label=r<span style="color: #CC9393;">'$\hat k_r(t)$'</span>)
plt.xlabel(<span style="color: #CC9393;">'Time(t)'</span>)
plt.legend(loc = 0)
fig.tight_layout()
plt.savefig(<span style="color: #CC9393;">'../../public/mrac.png'</span>)
<span style="color: #F0DFAF; font-weight: bold;">return</span> <span style="color: #CC9393;">'../../public/mrac.png'</span> <span style="color: #5F7F5F;"># </span><span style="color: #7F9F7F;">return the filename to org-mode</span>
</pre>
</div>
<p>
<a href=""></a>
</p>
</div>
</div>