Try the matlab file
cofactor.m from the
MATLAB teaching codes
>> A=[
2 4 6
1 2 5
-1 3 4.1]
A =
2.0000 4.0000 6.0000
1.0000 2.0000 5.0000
-1.0000 3.0000 4.1000
>> cofactor(A)
ans =
-6.8000 -9.1000 5.0000
1.6000 14.2000 -10.0000
8.0000 -4.0000 0
>>
A=2*randn(5);
A=round(A)
A =
-2 -1 0 2 1
1 1 -1 1 -1
1 -2 2 1 -1
3 0 -4 0 -1
1 0 1 1 -3
Ainv=cofactor(A)'/det(A)
Ainv =
-0.2727 0.5354 0.4040 -0.0303 -0.3939
-0.1818 0.6162 -0.1010 -0.2424 -0.1515
-0.1818 0.2828 0.2323 -0.2424 -0.1515
0.1818 0.6061 0.2121 -0.0909 -0.1818
-0.0909 0.4747 0.2828 -0.1212 -0.5758
A*Ainv
ans =
1.0000 0.0000 0 0 -0.0000
0.0000 1.0000 0.0000 0.0000 0.0000
0.0000 0.0000 1.0000 0.0000 0.0000
0.0000 -0.0000 0.0000 1.0000 0.0000
0 0 0.0000 0 1.0000
Evaluating determinants (equivalently finding cofactors)
can be very time consuming, i.e. it is very inefficient. Try:
for i=10:10:100,
A=randn(i);
tic;
cofactor(A);
times(i)=toc;
disp([i times(i)])
end
The output on a SUNW, Sun-Fire-480R workstation is:
10.0000 0.0194
20.0000 0.1018
30.0000 0.3546
40.0000 0.9531
50.0000 2.1730
60.0000 4.4029
70.0000 7.9435
80.0000 12.2863
90.0000 17.9972
100.0000 26.7261
You can now try plot(times(10*(1:10))) !!! To see the exponential growth.
(See file
loopcofactors.m.)