source('utils.R') ##n = 6 ## tester size n = 21 ## official problem size A = build_tridiagonal_matrix(n, 0, 1, 1) d = 10 A_shifted = A + diag(rep(d, n)) if(n<=10){ print('A_shifted=') print(A_shifted) } pm = power_method(A_shifted, tol=1e-3) exact = exact_tridiagonal_eigensystem(n, d, 1) ## Compare our results with the exact results: ## print(sprintf('my eigenvalue= %f; exact eigenvalue= %f; error= %f', pm$max_eigenvalue, exact$lambda[n], exact$lambda[n]-pm$max_eigenvalue)) print('my eigenvector=') print(pm$max_eigenvector) print('exact eigenvector=') ee = exact$U[, n]; dim(ee) = c(n, 1) ee = ee/norm(ee, '2') ## normalize to the same norm as the outputs from the power_method print(ee) print(sprintf('norm of the difference= %f', norm(pm$max_eigenvector-ee, '2')))