0% found this document useful (0 votes)
23 views

Program#1

Matrix operations

Uploaded by

Bashir Alsadawi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Program#1

Matrix operations

Uploaded by

Bashir Alsadawi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

 

 
University of Benghazi
 
Faculty of Engineering
 

 
Civil Engineering Department
 

Numerical Analysis CE 501


Program No.1
Matrix Operations
 

Submitted to:
Prof. Salem Alsanusi
07, Nov, 2022

Prepared by:
Bashir A. El-Saadawi
CE 501 Numerical Analysis
 
 Introduction
In this paper, we will discuss the following operations on matrices and their properties by using
Matlab programming language:

 Matrices Addition
 Matrices Multiplication
 Matrices Transpose
 Matrices Inverse

1- Matrices Addition:
*****************************************************************************
clc
clear all
close all
x=[1 2 3; 3 4 1; -2 4 7];
y=[2 3 4; 1 2 3; 0 8 -1];
x
y
z=x+y;
disp('matrix addition z=x+y')
[m n]=size(x);
z=zeros(m,n);
for a=1:m
for b=1:n
z(a,b)=x(a,b)+y(a,b);
b=b+1;
end
a=a+1;
end
disp(z)
*****************************************************************************

 

 
CE 501 Numerical Analysis
 
Example:
1 2 3 2 3 4
𝑥 3 4 1 , 𝑦 1 2 3
2 4 7 0 8 1
3 5 7
𝑧 𝑥 𝑦 →𝑧 4 6 4
2 12 6
By using Matlab code:

 

 
CE 501 Numerical Analysis
 
2- Matrices Multiplication:
*****************************************************************************
clc
clear all
close all
x=[ 1 2; 3 4];
y=[ 5 6 7; 8 9 10];
x
y
[m1 n1]=size(x);
[m2 n2]=size(y);
z=zeros(m1,n2);
for a=1:m1;
for b=1:n2;
for c=1:n1;
z(a,b)= z(a,b)+ x(a,c)*y(c,b);
c=c+1;
end
b=b+1;
end
a=a+1;
end
disp('multiplication of x*y =');
disp(z);

*****************************************************************************

 

 
CE 501 Numerical Analysis
 
Example:
1 2 5 6 7
𝑥 , 𝑦
3 4 8 9 10
21 24 27
𝑧 𝑥 𝑦 →𝑧
47 54 61
By using Matlab code:

 

 
CE 501 Numerical Analysis
 
3- Matrices Transpose:
*****************************************************************************
clc
clear all
close all
x=[ 1 2; 2 3; 5 6];
x
[m n]=size(x);
tran=zeros(n,m);
for a=1:n
for b=1:m
tran(a,b)=x(b,a);
end
end
disp('Transpose of matrix [x] is')
disp(tran);

*****************************************************************************

Example:
1 2
𝑥 2 3
5 6
1 2 5
𝑥
2 3 6
By using Matlab code:

 

 
CE 501 Numerical Analysis
 
4- Matrices Inverse:
*****************************************************************************
A=input('Matrix A')
[m,n]=size(A);
if m~=n
fprintf('Not a square matrix \n')
else
if det(A)==0
fprintf('A is a singular matrix \n')
else
for r=1:m
for c=1:n
if r==c
E(r,c)=1;
else
E(r,c)=0;
end
end
end
T=[A E];
%%

for k=1:m
if T(k,k)==0
for i=k:m
if T(i,k)~=0
break
end
end
for j=1:2*m
temp_line(1,j)=T(i,j);
T(i,j)=T(k,j);
T(k,j)=temp_line(1,j);
end
end
end

%%
for k=1:m
if T(k,k)==0
for i=1:m
if T(i,k)~=0
break
end
end
for j=1:2*m
T(k,j)=T(i,j)+T(k,j);
end
end
end

 

 
CE 501 Numerical Analysis
 

for i=1:m-1
coe=T(i,i);
for j=1:2*m
T(i,j)=T(i,j)/coe;
end
for u=i+1:m
for o=u:m
temp_coe=T(o,i);
for l=1:2*m
T(o,l)=T(o,l)-temp_coe*T(i,l);
end
for p=o:m
if T(p,p)==0
for q=o+1:m
if T(q,p)~=0
break
end
for w=1:2*m
T(p,w)=T(p,w)+T(q,w);
end
end
end
end
end
end
end

