Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_linsys_real_gen_lsqsol (f04am)

## Purpose

nag_linsys_real_gen_lsqsol (f04am) calculates the accurate least squares solution of a set of m$m$ linear equations in n$n$ unknowns, mn$m\ge n$ and rank = n$\text{}=n$, with multiple right-hand sides, AX = B$AX=B$, using a QR$QR$ factorization and iterative refinement.

## Syntax

[x, qr, alpha, ipiv, ifail] = f04am(a, b, eps, 'm', m, 'n', n, 'ir', ir)
[x, qr, alpha, ipiv, ifail] = nag_linsys_real_gen_lsqsol(a, b, eps, 'm', m, 'n', n, 'ir', ir)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: m has been made optional
.

## Description

To compute the least squares solution to a set of m$m$ linear equations in n$n$ unknowns (mn)$\left(m\ge n\right)$ AX = B$AX=B$, nag_linsys_real_gen_lsqsol (f04am) first computes a QR$QR$ factorization of A$A$ with column pivoting, AP = QR$AP=QR$, where R$R$ is upper triangular, Q$Q$ is an m$m$ by m$m$ orthogonal matrix, and P$P$ is a permutation matrix. QT${Q}^{\mathrm{T}}$ is applied to the m$m$ by r$r$ right-hand side matrix B$B$ to give C = QTB$C={Q}^{\mathrm{T}}B$, and the n$n$ by r$r$ solution matrix X$X$ is calculated, to a first approximation, by back-substitution in RX = C$RX=C$. The residual matrix S = BAX$S=B-AX$ is calculated using additional precision, and a correction D$D$ to X$X$ is computed as the least squares solution to AD = S$AD=S$. X$X$ is replaced by X + D$X+D$ and this iterative refinement of the solution is repeated until full machine accuracy has been obtained.

## References

Wilkinson J H and Reinsch C (1971) Handbook for Automatic Computation II, Linear Algebra Springer–Verlag

## Parameters

### Compulsory Input Parameters

1:     a(lda,n) – double array
lda, the first dimension of the array, must satisfy the constraint ldam$\mathit{lda}\ge {\mathbf{m}}$.
The m$m$ by n$n$ matrix A$A$.
2:     b(ldb,ir) – double array
ldb, the first dimension of the array, must satisfy the constraint ldbm$\mathit{ldb}\ge {\mathbf{m}}$.
The m$m$ by r$r$ right-hand side matrix B$B$.
3:     eps – double scalar
Must be set to the value of the machine precision.

### Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The first dimension of the arrays a, b. (An error is raised if these dimensions are not equal.)
m$m$, the number of rows of the matrix A$A$, i.e., the number of equations.
Constraint: m1${\mathbf{m}}\ge 1$.
2:     n – int64int32nag_int scalar
Default: The second dimension of the array a.
n$n$, the number of columns of the matrix A$A$, i.e., the number of unknowns.
Constraint: 0nm$0\le {\mathbf{n}}\le {\mathbf{m}}$.
3:     ir – int64int32nag_int scalar
Default: The second dimension of the array b.
r$r$, the number of right-hand sides.

### Input Parameters Omitted from the MATLAB Interface

lda ldx ldb ldqr e y z r

### Output Parameters

1:     x(ldx,ir) – double array
ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
The n$n$ by r$r$ solution matrix X$X$.
2:     qr(ldqr,n) – double array
ldqrm$\mathit{ldqr}\ge {\mathbf{m}}$.
Details of the QR$QR$ factorization.
3:     alpha(n) – double array
The diagonal elements of the upper triangular matrix R$R$.
4:     ipiv(n) – int64int32nag_int array
Details of the column interchanges.
5:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
The rank of A$A$ is less than n$n$; the problem does not have a unique solution.
ifail = 2${\mathbf{ifail}}=2$
The iterative refinement fails to converge, i.e., the matrix A$A$ is too ill-conditioned.

## Accuracy

Although the correction process is continued until the solution has converged to full machine accuracy, all the figures in the final solution may not be correct since the correction D$D$ to X$X$ is itself the solution to a linear least squares problem. For a detailed error analysis see page 116 of Wilkinson and Reinsch (1971).

The time taken by nag_linsys_real_gen_lsqsol (f04am) is approximately proportional to n2(3mn)${n}^{2}\left(3m-n\right)$, provided r$r$ is small compared with n$n$.

## Example

```function nag_linsys_real_gen_lsqsol_example
a = [1.1, 0.9;
1.2, 1;
1, 1];
b = [2.2;
2.3;
2.1];
epsilon = 1.111307226797642e-16;
[x, qr, alpha, ipiv, ifail] = nag_linsys_real_gen_lsqsol(a, b, epsilon)
```
```

x =

1.3010
0.7935

qr =

3.0105   -1.6697
1.2000   -0.1727
1.0000    0.1464

alpha =

-1.9105
0.1484

ipiv =

1
2

ifail =

0

```
```function f04am_example
a = [1.1, 0.9;
1.2, 1;
1, 1];
b = [2.2;
2.3;
2.1];
epsilon = 1.111307226797642e-16;
[x, qr, alpha, ipiv, ifail] = f04am(a, b, epsilon)
```
```

x =

1.3010
0.7935

qr =

3.0105   -1.6697
1.2000   -0.1727
1.0000    0.1464

alpha =

-1.9105
0.1484

ipiv =

1
2

ifail =

0

```