leastSquares2

Solve linear equation A*X = B (exactly if possible, or otherwise in a least square sense; A may be non-square and may be rank deficient)

Information

This information is part of the Modelica Standard Library maintained by the Modelica Association.

Syntax

X = Matrices.leastSquares2(A,B);

Description

Returns a solution of equation A*X = B in a least square sense (A may be rank deficient):

minimize | A*X - B |

Several different cases can be distinguished (note, rank is an output argument of this function):

size(A,1) = size(A,2)

A solution is returned for a regular, as well as a singular matrix A:

  • rank = size(A,1):
    A is regular and the returned solution X fulfills the equation A*X = B uniquely.
  • rank < size(A,1):
    A is singular and no unique solution for equation A*X = B exists.
    • If an infinite number of solutions exists, the one is selected that fulfills the equation and at the same time has the minimum norm |x| for all solution vectors that fulfill the equation.
    • If no solution exists, X is selected such that |A*X - B| is as small as possible (but A*X - B is not zero).

size(A,1) > size(A,2):

The equation A*X = B has no unique solution. The solution X is selected such that |A*X - B| is as small as possible. If rank = size(A,2), this minimum norm solution is unique. If rank < size(A,2), there are an infinite number of solutions leading to the same minimum value of |A*X - B|. From these infinite number of solutions, the one with the minimum norm |X| is selected. This gives a unique solution that minimizes both |A*X - B| and |X|.

size(A,1) < size(A,2):

  • rank = size(A,1):
    There are an infinite number of solutions that fulfill the equation A*X = B. From this infinite number, the unique solution is selected that minimizes |X|.
  • rank < size(A,1):
    There is either no solution of equation A*X = B, or there are again an infinite number of solutions. The unique solution X is returned that minimizes both |A*X - B| and |X|.

Note, the solution is computed with the LAPACK function "dgelsy", i.e., QR or LQ factorization of A with column pivoting.

Algorithmic details

The function first computes a QR factorization with column pivoting:

A * P = Q * [ R11 R12 ]
            [  0  R22 ]

with R11 defined as the largest leading submatrix whose estimated condition number is less than 1/rcond. The order of R11, rank, is the effective rank of A.

Then, R22 is considered to be negligible, and R12 is annihilated by orthogonal transformations from the right, arriving at the complete orthogonal factorization:

A * P = Q * [ T11 0 ] * Z
            [  0  0 ]

The minimum-norm solution is then

X = P * Z' [ inv(T11)*Q1'*B ]
           [        0       ]

where Q1 consists of the first "rank" columns of Q.

See also

Matrices.leastSquares (same as leastSquares2, but with a right hand side vector),
Matrices.solve2 (for square, regular matrices A)

Syntax

(X, rank) = leastSquares2(A, B, rcond)

Inputs (3)

A

Type: Real[:,:]

Description: Matrix A

B

Type: Real[size(A, 1),:]

Description: Matrix B

rcond

Default Value: 100 * Modelica.Constants.eps

Type: Real

Description: Reciprocal condition number to estimate rank of A

Outputs (2)

X

Type: Real[size(A, 2),size(B, 2)]

Description: Matrix 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)

rank

Type: Integer

Description: Rank of A