lsqlin: Linear Least-Squares Fitting

Description Usage Arguments Details Value Note Author(s) References See Also Examples

View source: R/lsqlin.R

Description

Solves linearly constrained linear least-squares problems.

Usage

1
  lsqlin(A, b, C, d, tol = 1e-13)

Arguments

A

nxm-matrix defining the least-squares problem.

b

vector or colum matrix with n rows; when it has more than one column it describes several least-squares problems.

C

pxm-matrix for the constraint system.

d

vector or px1-matrix, right hand side for the constraints.

tol

tolerance to be passed to pinv.

Details

lsqlin(A, b, C, d) minimizes ||A*x - b|| (i.e., in the least-squares sense) subject to C*x = d.

Value

Returns a least-squares solution as column vector, or a matrix of solutions in the columns if b is a matrix with several columns.

Note

The Matlab function lsqlin solves a more general problem, allowing additional linear inequalities and bound constraints. In pracma this task is solved applying function lsqlincon.

Author(s)

HwB email: <[email protected]>

References

Trefethen, L. N., and D. Bau III. (1997). Numerical Linear Algebra. SIAM, Society for Industrial and Applied Mathematics, Philadelphia.

See Also

nullspace, pinv, lsqlincon

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
A <- matrix(c(
    0.8147,    0.1576,    0.6557,
    0.9058,    0.9706,    0.0357,
    0.1270,    0.9572,    0.8491,
    0.9134,    0.4854,    0.9340,
    0.6324,    0.8003,    0.6787,
    0.0975,    0.1419,    0.7577,
    0.2785,    0.4218,    0.7431,
    0.5469,    0.9157,    0.3922,
    0.9575,    0.7922,    0.6555,
    0.9649,    0.9595,    0.1712), 10, 3, byrow = TRUE)
b <- matrix(c(
    0.7060,    0.4387,
    0.0318,    0.3816,
    0.2769,    0.7655,
    0.0462,    0.7952,
    0.0971,    0.1869,
    0.8235,    0.4898,
    0.6948,    0.4456,
    0.3171,    0.6463,
    0.9502,    0.7094,
    0.0344,    0.7547), 10, 2, byrow = TRUE)
C <- matrix(c(
    1.0000,    1.0000,    1.0000,
    1.0000,   -1.0000,    0.5000), 2, 3, byrow = TRUE)
d <- as.matrix(c(1, 0.5))

# With a full rank constraint system
(L <- lsqlin(A, b, C, d))
#  0.10326838 0.3740381
#  0.03442279 0.1246794
#  0.86230882 0.5012825
C %*% L
#  1.0  1.0
#  0.5  0.5

## Not run: 
# With a rank deficient constraint system
C <- str2num('[1 1 1;1 1 1]')
d <- str2num('[1;1]')
(L <- lsqlin(A, b[, 1], C, d))
#  0.2583340
# -0.1464215
#  0.8880875
C %*% L         # 1 1  as column vector

# Where both A and C are rank deficient
A2 <- repmat(A[, 1:2], 1, 2)
C <- ones(2, 4) # d as above
(L <- lsqlin(A2, b[, 2], C, d))
#  0.2244121
#  0.2755879
#  0.2244121
#  0.2755879
C %*% L         # 1 1  as column vector
## End(Not run)

Example output

           [,1]      [,2]
[1,] 0.10326838 0.3740381
[2,] 0.03442279 0.1246794
[3,] 0.86230882 0.5012825
     [,1] [,2]
[1,]  1.0  1.0
[2,]  0.5  0.5
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
     [,1]
[1,]    1
[2,]    1
           [,1]
[1,]  0.2583340
[2,] -0.1464215
[3,]  0.8880875
     [,1]
[1,]    1
[2,]    1
          [,1]
[1,] 0.2244121
[2,] 0.2755879
[3,] 0.2244121
[4,] 0.2755879
     [,1]
[1,]    1
[2,]    1

pracma documentation built on Aug. 26, 2018, 1:04 a.m.