## 4.5-10 ## ## Get the matrix A and A^{-1} from Exercise 4.2-8 (the exact solution is [1, 1, 1, 1]): ## source('./section_4_4_ex_2.R') x_exact = matrix(c(1, 1, 1, 1), nrow=4, ncol=1) ## Get the right-hand-side from Exercise 4.2-8: b = matrix(c(0.96, 0.71, 1.26, 1.53), nrow=4, ncol=1) print(b) ## Compute the conditions numbers for A: WWX: TODO ## one_norm = norm(A, 'O') * norm(AInv, 'O') inf_norm = norm(A, 'I') * norm(AInv, 'I') print(sprintf('Condition numbers: One-norm= %f; Inf-norm= %f', one_norm, inf_norm)) ## What was the solutions found for x in Exercises 4.2-8 and 4.3-4: ## source('./section_4_2_ex_8.R') x_ex_4_2_8 = x source('./section_4_3_ex_4.R') x_ex_4_3_4 = x Xs = cbind(x_ex_4_2_8, x_ex_4_3_4, x_exact) colnames(Xs) = c('ex_4_2_8_sol', 'ex_4_3_4_sol', 'true_solution') print(Xs) ## Compute the relative errors: ## print(sprintf('Relative errors (one-norm): ex_4_2_8= %f; ex_4_3_4= %f', norm(x_ex_4_2_8 - x_exact, 'O') / norm(x_exact, 'O'), norm(x_ex_4_3_4 - x_exact, 'O') / norm(x_exact, 'O'))) ## Compute the residuals: ## r_ex_4_2_8 = b - A %*% x_ex_4_2_8 r_ex_4_3_4 = b - A %*% x_ex_4_3_4 Rs = cbind(r_ex_4_2_8, r_ex_4_3_4) colnames(Rs) = c('ex_4_2_8_residual', 'ex_4_3_4_residual') print(Rs) ## Compute A^{-1} r: ## results = GE_w_scaled_partial_pivoting(A, r_ex_4_2_8) e_hat_ex_4_2_8 = results$X results = GE_w_scaled_partial_pivoting(A, r_ex_4_3_4) e_hat_ex_4_3_4 = results$X EHats = cbind(e_hat_ex_4_2_8, e_hat_ex_4_3_4) colnames(EHats) = c('ex_4_2_8', 'ex_4_3_4') print(EHats) ## Add in our correction: ## print(x_ex_4_2_8 + e_hat_ex_4_2_8) print(x_ex_4_3_4 + e_hat_ex_4_3_4)