# qr

## Purpose

Orthogonal-triangular decomposition.

## Synopsis

    [Q,R] = qr(X)
    [Q,R,E] = qr(X)
    A = qr(X)

## Description

`qr` performs the orthogonal-triangular decomposition of a matrix. This factorization is useful for both square and rectangular matrices. It expresses the matrix as the product of a real orthonormal or complex unitary matrix and an upper triangular matrix.

`[Q,R] = qr(X)` produces an upper triangular matrix `R` of the same dimension as `X` and a unitary matrix `Q` so that `X = Q`*`R`.

`[Q,R,E] = qr(X)` produces a permutation matrix `E`, an upper triangular matrix`R` with decreasing diagonal elements, and a unitary matrix `Q` so that `X`*`E = Q`*`R`.

`A` `=` `qr(X)` returns the output of the LINPACK subroutine `ZQRDC`. `triu(qr(X))` is `R`.

## Examples

Start with

    A =
         1     2     3
         4     5     6
         7     8     9
        10    11    12

This is a rank-deficient matrix; the middle column is the average of the other two columns. The rank deficiency is revealed by the factorization:

    [Q,R] = qr(A)

    Q =
        -0.0776    -0.8331     0.5444     0.0605
        -0.3105    -0.4512    -0.7709     0.3251
        -0.5433    -0.0694    -0.0913    -0.8317
        -0.7762     0.3124     0.3178     0.4461

    R =
        -12.8841    -14.5916    -16.2992
               0     -1.0413     -2.0826
               0           0      0.0000
               0           0           0

The triangular structure of `R` gives it zeros below the diagonal; the zero on the diagonal in `R(3,3)` implies that `R`, and consequently `A`, does not have full rank.

The QR factorization is used to solve linear systems with more equations than unknowns. For example

    b =
        1
        3
        5
        7

The linear system Ax = b represents four equations in only three unknowns. The best solution in a least squares sense is computed by

    x = A\b

which produces

    Warning: Rank deficient, rank = 2, tol = 1.4594E-014

    x =
        0.5000
        0.0000
        0.1667

The quantity `tol` is a tolerance used in deciding that a diagonal element of `R` is negligible. If `[Q,R,E] = qr(A)`, then

    tol = max(size(A))*eps*abs(R(1,1))

The solution `x` was computed using the factorization and the two steps

    y = Q'*b;
    x = R\y

The computed solution can be checked by forming Ax. This equals b to within roundoff error, which indicates that even though the simultaneous equations Ax = b are overdetermined and rank deficient, they happen to be consistent. There are infinitely many solution vectors `x`; the QR factorization has found just one of them.

## Algorithm

`qr` uses the LINPACK routines `ZQRDC` and `ZQRSL`. `ZQRDC` computes the QR decomposition, while `ZQRSL` applies the decomposition.

## See Also

    \, /, lu, null, orth, qrdelete, qrinsert

## References

J.J. Dongarra, J.R. Bunch, C.B. Moler, and G.W. Stewart, LINPACK User's Guide, SIAM, Philadelphia, 1979.

(c) Copyright 1994 by The MathWorks, Inc.