coe_t=T(m,m);
for j=m:2*m
T(m,j)=T(m,j)/coe_t;
end

%%
for i=m-1:-1:1
for k=i+1:m
l=k;
temp_coe=T(i,k)/T(l,k);
for j=i:2*m;
T(i,j)=T(i,j)-temp_coe*T(l,j);
end
end
end

%%
for i=1:m
for j=m+1:2*m
inverse_A(i,j-m)=T(i,j);
end
end

inverse_A
end
end
*****************************************************************************
 

 
CE 501 Numerical Analysis
 
Example:
1 2 1
𝐴 2 0 1
1 1 0
1 1 2
𝐴 1 1 1
2 3 4
By using Matlab code:

 
 

 
CE 501 Numerical Analysis
 
Solve Home work by using Matlab code
3923 600 0 0 800 600
⎡ 600 2532 0 20832 600 450 ⎤
⎢ 0 0 21625 0 1562 0 ⎥ 
 𝑘 ⎢ 0 20832 0 2983.3 0 0 ⎥
⎢ 800 600 1562 0 2362.5 0 ⎥
⎣ 600 450 0 0 0 353335⎦
 

 
 

 
CE 501 Numerical Analysis

Appendix
Output from Matlab
Addition MATLAB Command Window 1 of 1

>> x=[1 2 3; 3 4 1; -2 4 7];


y=[2 3 4; 1 2 3; 0 8 -1];
x
y
z=x+y;
disp('matrix addition z=x+y')
[m n]=size(x);
z=zeros(m,n);
for a=1:m
for b=1:n
z(a,b)=x(a,b)+y(a,b);
b=b+1;
end
a=a+1;
end
disp(z)

x =

1 2 3
3 4 1
-2 4 7

y =

2 3 4
1 2 3
0 8 -1

matrix addition z=x+y


3 5 7
4 6 4
-2 12 6

>>
Multiplication MATLAB Command Window 1 of 1

>> x=[ 1 2; 3 4];


y=[ 5 6 7; 8 9 10];
x
y
[m1 n1]=size(x);
[m2 n2]=size(y);
z=zeros(m1,n2);
for a=1:m1;
for b=1:n2;
for c=1:n1;
z(a,b)= z(a,b)+ x(a,c)*y(c,b);
c=c+1;
end
b=b+1;
end
a=a+1;
end
disp('multiplication of x*y =');
disp(z);

x =

1 2
3 4

y =

5 6 7
8 9 10

multiplication of x*y =
21 24 27
47 54 61

>>
Transpose MATLAB Command Window 1 of 1

>> x=[ 1 2; 2 3; 5 6];


x
[m n]=size(x);
tran=zeros(n,m);
for a=1:n
for b=1:m
tran(a,b)=x(b,a);
end
end
disp('Transpose of matrix [x] is')
disp(tran);

x =

1 2
2 3
5 6

Transpose of matrix [x] is


1 2 5
2 3 6

>>
Inverse MATLAB Command Window 1 of 3

>> A=input('Matrix A')


%Build a Temp_Matrix:T=[A E], and E=A*inverse(A).
[m,n]=size(A);
if m~=n
fprintf('Not a square matrix \n')
else
if det(A)==0
fprintf('A is a singular matrix \n')
else
for r=1:m
for c=1:n
if r==c
E(r,c)=1;
else
E(r,c)=0;
end
end
end
T=[A E];
%%

for k=1:m
if T(k,k)==0
for i=k:m
if T(i,k)~=0
break
end
end
for j=1:2*m
temp_line(1,j)=T(i,j);
T(i,j)=T(k,j);
T(k,j)=temp_line(1,j);
end
end
end

%%
for k=1:m
if T(k,k)==0
for i=1:m
if T(i,k)~=0
break
end
end
for j=1:2*m
T(k,j)=T(i,j)+T(k,j)
end
end
end
Inverse MATLAB Command Window 2 of 3

%%
for i=1:m-1
coe=T(i,i);
for j=1:2*m
T(i,j)=T(i,j)/coe;
end
for u=i+1:m
for o=u:m
temp_coe=T(o,i);
for l=1:2*m
T(o,l)=T(o,l)-temp_coe*T(i,l);
end
for p=o:m
if T(p,p)==0
for q=o+1:m
if T(q,p)~=0
break
end
for w=1:2*m
T(p,w)=T(p,w)+T(q,w);
end
end
end
end
end
end
end

