Cramer rule

% This is the code to compare Cramer rool, calculation of inverse and

% Matlab matrix division. tic and toc measure time. The size of the

% matrices is set to 4, but code can be easily generalized.

% To choose random matrices uncoment rand statements below

%

% Yuri Lvov, Mon Sep 9 13:16:53 EDT 2002

tic;

A=[1 2 3 9,

5 6 7 8,

9 10 11 12,

13 14 11 16];

b=[-1,

-2,

-9,

-10];

%A=rand(4,4);

%b=rand(4,1);

DET=det(A);

if (DET==0) disp('singular'); return; end;

x=zeros(4,1);

for I = 1:4, B=A;

B(:,I)=b;

disp('matrix');disp(B);

x(I)=det(B)/DET;

%disp('x=');disp(x);

end

disp('Cramer rule done');

disp('just checking');disp(A*x-b);

disp('Cramer Done');

toc

tic

X1=A;

toc

disp('Matlab matrix division done');

tic

X2=inv(A)*b;

toc

disp('Compute x via inverse matrix done');

disp('difference between Cramer and Matlab inverse matrix division');

disp(x-X1)

disp('difference between Cramer and using inverse matrix ');

disp(x-X2)

disp('Have a great day');