-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverseIteration.m
38 lines (33 loc) · 1.09 KB
/
inverseIteration.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function [domVal domVec counter] = inverseIteration(A, numTimes)
epsilon = 0.0001;
counter = 0;
rcondition = rcond(A);
% call power iteration until convergence is around 1
% then approximation is good enough to start inverse iteration
[appxVal appxVec error] = power(A,1,numTimes);
while (counter < numTimes) && (error > epsilon) && (rcondition > 1e-15)
temp = appxVec;
C = (A-appxVal*eye(size(A)))\appxVec;
appxVec = C/norm(C,2);
appxVal = appxVec'*A*appxVec;
error = norm(temp-appxVec,2);
counter = counter+1;
rcondition = rcond(A-appxVal*eye(size(A)));
end
domVec = appxVec;
domVal = appxVal;
end
function [domVal domVec error counter] = power(A, tolerance, numTimes)
appx = ones(length(A),1);
counter = 0;
epsilon = tolerance;
error = 100;
while (counter < numTimes) && (error > epsilon)
temp = appx;
appx = A*appx / norm(A*appx, 2);
error = norm(temp-appx,2);
counter = counter + 1;
end
domVec = appx;
domVal = appx'*A*appx;
end