coe_t=T(m,m);
for j=m:2*m
T(m,j)=T(m,j)/coe_t;
end

%%
for i=m-1:-1:1
for k=i+1:m
l=k;
temp_coe=T(i,k)/T(l,k);
for j=i:2*m;
T(i,j)=T(i,j)-temp_coe*T(l,j);
end
end
end

%%
for i=1:m
for j=m+1:2*m
inverse_A(i,j-m)=T(i,j);
end
end
Inverse MATLAB Command Window 3 of 3

inverse_A
end
end
Matrix A [1 2 -1; -2 0 1; 1 -1 0]

A =

1 2 -1
-2 0 1
1 -1 0

inverse_A =

1.0000 1.0000 2.0000


1.0000 1.0000 1.0000
2.0000 3.0000 4.0000

>>
Inverse MATLAB Command Window 1 of 3

>> A=input('Matrix A')


%Build a Temp_Matrix:T=[A E], and E=A*inverse(A).
[m,n]=size(A);
if m~=n
fprintf('Not a square matrix \n')
else
if det(A)==0
fprintf('A is a singular matrix \n')
else
for r=1:m
for c=1:n
if r==c
E(r,c)=1;
else
E(r,c)=0;
end
end
end
T=[A E];
%%

for k=1:m
if T(k,k)==0
for i=k:m
if T(i,k)~=0
break
end
end
for j=1:2*m
temp_line(1,j)=T(i,j);
T(i,j)=T(k,j);
T(k,j)=temp_line(1,j);
end
end
end

%%
for k=1:m
if T(k,k)==0
for i=1:m
if T(i,k)~=0
break
end
end
for j=1:2*m
T(k,j)=T(i,j)+T(k,j)
end
end
end
Inverse MATLAB Command Window 2 of 3

%%
for i=1:m-1
coe=T(i,i);
for j=1:2*m
T(i,j)=T(i,j)/coe;
end
for u=i+1:m
for o=u:m
temp_coe=T(o,i);
for l=1:2*m
T(o,l)=T(o,l)-temp_coe*T(i,l);
end
for p=o:m
if T(p,p)==0
for q=o+1:m
if T(q,p)~=0
break
end
for w=1:2*m
T(p,w)=T(p,w)+T(q,w);
end
end
end
end
end
end
end

coe_t=T(m,m);
for j=m:2*m
T(m,j)=T(m,j)/coe_t;
end

%%
for i=m-1:-1:1
for k=i+1:m
l=k;
temp_coe=T(i,k)/T(l,k);
for j=i:2*m;
T(i,j)=T(i,j)-temp_coe*T(l,j);
end
end
end

%%
for i=1:m
for j=m+1:2*m
inverse_A(i,j-m)=T(i,j);
end
end
Inverse MATLAB Command Window 3 of 3

inverse_A
end
end
Matrix A [3923 600 0 0 -800 -600;
600 2532 0 -20832 -600 -450;
0 0 21625 0 -1562 0;
0 -20832 0 2983.3 0 0;
-800 -600 -1562 0 2362.5 0;
-600 -450 0 0 0 353335]

A =

1.0e+05 *

0.0392 0.0060 0 0 -0.0080 -0.0060


0.0060 0.0253 0 -0.2083 -0.0060 -0.0045
0 0 0.2163 0 -0.0156 0
0 -0.2083 0 0.0298 0 0
-0.0080 -0.0060 -0.0156 0 0.0236 0
-0.0060 -0.0045 0 0 0 3.5334

inverse_A =

1.0e-03 *

0.2748 0.0007 0.0071 0.0052 0.0979 0.0005


0.0007 -0.0070 -0.0001 -0.0488 -0.0016 -0.0000
0.0071 -0.0001 0.0487 -0.0008 0.0346 0.0000
0.0052 -0.0488 -0.0008 -0.0055 -0.0112 -0.0001
0.0979 -0.0016 0.0346 -0.0112 0.4789 0.0002
0.0005 -0.0000 0.0000 -0.0001 0.0002 0.0028

>>

You might also like