Fix MATLAB code for condition number estimate The code canno
Fix MATLAB code for condition number estimate:
The code cannot use built-in MATLAB functions for inverse or condition number and must use the LU decomposition of a square matrix A and the power method to calculate the estimate: K2(A)=||A||2||A-1||2
function [cond_est, u1, v1, u2, v2] = cond2(A)
[L, U] = lu(A);
[n,] = size(A);
u1 = ones(n);
v1 = ones(n);
k = 10;
for i=0 : k
y = A*v1;
normy = norm(y,2);
u2 = y/normy;
t = transpose(A)*u2;
normt = norm(t);
v2 = t/normt;
end
for i = 0 : k
y = U\'\\v2;
y = L\'\\y;
normy2 = norm(y,2);
u2 = y/normy2;
z = L\\u2;
z = U\\z;
normz = norm(z,2);
v2 = z/normz;
end
cond_est = normt * normz;
end
Solution
n = 500;
Q = orth(randn(n,n));
d = logspace(0,-10,n);
A = Q*diag(d)*Q\';
x = randn(n,1);
b = A*x;
tic
y = inv(A)*b;
t = toc
err_inv = norm(y-x)
err_inv =
5.0942e-06
tic
z = A\\b;
t1 = toc
