# $$\text{LeastSquares}$$¶

You can use the $$\text{LeastSquares}$$ function to calculate the least squares error solution to $$A x = y$$.

You can use the \leastsq backslash command to insert this function.

The following variants of this function are available:

• $$\text{real matrix } \text{LeastSquares} \left ( \text{<A>}, \text{<y>} \right )$$

The $$A$$ parameter represents a full rank matrix to use to calculate the solution. The $$y$$ parameter represents the solution to $$A x = y$$ that is to be used to calculate $$x$$. This function will find the matrix, $$x$$ that minimizes the value of

$\sum_i \left [ \left ( A x \right ) _ i - y _ i \right ]$

Equivalently, find the column matrix $$x$$ that minimizes $$\left \Vert A x - y \right \Vert _ 2$$ where $$\left \Vert x \right \Vert _ 2$$ represents the Euclidean norm of $$x$$.

When $$A$$ is an m-by-n matrix with $$m \geq n$$, we have an overdetermined system of simultaneous equations. In this scenario, the solution is found using QR factorization of matrix $$A$$.

When $$m < n$$, we have an underdetermined system with an infinite number of possible solutions that satisfy $$A x = y$$. In this scenario, the $$\text{LeastSquares}$$ function uses LQ factorization to find the solution that minimizes $$\left \Vert A x - y \right \Vert _ 2$$.

Figure 164 shows the basic use of the $$\text{LeastSquares}$$ function.

Figure 164 Example Use Of The LeastSquares Function