Purpose - To work out Exercise 11.3

> library(RColorBrewer)
> cols <- brewer.pal(4, "Set1")
> A <- matrix(data = 0, nrow = 50, ncol = 12)
> x.t <- seq(0, 1, length.out = 50)
> for (i in 1:12) {
+     A[, i] <- x.t^(i - 1)
+ }
> b <- cos(4 * x.t)

NORMAL EQUATIONS
Error in solve.default(t(A)times A) : system is computationally singular: reciprocal condition number

QR Factorization

> Q <- qr.Q(qr(A))
> b_prime <- t(Q) %*% b
> R <- qr.R(qr(A))
> x <- solve(R, b_prime)
> x1 <- x
> plot(10^8 * (A %*% x1 - cos(4 * x.t)))

Exer_11_3-003.jpg

SVD Factorization

> U <- svd(A)$u
> d <- diag(svd(A)$d)
> V <- svd(A)$v
> b_prime <- t(U) %*% b
> x <- V %*% solve(d, b_prime)
> x2 <- x
> plot(10^8 * (A %*% x2 - cos(4 * x.t)))

Exer_11_3-004.jpg

Compare the 12 coefficient list

> cbind(x1, x2)
                        [,1]                   [,2]
 [1,]  1.000000000996605e+00  1.000000000996604e+00
 [2,] -4.227428691202565e-07 -4.227430102352229e-07
 [3,] -7.999981235689559e+00 -7.999981235685077e+00
 [4,] -3.187631876893836e-04 -3.187632430210450e-04
 [5,]  1.066943079560576e+01  1.066943079597327e+01
 [6,] -1.382028678074381e-02 -1.382028825931538e-02
 [7,] -5.647075630590243e+00 -5.647075626774281e+00
 [8,] -7.531601862032665e-02 -7.531602508485076e-02
 [9,]  1.693606956958508e+00  1.693606964113136e+00
[10,]  6.032113428498480e-03  6.032108446974022e-03
[11,] -3.742417053440816e-01 -3.742417033639870e-01
[12,]  8.804057640437810e-02  8.804057606180213e-02

One can use

  1. Classic Gram Schmidt
  2. Improved Gram Schmidt
  3. SVD
  4. QR Triangular Decomposition
  5. QR by Householder Orthogonal decomposition

One must keep in mind operation count, numerical stability accuracy issues to decide on the algo to be used.