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_fit_glin_l1sol (e02ga)

## Purpose

nag_fit_glin_l1sol (e02ga) calculates an l1${l}_{1}$ solution to an over-determined system of linear equations.

## Syntax

[a, b, x, resid, irank, iter, ifail] = e02ga(a, b, 'm', m, 'nplus2', nplus2, 'toler', toler)
[a, b, x, resid, irank, iter, ifail] = nag_fit_glin_l1sol(a, b, 'm', m, 'nplus2', nplus2, 'toler', toler)

## Description

Given a matrix A$A$ with m$m$ rows and n$n$ columns (mn)$\left(m\ge n\right)$ and a vector b$b$ with m$m$ elements, the function calculates an l1${l}_{1}$ solution to the over-determined system of equations
 Ax = b. $Ax=b.$
That is to say, it calculates a vector x$x$, with n$n$ elements, which minimizes the l1${l}_{1}$ norm (the sum of the absolute values) of the residuals
 m r(x) = ∑ |ri|, i = 1
$r(x)=∑i=1m|ri|,$
where the residuals ri${r}_{i}$ are given by
 n ri = bi − ∑ aijxj,  i = 1,2, … ,m. j = 1
$ri=bi-∑j=1naijxj, i=1,2,…,m.$
Here aij${a}_{ij}$ is the element in row i$i$ and column j$j$ of A$A$, bi${b}_{i}$ is the i$i$th element of b$b$ and xj${x}_{j}$ the j$j$th element of x$x$. The matrix A$A$ need not be of full rank.
Typically in applications to data fitting, data consisting of m$m$ points with coordinates (ti,yi)$\left({t}_{i},{y}_{i}\right)$ are to be approximated in the l1${l}_{1}$ norm by a linear combination of known functions φj(t)${\varphi }_{j}\left(t\right)$,
 α1φ1(t) + α2φ2(t) + ⋯ + αnφn(t). $α1ϕ1(t)+α2ϕ2(t)+⋯+αnϕn(t).$
This is equivalent to fitting an l1${l}_{1}$ solution to the over-determined system of equations
 n ∑ φj(ti)αj = yi,  i = 1,2, … ,m. j = 1
$∑j=1nϕj(ti)αj=yi, i=1,2,…,m.$
Thus if, for each value of i$i$ and j$j$, the element aij${a}_{ij}$ of the matrix A$A$ in the previous paragraph is set equal to the value of φj(ti)${\varphi }_{j}\left({t}_{i}\right)$ and bi${b}_{i}$ is set equal to yi${y}_{i}$, the solution vector x$x$ will contain the required values of the αj${\alpha }_{j}$. Note that the independent variable t$t$ above can, instead, be a vector of several independent variables (this includes the case where each φi${\varphi }_{i}$ is a function of a different variable, or set of variables).
The algorithm is a modification of the simplex method of linear programming applied to the primal formulation of the l1${l}_{1}$ problem (see Barrodale and Roberts (1973) and Barrodale and Roberts (1974)). The modification allows several neighbouring simplex vertices to be passed through in a single iteration, providing a substantial improvement in efficiency.

## References

Barrodale I and Roberts F D K (1973) An improved algorithm for discrete l1${l}_{1}$ linear approximation SIAM J. Numer. Anal. 10 839–848
Barrodale I and Roberts F D K (1974) Solution of an overdetermined system of equations in the l1${l}_{1}$-norm Comm. ACM 17(6) 319–320

## Parameters

### Compulsory Input Parameters

1:     a(lda,nplus2) – double array
lda, the first dimension of the array, must satisfy the constraint ldam + 2$\mathit{lda}\ge {\mathbf{m}}+2$.
a(i,j)${\mathbf{a}}\left(\mathit{i},\mathit{j}\right)$ must contain aij${a}_{\mathit{i}\mathit{j}}$, the element in the i$\mathit{i}$th row and j$\mathit{j}$th column of the matrix A$A$, for i = 1,2,,m$\mathit{i}=1,2,\dots ,m$ and j = 1,2,,n$\mathit{j}=1,2,\dots ,n$. The remaining elements need not be set.
2:     b(m) – double array
m, the dimension of the array, must satisfy the constraint mn1${\mathbf{m}}\ge n\ge 1$.
b(i)${\mathbf{b}}\left(\mathit{i}\right)$ must contain bi${b}_{\mathit{i}}$, the i$\mathit{i}$th element of the vector b$b$, for i = 1,2,,m$\mathit{i}=1,2,\dots ,m$.

### Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The dimension of the array b.
The number of equations, m$m$ (the number of rows of the matrix A$A$).
Constraint: mn1${\mathbf{m}}\ge n\ge 1$.
2:     nplus2 – int64int32nag_int scalar
Default: The second dimension of the array a.
n + 2$n+2$, where n$n$ is the number of unknowns (the number of columns of the matrix A$A$).
Constraint: 3nplus2m + 2$3\le {\mathbf{nplus2}}\le {\mathbf{m}}+2$.
3:     toler – double scalar
A non-negative value. In general toler specifies a threshold below which numbers are regarded as zero. The recommended threshold value is ε2 / 3${\epsilon }^{2/3}$ where ε$\epsilon$ is the machine precision. The recommended value can be computed within the function by setting toler to zero. If premature termination occurs a larger value for toler may result in a valid solution.
Default: 0.0$0.0$.

