R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # c3nwdemo.r New demo for Chapter 3 > # September 2007, revised 2009 > # > # simpler example for condition number for solving systems > # of linear equations > # > # here's a matrix > A <- matrix(c(4,-2,2,4,-2,2,-2,-2,2,-2,11,5,4,-2,5,6),4,4) > A # write it out [,1] [,2] [,3] [,4] [1,] 4 -2 2 4 [2,] -2 2 -2 -2 [3,] 2 -2 11 5 [4,] 4 -2 5 6 > # > # Cholesky factorization > # > Lt <- chol(A) > Lt # write it out [,1] [,2] [,3] [,4] [1,] 2 -1 1 2 [2,] 0 1 -1 0 [3,] 0 0 3 1 [4,] 0 0 0 1 > # > # rhs > # > b <- c(2,0,0,2) > # > # solve these equations > # > y <- forwardsolve( t(Lt), b) > x <- backsolve( Lt, y ) > x # write it out [1] 1 1 0 0 > # > # check the solution > # > A %*% x [,1] [1,] 2 [2,] 0 [3,] 0 [4,] 2 > # > # get infinity norm (from homework exercise) > # > normA <- max( apply( abs(A), 1, sum) ) > # > # get infinity norm of inverse for condition number > # > kappa <- normA*max( apply( abs(chol2inv(Lt)),1,sum) ) > kappa [1] 73.33333 > # > # small change in matrix A > # > E <- matrix( 1.e-4, 4, 4) # recycling > # > # Cholesky factor of changed matrix > # > Lte <- chol( A+E ) > Lte [,1] [,2] [,3] [,4] [1,] 2.000025 -0.9999375 1.0000375 2.0000250 [2,] 0.000000 1.0001125 -0.9998125 0.0000000 [3,] 0.000000 0.0000000 3.0000666 0.9999778 [4,] 0.000000 0.0000000 0.0000000 1.0000222 > # > # solve equations > # > y <- forwardsolve(t(Lte),b) > xe <- backsolve(Lte,y) > xe [1] 9.996668e-01 9.996112e-01 -8.885878e-05 1.332882e-04 > # > # norm of difference > # > max( abs(x-xe) ) [1] 0.0003887571 > # > # > # small change in rhs > # > be <- b + 1.e-4 # recycling > # > # solve equations > # > y <- forwardsolve(t(Lt),be) > xe <- backsolve(Lt,y) > xe [1] 1.000167e+00 1.000194e+00 4.444444e-05 -6.666667e-05 > # > # norm of difference > # > max( abs(x-xe) ) [1] 0.0001944444 > # > # done > rm(list=ls()) > >