Sie sind auf Seite 1von 9

MATLAB Code

clc
clear all

%1.Matrix-vector operations in Matlab


%part a)
fprintf('\nPart A\n')

%create 3 random 3x3 matrices


A=rand(3,3)
B=rand(3,3)
C=rand(3,3)
%compute the entity by entity product of A and B matrices
a=A.*B;
%Now numerically prove the following given equations in your textbook
%In all parts if the difference value is equal to zero, the given equation
%will be proved.

%check whether the equation 2.48 @page 74 is true or not


fprintf('eqn.2.48 (A+B)"=A"+B"\n')
b=(A+B)';
c=A'+B';
difference =det(b-c)

%check whether the equation 2.49 @page 74 is true or not


fprintf('eqn.2.49 (A*B)"=(B")*(A")\n')
d=(A*B)';
e=(B')*(A');
difference=det(d-e)

%check whether the equation 2.60 @page 77 is true or not


fprintf('eqn.2.60 sum(eig(A))=trace(A)\n')
f=sum(eig(A));

g=trace(A);
difference=f-g

%check whether the equation 2.62 @page 77 is true or not


%and also check whether the product of a(3x3) matrix A and inverse of matrix A is equal
%to the identity matrix (3x3) or not
fprintf('eqn.2.62 A*inv(A)=inv(A)*A\n')
i=A*inv(A);
k=inv(A)*A;
difference=det(i-k)

l=eye(3,3);
difference=det(k-l)

%check whether the equation 2.63 @page 78 is true or not


fprintf('eqn.2.63 (A*B)^-1=(B^-1)*(A^-1)\n')
m=(A*B)^-1;
n=(B^-1)*(A^-1);
difference=det(m-n)

%check whether the equation 2.64 @page 78 is true or not


fprintf('eqn.2.64 (A*B*C)^-1=(C^-1)*(B^-1)*(A^-1)\n')
p=(A*B*C)^-1;
r=(C^-1)*(B^-1)*(A^-1);
difference=det(p-r)

%part b)
fprintf('\nPart B\n')

A=[3 1 2 -4;5 2 1 3;6 2 4 -8]

%rank of A
fprintf('Rank of A\n')

rank=rank(A)

%Determinant of A
fprintf('Determinant of A\n')
fprintf('Determinant is defined for square matrices. Because A is not a square matrix\n')
fprintf('Evaluation determinant for A is not possible.\n')

%part c)
fprintf('\nPart C\n')

A=[1 1 1 0 0 0 0 0 0 0 0 0 0 0;
1.17 0 0 -1 0 0 0 0 0 0 0 0 0 0;
00001000000000;
0 0 1 0 1 -1 -1 -1 0 0 0 0 1 0;
0 0 0 0 0 1 1 1 1 -1 -1 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0 -1 0;
1 0 0 -1 0 0 0 0 0 1 0 0 0 1;
0 0 0 0 0 0 0 0 0 4.594 0 0 0 0.11;
0 0 0 0 0 0 0 0 1 0 0 0 0 0;
0 1 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0 0 -0.0147;
0 0 1 0 0 0 0 0 0 0 0 -0.07 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 0 -1 0 1];
d=[43.93 0 95.798 99.1 -8.4 24.2 189.14 146.55 10.56 2.9056 0 0 14.6188 -97.9];
c=d';
%try two method for solving the given linear equation
fprintf('\nMethod of x=inv(A)*c\n')
x1=inv(A)*c

fprintf('\nMethod of x=A|c \n')


x2=A\c

%Optional part show that whether both methods give the same result or not,
%that part controls row by row
fprintf('*Optional part*\n\n')
tolerance = 0.00000001;
for i=[1:1:14]
if abs(x1(i,1)-x2(i,1)) < tolerance
fprintf('the two method give same result for row %i\n',i);
else
fprintf('the two method does not give same resultfor row %i\n',i);
end
end

%check whether it give the same results with p.111, Table E2.2
x1(8)=0;
fprintf('\nThe x matrix @Page 111 Table E2.2')
x=[20.7;2.9;20.3;24.2;95.8;2.4;14.6;0.0;10.6;28.0;8.0;290.6;0.0;164.7]

fprintf('\n The x matrix that we found')


x1

Output
Part A
A=
0.7400 0.9706 0.3664
0.2348 0.8669 0.3692
0.7350 0.0862 0.6850

B=
0.5979 0.2060 0.2057
0.7894 0.0867 0.3883
0.3677 0.7719 0.5518

C=
0.2290 0.1518 0.2941
0.6419 0.7819 0.2374
0.4845 0.1006 0.5309

eqn.2.48 (A+B)"=A"+B"

difference =
0

eqn.2.49 (A*B)"=(B")*(A")

difference =
0

eqn.2.60 sum(eig(A))=trace(A)

difference =
-4.4409e-016

eqn.2.62 A*inv(A)=inv(A)*A

difference =
0

difference =
2.2024e-048

eqn.2.63 (A*B)^-1=(B^-1)*(A^-1)

difference =

2.8376e-044

eqn.2.64 (A*B*C)^-1=(C^-1)*(B^-1)*(A^-1)

difference =
4.1613e-041

Part B

A=
3

2 -4

4 -8

Rank of A
rank =
2

Determinant of A
Determinant is defined for square matrices. Because A is not a square matrix
Evaluation determinant for A is not possible.

Part C
Method of x=inv(A)*c

x1 =
20.6854
2.9056
20.3390
24.2020
95.7980
2.4211

14.6188
-0.0010
10.5600
27.9567
8.0422
290.5565
0.0020
164.6998

Method of x=A|c

x2 =
20.6854
2.9056
20.3390
24.2020
95.7980
2.4211
14.6188
-0.0010
10.5600
27.9567
8.0422
290.5565
0.0020
164.6998

*Optional part*
the two method give same result for row 1
the two method give same result for row 2
the two method give same result for row 3

the two method give same result for row 4


the two method give same result for row 5
the two method give same result for row 6
the two method give same result for row 7
the two method give same result for row 8
the two method give same result for row 9
the two method give same result for row 10
the two method give same result for row 11
the two method give same result for row 12
the two method give same result for row 13
the two method give same result for row 14

The x matrix @Page 111 Table E2.2


x=
20.7000
2.9000
20.3000
24.2000
95.8000
2.4000
14.6000
0
10.6000

28.0000
8.0000
290.6000
0
164.7000
The x matrix that we found
x1 =
20.6854
2.9056
20.3390
24.2020
95.7980
2.4211
14.6188
0
10.5600
27.9567
8.0422
290.5565
0.0020
164.6998

Das könnte Ihnen auch gefallen