lda iwork

### Output Parameters

1:     a(lda,nplus2) – double array
ldam + 2$\mathit{lda}\ge {\mathbf{m}}+2$.
Contains the last simplex tableau generated by the simplex method.
2:     b(m) – double array
The i$\mathit{i}$th residual ri${r}_{\mathit{i}}$ corresponding to the solution vector x$x$, for i = 1,2,,m$\mathit{i}=1,2,\dots ,m$.
3:     x(nplus2) – double array
x(j)${\mathbf{x}}\left(\mathit{j}\right)$ contains the j$\mathit{j}$th element of the solution vector x$x$, for j = 1,2,,n$\mathit{j}=1,2,\dots ,n$. The elements x(n + 1)${\mathbf{x}}\left(n+1\right)$ and x(n + 2)${\mathbf{x}}\left(n+2\right)$ are unused.
4:     resid – double scalar
The sum of the absolute values of the residuals for the solution vector x$x$.
5:     irank – int64int32nag_int scalar
The computed rank of the matrix A$A$.
6:     iter – int64int32nag_int scalar
The number of iterations taken by the simplex method.
7:     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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

W ifail = 1${\mathbf{ifail}}=1$
An optimal solution has been obtained but this may not be unique.
ifail = 2${\mathbf{ifail}}=2$
The calculations have terminated prematurely due to rounding errors. Experiment with larger values of toler or try scaling the columns of the matrix (see Section [Further Comments]).
ifail = 3${\mathbf{ifail}}=3$
 On entry, nplus2 < 3${\mathbf{nplus2}}<3$, or nplus2 > m + 2${\mathbf{nplus2}}>{\mathbf{m}}+2$, or lda < m + 2$\mathit{lda}<{\mathbf{m}}+2$.

## Accuracy

Experience suggests that the computational accuracy of the solution x$x$ is comparable with the accuracy that could be obtained by applying Gaussian elimination with partial pivoting to the n$n$ equations satisfied by this algorithm (i.e., those equations with zero residuals). The accuracy therefore varies with the conditioning of the problem, but has been found generally very satisfactory in practice.

The effects of m$m$ and n$n$ on the time and on the number of iterations in the Simplex Method vary from problem to problem, but typically the number of iterations is a small multiple of n$n$ and the total time taken is approximately proportional to mn2$m{n}^{2}$.
It is recommended that, before the function is entered, the columns of the matrix A$A$ are scaled so that the largest element in each column is of the order of unity. This should improve the conditioning of the matrix, and also enable the parameter toler to perform its correct function. The solution x$x$ obtained will then, of course, relate to the scaled form of the matrix. Thus if the scaling is such that, for each j = 1,2,,n$j=1,2,\dots ,n$, the elements of the j$j$th column are multiplied by the constant kj${k}_{j}$, the element xj${x}_{j}$ of the solution vector x$x$ must be multiplied by kj${k}_{j}$ if it is desired to recover the solution corresponding to the original matrix A$A$.

## Example

```function nag_fit_glin_l1sol_example
a = zeros(7, 5);
for i = 1:5
a(i, 1) = exp((i-1)/5);
a(i, 2) = exp(-(i-1)/5);
a(i, 3) = 1;
end
b = [4.501;
4.36;
4.333;
4.418;
4.625];
[aOut, bOut, x, resid, irank, iter, ifail] = nag_fit_glin_l1sol(a, b)
```
```

aOut =

2.4750    4.1341   -1.6591    1.0014    1.0000
3.6923    9.2006   -5.5083    2.0035    2.0000
-6.1673  -13.3347    6.1673    1.4960    3.0000
0.1213    0.7525    0.3688    0.0005    5.0000
0.3688   -0.7525    0.1213    0.0023   -7.0000
-0.5098   -1.0000   -0.5098    0.0028         0
8.0000   -6.0000   -4.0000         0         0

bOut =

0
0.0005
0
-0.0023
0

x =

1.0014
2.0035
1.4960
0
0

resid =

0.0028

irank =

3

iter =

5

ifail =

0

```
```function e02ga_example
a = zeros(7, 5);
for i = 1:5
a(i, 1) = exp((i-1)/5);
a(i, 2) = exp(-(i-1)/5);
a(i, 3) = 1;
end
b = [4.501;
4.36;
4.333;
4.418;
4.625];
[aOut, bOut, x, resid, irank, iter, ifail] = e02ga(a, b)
```
```

aOut =

2.4750    4.1341   -1.6591    1.0014    1.0000
3.6923    9.2006   -5.5083    2.0035    2.0000
-6.1673  -13.3347    6.1673    1.4960    3.0000
0.1213    0.7525    0.3688    0.0005    5.0000
0.3688   -0.7525    0.1213    0.0023   -7.0000
-0.5098   -1.0000   -0.5098    0.0028         0
8.0000   -6.0000   -4.0000         0         0

bOut =

0
0.0005
0
-0.0023
0

x =

1.0014
2.0035
1.4960
0
0

resid =

0.0028

irank =

3

iter =

5

ifail =

0

```