PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_linsys_real_gen_solve (f04jg)
Purpose
nag_linsys_real_gen_solve (f04jg) finds the solution of a linear least squares problem, , where is a real by matrix and is an element vector. If the matrix of observations is not of full rank, then the minimal least squares solution is returned.
Syntax
[
a,
b,
svd,
sigma,
irank,
work,
ifail] = f04jg(
a,
b,
tol,
lwork, 'm',
m, 'n',
n)
[
a,
b,
svd,
sigma,
irank,
work,
ifail] = nag_linsys_real_gen_solve(
a,
b,
tol,
lwork, 'm',
m, 'n',
n)
Description
The minimal least squares solution of the problem is the vector of minimum (Euclidean) length which minimizes the length of the residual vector .
The real
by
matrix
is factorized as
where
is an
by
orthogonal matrix and
is an
by
upper triangular matrix. If
is of full rank, then the least squares solution is given by
If
is not of full rank, then the singular value decomposition of
is obtained so that
is factorized as
where
and
are
by
orthogonal matrices and
is the
by
diagonal matrix
with
, these being the singular values of
. If the singular values
are negligible, but
is not negligible, relative to the data errors in
, then the rank of
is taken to be
and the minimal least squares solution is given by
where
.
The function also returns the value of the standard error
being the residual sum of squares and
the rank of
.
References
Lawson C L and Hanson R J (1974) Solving Least Squares Problems Prentice–Hall
Parameters
Compulsory Input Parameters
- 1:
– double array
-
lda, the first dimension of the array, must satisfy the constraint
.
The by matrix .
- 2:
– double array
-
The right-hand side vector .
- 3:
– double scalar
-
A relative tolerance to be used to determine the rank of
.
tol should be chosen as approximately the largest relative error in the elements of
. For example, if the elements of
are correct to about
significant figures then
tol should be set to about
. See
Further Comments for a description of how
tol is used to determine rank. If
tol is outside the range
, where
is the
machine precision, then the value
is used in place of
tol. For most problems this is unreasonably small.
- 4:
– int64int32nag_int scalar
-
The dimension of the array
work.
Constraint:
.
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the array
b and the first dimension of the array
a. (An error is raised if these dimensions are not equal.)
, the number of rows of
a.
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the second dimension of the array
a.
, the number of columns of .
Constraint:
.
Output Parameters
- 1:
– double array
-
If
svd is returned as
false,
a stores details of the
factorization of
.
If
svd is returned as
true, the first
rows of
a store the right-hand singular vectors, stored by rows; and the remaining rows of the array are used as workspace.
- 2:
– double array
-
The first
elements of
b contain the minimal least squares solution vector
. The remaining
elements are used for workspace.
- 3:
– logical scalar
-
Is returned as
false if the least squares solution has been obtained from the
factorization of
. In this case
is of full rank.
svd is returned as
true if the least squares solution has been obtained from the singular value decomposition of
.
- 4:
– double scalar
-
The standard error, i.e., the value when , and the value zero when . Here is the residual vector and is the rank of .
- 5:
– int64int32nag_int scalar
-
, the rank of the matrix
. It should be noted that it is possible for
irank to be returned as
and
svd to be returned as
true. This means that the matrix
only just failed the test for nonsingularity.
- 6:
– double array
-
If
svd is returned as
false, then the first
elements of
work contain information on the
factorization of
(see argument
a above), and
contains the condition number
of the upper triangular matrix
.
If
svd is returned as
true, then the first
elements of
work contain the singular values of
arranged in descending order and
contains the total number of iterations taken by the
algorithm. The rest of
work is used as workspace.
- 7:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
Constraint: .
Constraint: .
Constraint: .
On entry,
lwork is too small.
-
-
The algorithm has failed to converge to the singular values in iterations. This failure can only happen when the singular value decomposition is employed, but even then it is not likely to occur.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
The computed factors
,
,
,
and
satisfy the relations
where
being the
machine precision, and
and
being modest functions of
and
. Note that
.
For a fuller discussion, covering the accuracy of the solution
see
Lawson and Hanson (1974), especially pages 50 and 95.
Further Comments
If the least squares solution is obtained from the factorization then the time taken by the function is approximately proportional to . If the least squares solution is obtained from the singular value decomposition then the time taken is approximately proportional to . The approximate proportionality factor is the same in each case.
This function is column biased and so is suitable for use in paged environments.
Following the
factorization of
the condition number
is determined and if
is such that
then
is regarded as singular and the singular values of
are computed. If this test is not satisfied,
is regarded as nonsingular and the rank of
is set to
. When the singular values are computed the rank of
, say
, is returned as the largest integer such that
unless
in which case
is returned as zero. That is, singular values which satisfy
are regarded as negligible because relative perturbations of order
tol can make such singular values zero.
Example
This example obtains a least squares solution for
, where
and the value
tol is to be taken as
.
Open in the MATLAB editor:
f04jg_example
function f04jg_example
fprintf('f04jg example results\n\n');
a = [0.05, 0.05, 0.25, -0.25;
0.25, 0.25, 0.05, -0.05;
0.35, 0.35, 1.75, -1.75;
1.75, 1.75, 0.35, -0.35;
0.3, -0.3, 0.3, 0.3;
0.4, -0.4, 0.4, 0.4];
b = [1; 2; 3; 4; 5; 6];
tol = 0.0005;
lwork = int64(32);
[a, x, svd, sigma, irank, work, ifail] = ...
f04jg(a, b, tol, lwork);
disp('Solution');
disp(x(1:4)');
fprintf('Standard error = %6.3f, rank = %2d\n\n', sigma, irank);
if svd==1
fprintf('solution obtained from SVD of A\n');
else
fprintf('solution obtained from QU factorization of A\n');
end
f04jg example results
Solution
4.9667 -2.8333 4.5667 3.2333
Standard error = 0.909, rank = 3
solution obtained from SVD of A
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015