using LinearAlgebra A = randn(5,5) b = randn(5) x = A \ b # solve Ax = b b - A*x # this "residual" should be zero A = [1 2 2.01 3.99] b = [1,2] x = A \ b # "exact" answer Δb = [-0.01, 0.004] x′ = A \ (b + Δb) x′ - x # the error Δx Δx = A \ Δb norm(Δb) # the size of the error in the input norm(Δx) # the size of the error in the output opnorm(A) # ‖A‖ opnorm(inv(A)) # ‖A⁻¹‖ using PyPlot θ = range(0,2π,length=100) plot(θ/(2π), [norm(A \ [cos(θ), sin(θ)]) for θ in θ], "r-") plot(θ/(2π), ones(length(θ))*norm(inv(A)), "k--") xlabel(L"\theta / 2\pi") ylabel(L"\Vert A^{-1} y \Vert") text(0.16,opnorm(inv(A))*0.95, L"\Vert A^{-1} \Vert") title(L"maximizing $\Vert A^{-1} y \Vert$ over angle $\theta$") eigvals(inv(A)) abs.(eigvals(inv(A))) abs.(eigvals(inv(A)))[1] - opnorm(inv(A)) B = randn(2,2) opnorm(B) abs.(eigvals(B)) svdvals(B) svdvals(B)[1] - opnorm(B) svdvals(inv(A))[1] - opnorm(inv(A)) opnorm(B) sqrt.(eigvals(B'B)) svdvals(B) opnorm(10*B) / opnorm(B) B₂ = randn(size(B)) opnorm(B + B₂) / (opnorm(B) + opnorm(B₂)) opnorm(B * B₂) / (opnorm(B) * opnorm(B₂)) opnorm(inv(A)) 1 ./ svdvals(A) opnorm(inv(A)) - 1/svdvals(A)[2] cond(A) opnorm(A) * opnorm(inv(A)) σ = svdvals(A) maximum(σ) / minimum(σ) cond(A) A norm(Δx)/norm(x) / (norm(Δb)/norm(b)) X = [1 1 0 1e-14] M = X * Diagonal([1,1+1e-14]) / X exp(M) X * exp(Diagonal([1,1+1e-14])) / X