Write a MATLAB function to estimate the condition number of
Write a MATLAB function to estimate the condition number of the square matrix A in the two-norm using the symmetric power iteration. The function should be of the form [cond_est,u1,v1,u2,v2]=cond2(L,U,A) where L and U are from [L, U]=lu(A). Your function cannot call cond or inv or use y=A\\v or z= A\'\\u
Solution
You are to write a MATLAB function to implement to estimate the condition
 number of the square matrix A in the two-norm using the symmetric power
 iteration.
 Your function should have the form [cond est,u1,v1,u2,v2]=cond2(L,U,A). where L and U are the out- put from the from the command [L,U]=lu(A) which does Gaussian elimination with partial pivoting.
The value cond est is your estimate for the
 value
 2 (A) = kA1 k2 kAk2 .
 Thus, you will have to estimate both kA1 k2 and kAk2 using the power
 method. The vectors u1,v1, u2 and v2 satisfy
 ku1k2 = kv1k2 = ku2k2 = kv2k2 ,
 kAv1k2  kAk2 ,
 kAT u1k2  kAk2 ,
 kA1 v2k2  kA1 k2 ,
 kAT u2k2  kAT k2 .
 These four vectors should emerge out of the power method for approximating
 kAk2 and kA1 k2 . To find kAk2 you are implicitly performing the power
 method on AT A, to find kA1 k2 you are doing the same for (AT A)1 =
 A1 AT .
 To estimate kAk2 , you should perform k power iterations to produce  (k) ,
 u1 and v1 such that
 A  v1 =  (k) u1
 and either
 | (k)   (k1) |  (1e  3)   (k) 1 or
 kA0  u1   (k) v1k2  (1e  3)   (k)
 or
 k = 10.
 To estimate kA1 k2 , you should perform ` power iterations to produce  (`) ,
 u2 and v2 such that
 A1  v2 =  (`) u2
 and either
 | (`)   (`1) |  (1e  3)   (`)
 or
 kAT  u2   (`) v1k2  (1e  3)   (`)
 or
 ` = 10.
 Your estimate of the condition number is thus, cond est =  (`)   (k) . Our
 course, you can get away with storing only the two most recent values of  (k)
 and  (`) .
 In order to implement this method, you will need four matrix operations
 with A.
 1. A  v for a vector v. this is just implmented as is in MATLAB.
 2. AT  u for a vector u.this is just A0  u in MATLAB.
 3. A1  v for a vector v
 4. AT  u for a vector u.
 To do the last two operations above, use the results from lu, namely a
 factorization of A into A = LU where L is a permutation of a lower triangular
 matrix and U is an upper triangular matrix. Remember that y = A1 v may
 be computed from
 w=L\\v
 y=U\\w
 and z = AT u may be computed from 2 w=U’\\u
 z=L’\\w
function [ v d] = power_method( A )
% for finding the largest eigen value by power method
disp ( \' Enter the matrix whose eigen value is to be found\')
% Calling matrix A
A = input ( \' Enter matrix A : \ \')
% check for matrix A
% it should be a square matrix
[na , ma ] = size (A);
if na ~= ma
disp(\'ERROR:Matrix A should be a square matrix\')
return
end
% initial guess for X..?
% default guess is [ 1 1 .... 1]\'
disp(\'Suppose X is an eigen vector corresponding to largest eigen value of matrix A\')
r = input ( \'Any guess for initial value of X? (y/n): \',\'s\');
switch r
case \'y\'
% asking for initial guess
X0 = input(\'Please enter initial guess for X :\ \')
% check for initial guess
[nx, mx] = size(X0);
if nx ~= na || mx ~= 1
disp( \'ERROR: please check your input\')
return
end
otherwise
X0 = ones(na,1);
end
%allowed error in final answer
t = input ( \'Enter the error allowed in final answer: \');
tol = t*ones(na,1);
% initialing k and X
k= 1;
X( : , 1 ) = X0;
%initial error assumption
err= 1000000000*rand(na,1);
% loop starts
while sum(abs(err) >= tol) ~= 0
X( : ,k+ 1 ) = A*X( : ,k); %POWER METHOD formula
% normalizing the obtained vector
[ v i ] = max(abs(A*X( : ,k+ 1 )));
E = X( : ,k+ 1 );
e = E( i,1);
X(:,k+1) = X(:,k+1)/e;
err = X( :,k+1) - X( :, k);% finding error
k = k + 1;
end
%display of final result
fprintf (\' The largest eigen value obtained after %d itarations is %7.7f \ \', k, e)
disp(\'and the corresponding eigen vector is \')
X( : ,k)
use the above code to produce power iteration.



