x = Matrices.leastSquares(A,b);
A linear system of equations A*x = b has no solutions or infinitely many solutions if A is not square. Function "leastSquares" returns a solution in a least squarse sense:
size(A,1) > size(A,2): returns x such that |A*x - b|^2 is a minimum size(A,1) = size(A,2): returns x such that A*x = b size(A,1) < size(A,2): returns x such that |x|^2 is a minimum for all vectors x that fulfill A*x = b
The solution is computed with the LAPACK function "dgelsx", i.e., QR or LQ factorization of A with column pivoting. If A does not have full rank, the solution is not unique and from the infinitely many solutions the one is selected that minimizes both |x|^2 and |A*x - b|^2.
function leastSquares extends Modelica.Icons.Function; input Real A[:, :] "Matrix A"; input Real b[size(A, 1)] "Vector b"; output Real x[size(A, 2)] "Vector x such that min|A*x-b|^2 if size(A,1) >= size(A,2) or min|x|^2 and A*x=b, if size(A,1) < size(A,2)"; end leastSquares;