Solution:
syscheck.m
function [] = syscheck(A,B)
%SYSCHECK denotes whether or not matricies A and B will fit into the
% equation A x X = B and give solutions for all x-values.
[m1 n1] = size(A);
[m2 n2] = size(B);
if n2 ~= 1
error('Indeterminant: Matrix B is not a column matrix');
elseif m1 ~= m2
error('Indeterminant: Matricies A and B must have the same number of rows');
end
[X, pivot] = rref([A,B]);
if length(pivot) ~= m1
error('Indeterminant: Incorrect number of linearly independant rows');
else
disp('System is OK');
end
return;
Solution:
systemsolver.m
function [X] = systemsolver(A,B)
K=[A,B];
X=rref(K);
return;
>> A = [1 2 3; 4 5 6; 7 8 10];
>> B = ones(3,1);
>> systemsolver(A,B)
ans =
1 0 0 -1
0 1 0 1
0 0 1 0
>> A = [1 2 3; 4 5 6];
>> B = ones (2,1);
>> systemsolver(A,B)
ans =
1 0 -1 -1
0 1 2 1
>> A = [2 -1; 1 10; 1 2];
>> B = ones(3,1);
>> systemsolver(A,B)
ans =
1 0 0
0 1 0
0 0 1
The solution is not necessarily the same as using the '\' operator because A\B requires
that rank([A,B])=rank(A). In any other case, it may give an estimation or not be able to
complete the operation.
Solution:
matrixinverse.mfunction [B]=matrixinverse(A) [a b]=size(A); K=[A,eye(a,b)]; J=rref(K); B=J(:,(b+1):2*b); return;
Solution:
cofact.m
function [X]=cofact(A,m,n)
[a,b]=size(A);
if m>a | n>b | (a~=b)
error('Index exceeds matrix dimensions or matrix is not square. Word.')
end
c=1;
for k=1:a
if k ~= m
B(c,:)=[A(k,1:(n-1)), A(k,(n+1):b)];
c=c+1;
end
end
X=((-1)^(m+n))*det(B);
return;
Solution:
rDet.m
function d=rDet(A)
[a,b]=size(A);
if a~=b
error('Matrix must be square.');
end
if a==1
d=A; return;
end
m=1; x=0;
for n=1:b
c=1;
for k=1:a
if k ~= m
B(c,:)=[A(k,1:(n-1)), A(k,(n+1):b)];
c=c+1;
end
end
if size(B)==[2 2]
x=A(m,n)*((-1)^(m+n)*(B(1,1)*B(2, 2)-B(2, 1)*B(1, 2)))+x;
else
x=A(m,n)*((-1)^(m+n)*rDet(B))+x;
end
end
d=x;
return;
Solution:
adjoint.m
function Z=adjoint(A)
[a,b]=size(A);
for m=1:a
for n=1:b
c=1;
for k=1:a
if k ~= m
B(c,:)=[A(k,1:(n-1)), A(k,(n+1):b)];
c=c+1;
end
end
X(m,n)=((-1)^(m+n))*det(B);
end
end
Z=transpose(X);
return;
Solution:
Please click here for the proof.
A =
4 2 5
5 3 7
1 9 2
>> Q=adjoint(A)
Q =
-57 41 -1
-3 3 -3
42 -34 2
>> R=det(A)
R =
-24
>> X=Q/R
X =
2.3750 -1.7083 0.0417
0.1250 -0.1250 0.1250
-1.7500 1.4167 -0.0833
>> Y=inv(A)
Y =
2.3750 -1.7083 0.0417
0.1250 -0.1250 0.1250
-1.7500 1.4167 -0.0833
Since X = Y, it follows that inv(A) = adj(A)/det(A)
Write a MATLAB code to verify the above theorem.
Solution:
Please click here for the proof.
>> A=magic(3)
A =
8 1 6
3 5 7
4 9 2
>> C=poly(A)
C =
1.0000 -15.0000 -24.0000 360.0000
>> X=polyvalm(C,A)
X =
1.0e-012 *
0.4547 -0.5116 -0.0853
-0.3340 0.1705 0.0568
-0.2700 0.1705 0
X is approximately a matrix of zeros. The residual values are due to roundoff errors.
E=eig(eye(3))
E =
1
1
1
>> A=eye(3);
>> [V D]=eig(A)
V =
1 0 0
0 1 0
0 0 1
D =
1 0 0
0 1 0
0 0 1
>> A=eye(3);
>> [E, S]=eig(A);
>> S*A*inv(S)
ans =
1 0 0
0 1 0
0 0 1
>> E
E =
1 0 0
0 1 0
0 0 1