[Math] Batch Least squares

least squareslinear algebrapseudoinversesystem identification

I need to solve in Matlab a Least squares problem,

\begin{equation}
\begin{bmatrix}
x^2 \\ ux \\ u^2
\end{bmatrix}^T\begin{bmatrix}
H_{xx} \\ 2H_{xu} \\ H_{uu}
\end{bmatrix} = y
\end{equation}

Set $z= \begin{bmatrix}
x^2 \\ ux \\ u^2
\end{bmatrix}$
, $H = \begin{bmatrix}
H_{xx} \\ 2H_{xu} \\ H_{uu}
\end{bmatrix}$
, where $H$ is the vector of unknowns. As I have q=3 unknowns, I need to collect at least $N\geq q$ data samples:
\begin{equation}
\Phi = [z^1 \ z^2 \ z^3]
\end{equation}

where the data are collected such that the rank condition is satisfied (rank($\Phi$)=q) and
\begin{equation}
Y = \begin{bmatrix}y^1 \\ y^2 \\ y^3\end{bmatrix}
\end{equation}

The Least squares solution is then
\begin{equation}
H = (\Phi^T\Phi)^{-1}\Phi^T Y
\end{equation}

However, the computed H is too big and cannot converge to the true value. This is probably because the matrix $\Phi$ is ill-conditioned (indeed cond(Phi) returns a number of order e+03). What am I missing?

This is the reference I am using: see Algorithm 2

Solved: there was a mistake in the collection of datasample $z^{i}$, so even though the rank condition was satisfied, its inverse was almost singular (indeed the condition number was very high).

Best Answer

Each sample is generated according to $z_i^T H = y_i$. If you have $N$ data points (each one consisting of a three-dimensional vector $z_i$ and an observation $y_i$), you collect them in an $N\times 3$ matrix $$\Phi =\begin{bmatrix} z_1^T\\ \vdots\\z_N^T\end{bmatrix},$$ and an $N\times 1$ vector $$y=\begin{bmatrix} y_1^T\\ \vdots\\y_N^T\end{bmatrix};$$ then, you find the least squares solution and $$\hat H = (\Phi^T \Phi)^{-1}\Phi y.$$

In matlab, you should use the division operation H_hat = Phi\y, which will choose the most appropriate method depending on the matrices (usually it relies on qr factorization).