# How should I interprent the Recursive Least Square algorithm?

This is the recursive least square algorithm.

$$\hat{\theta}(t) = \hat{\theta}(t-1) + K(t)(y(t) - \phi^T(t)\hat{\theta}(t-1)) \\ K(t) = P(t)\phi(t) \\ P(t) = (I-K(t)\phi^T(t))P(t-1)$$

As I know, I want to find $\hat{\theta}(t)$, $\hat{\theta}(t-1)$ is the past "what I want to find" and I need to set some initial conditions for $\hat{\theta}(t_0)$ and $P(t_0)$.

But what is the rest for the variables? My book only describe $K(t), \phi(t), P(t)$ as matrices and vectors. I cannot accept that as a explanation.

So, how do I use this algorithm? Where do I start?

Assume that we are going to curve fit this discrete transfer function.

$$G(z) = \frac{a_0z^{-1} + a_1}{b_0z^{-2} + b_1 z^{-1} + 1}$$

Where $$z^{-1} = (k+1)\\ z^{-2} = (k+2)$$

We can expand $G(z)$ into a discrete ODE:

$$y(k+0) + b_1y(k+1) + b_0y(k+2) = a_0u(k+1) + a_1u(k+0)$$

We want to let $y(k)$ to be alone.

$$y(k+0) =-b_0y(k+2) - b_1y(k+1)+a_0u(k+1) + a_1u(k+0)$$

Remember that I know $y(k)$ for all $k$ and also I know $u(k)$ for all $k$. It's the measurement.

What I don't know is $$b_0, b_1, a_0, a_1$$

So we first create the vectors $\phi(k)$ and $\hat{\theta}$. But we want to display $\phi(k)$ as a single row matrix.

$$\phi^T(k) = [-y(k+2) -y(k+1)+u(k+1) + u(k+0)]$$

And our goal with Recursive Least Square is to find: $$\hat{\theta^T} = [b_0 \space\space\space b_1 \space\space\space a_0 \space\space\space a_1]$$

Becuase:

$$y(k+0) = [-y(k+2) -y(k+1) u(k+1) + u(k+0)] \begin{bmatrix} b_0\\ b_1\\ a_0\\ a_1 \end{bmatrix} = \phi^T(k) \hat{\theta}$$

I have now introduce the definition of $\phi^T(k)$ and $\hat{\theta}$.

As you can see, the RLS algorithm shows $$\hat{\theta}(t-1)$$

It's a definition for the past $\hat{\theta}$ and $\hat{\theta}(k)$ is the definition of the current $\hat{\theta}$.

So what is $P$ then?. Well, they need to be computed too! To compute $P(k)$ we need to do this. This is the initial condition:

$$P(k_0) = (\Phi^T(k_0)\Phi(k_0))^{-1}$$

The definition of the matrix $\Phi(k_0)$ is:

$$\Phi(k_0) = \begin{bmatrix} -y(k_0+2) & -y(k_0+1) & u(k_0+1) &u(k_0+0) \\ -y(k_0+3) & -y(k_0+2) & u(k_0+2) &u(k_0+1) \\ -y(k_0+4) & -y(k_0+3) & u(k_0+3) &u(k_0+2) \\ \vdots & \vdots & \vdots &\vdots \\ -y(k_0+n) & -y(k_0+n-1) & u(k_0+n-1) &u(k_0+n-2) \\ \end{bmatrix}$$

Where $k_0$ is the beginning of the sample time and $n$ is the number of samples taken.

To compute the initial $\hat{\theta}$ we compute this as:

$$\hat{\theta}(k_0) = P(k_0)\Phi^T(k_0)Y(k_0)$$

And $Y(k_0)$ is

$$Y(k_0) = \begin{bmatrix} y(k_0 + 0) \\ y(k_0 + 1) \\ y(k_0 + 2)\\ \vdots \\ y(k_0 + n -2)\\ \end{bmatrix}$$

I how you understand this.

Then to compute $\hat{\theta}(k)$ we do this:

Step 1:

$$P(k) = P(k-1) - P(k-1)\phi(k)(I-\phi^T(k)P(k-1)\phi(k))^{-1}\phi^T(k)P(k-1)$$

Here the initial conditions are: $$P(k-1) = (\Phi^T(0)\Phi(0))^{-1}$$ and $$\phi(k) = \phi(1)$$

Step 2:

$$K(k) = P(1)\phi(1)$$

Step 3:

$$\hat{\theta}(k) = \hat{\theta}(k-1) + K(k)(y(k) - \phi^T(k)\hat{\theta}(k-1))$$

Where initial condition for the estimate is:

$$\hat{\theta}(k-1) = P(0)\Phi^T(0)Y(0)$$