HADHRAMOUT UNIVERSITY DEPARTMENT OF
COLLEG OF PETROLEUM PETROLUEM
ENGINEERING ENGINEERING
Computer Applications
(Matlab Program)
Dr. Abdelrigeeb A. Al-Gathe
2016-2017
Lecture 1
Matlab with arithmetic operations:-
Symbol Example Answer
Additional , a+b + 2+10 23
Subtract , a-b - 25-10 26
Multiplication, * 6*2 23
a*b
Division, a/b / or \ 10/2 or 2\10 6
Exponentiation , a^b 102 211
ab
√ sqrt(a) sqrt(25) 6
Sin(a) Sin(a) Sin(pi/4) 178182
Cos(a) Cos(a) Cos(pi/4) 178182
tan(a) tan(a) tan(pi/4) 2
𝜋 pi pi 472527
| | abs abs(-5) 6
√ a^(1/n) 27^(1/3) 4
Note : The angles must be in the radian system.
Example:
Find Sin (45) ?
Solution:
First convert degree system to radian system, 45o=
After that in Matlab , sin(0.7854)=0.7071
Another method :- in Matlab , sin(45*pi/180)=0.7071
Exercises:-
Using Matlab as calculator:
Problem Matlab code Result
1- 6*3-8 6*3-8 10
2- 20/5+4*2-2 20/5+4*2-2 10
3- 52-32 5^2-3^2 -2
4- √ 12
5- 2𝜋 2 6.2832
6- | | 6
7- √ 4
8- Sin(60o) 0.866
Sin(60o)
9- cos(360o) 1
Cos (360o)
10- Sin2(30o)+ cos2(30o) 1
0.5774
11-
0.7071
Lecture 2
Matrix:-
5 6 3
4 2 1
3 3 3
In th Matlab [5 6 3; 4 2 1;3 3 3]
Or [5,6,3;4,2,1;3,3,3]
*between each number in the row space or comma ,
* Before new row (;)
Sum & subtraction matrixes:
For sum or subtraction matrixes must be have the same size (size is number of rows and
columns)
2 6
Row =3 column =2 Size = (3, 2) → 10 8
1 7
Example:
2 7 0 5
A= 10 3 , B= 2 1 , find A+B , A-B
1 7 4 7
Solution:
In mathematical method
2 7 0 5 2 0 7 5 2 12
A+B= 10 3 2 1 10 2 3 1 12 4 In Matlab
1 7 4 7 1 4 7 7 5 14
>>A=[2 7;10 3;1 7] , B=[0 5;2 1;4 7]
2 7 0 5 2 0 7 5 2 2 >>A+B
A-B = 10 3 2 1 10 2 3 1 8 2
1 7 4 7 1 4 7 7 3 0 >>A-B
Multiplication Matrixes:
For multiplication of matrixes must be number of columns in first matrix is equal number of
row in second matrix.
Example:
2 3
A= , B= , find A*B , B*A
1 0
Solution:
2 3
A*B = * = =
1 0
B*A = error (because number of columns in first matrix (B) is not equal number of rows
in second matrix)
In Matlab
>>A=[5 1] , B=[2 3;1 0]
>> A*B
>>B*A
(Matrixes)' :
Example
6 1
6 3 0 3 4
A= A' = Symbol ( ' ) convert all rows to columns .
1 4 5 0 5
>>A= [6 3 0; 1 4 5] >>A' = [6 1; 3 4;0 5]
Solve the equation :- ( First class equation)
Example
X1+2X2+3X3=366………………….. (1)
4X1+5X2+6X3=804………………… (2)
7X1+8X2=351………………………. (3)
Solution:
First step:
1 2 3 X1 366
4 5 6 X2
A= , C= , B= 804
7 8 0 X3 351
Second step : C=B*inv (A) or C=A\B
Then the result: X1= 561 , X2=-222 , X3=99 (by using Matlab)
Mean (a), Mean2(a) , Max(a), Min(a) , Sum(a) :-
Mean→ average (but in the matrix , mean →average of each column)
mean→ average , Max → maximum value , Min → minimum value
Sum → summation (but in the matrix Sum → summation of each column)
Example
2 4 6
A = 3 6 7
1 2 8
Min (min (A)) = 1
Mean (A) = 2 4 7 Mean2 (A) = 4.3333
Max (A) = 3 6 8 Min (A) = 1 2 6
Sum (A) = 6 12 21 Max (Max (A))= 8
Mean (Mean (A) )=4.3333 Sum (Sum (A))=39
Exercises:
Problem Results
1 2 1 0 3 0 1 5 1
5 4 1 5 6 4
+ 1 2 3
0 3 1 2 1 0 5 1 1
1 3 4 0
2 6 8 0
* [ ]
3 9 12 0
Solve:
a= -14.6501
-4a+5b+11c=40
b=19.7230
2a+4b+7c=-25
c= -10.6560
3a+10b+3c=100
problem Results
Mean(A)=3 -0.5 3.5
Mean2(A)= 2
2 1 5 Max (A)= 4 0 5
4 0 2 Min(A)= 2 -1 2
A= Sum(A)= 6 -1 7
Max(Max(A))= 5
Mean(A) , Mean2(A) , Max (A) ,Min(A) Min(Min(A))= -1
Sum(A) Sum(Sum(A))= 12
Max(Max(A)) , Min(Min(A)) , Max(Mean(A))= 3.5
Sum(Sum(A)) , Max(Mean(A)) Min(Max(A))= 0
Min(Max(A)) , Sum(Max(A)) Sum(Max(A))= 9
B*A= 16
1
2 4 3 2
B=[4 3 2] , A=
3 A* B= 8 6 4
12 9 6
Find: B *A , A* B , A' * B' A' * B' = 16
Lecture 3
Linspace (a,b,n)
Example
Find linspace (1, 0.5, 3)
Solution:
1 0.75 0.5
Solve Polynomial Equations
Example 1
X4-12X3+25X+116=0
Before the solution the equation must be equal zero.
In matlab , a polynomial is represented by row vector of its coefficients in descending order.
Solution:
Making matrix:- A= [1 -12 0 25 116] → using Matlab
1 → coefficient X4
R = roots (A)
3
-12 → coefficient X
=11.7473
0 → coefficient X2
2.7028
25 → coefficient X
-1.2251+1.4672i
-1.2251-1.4672i
Example 2
If the root are given , What's the equation ??
The roots are 0 , 1 find the equation .
Solution:
Making matrix : by using Matlab
A= [0 1]
B=poly (A)
1 -1 0
The results are three numbers , first number (1) → coefficient X2
Second number (-1) → coefficient X three number=0
The equation is X2 –X+0=0 → X2 –X=0
Example 3
Roots are: 11.7473 , 2.7028 , -1.2251+1.4672i , -1.2251-1.8672i
Find the equation ?
Solution:
A=[11.7473 2.7028 -1.2251+1.4672i -1.2251-1.8672i]
B=poly (A)
B= 1 -12 -0.0015 25 116
The equation is :
X4-12X3-0.0015X2+25X+116=0
Curve fitting and interpolation
Polyfit (x, y, n)
Polyval (x, p)
Calculate the polynominal x at point p
Example
X=[0 0.1 0.2 0.3 0.4 0.5] Y=[-0.447 1.978 3.28 6.16 7.08 7.34]
Find :
P=polyfit (x, y, 2) & the equation of p
Xi =linspace (0, 0.5, 4)
Z=polyval (P, Xi)
Solution:
P= -22.0589 27.5497 -0.5835
The equation is: -22.0589X2+27.5497X-0.5835
Xi = 0 1.667 0.3333 0.5 Z = -0.5835 3.3620 0.5000 7.5766
Interpolation (interpl)
Estimate value not exsit
Example
T=[50 60 70 80 ] H=(2592.2 2609.7 2626.9 2648.8]
Find
1- H at T=65 2- H at T =75
Solution:
1- interp1(T,H,65) =2618.3
2- interp1(T,H,75)= 2637.9
Lecture 4
Round , fix , factor , eye , ones , zeros , diag
Example 1
A=[2.9 1.49 1.5 -2.4 -2.8] by using Matlab
Solution:
1. round (A) = 3 1 2 -2 -3
2. fix(A) = 2 1 1 -2 -2
Example 2
Find factor( 9) ,factor(10) , factor (12) by using Matlab
Solution:
factor (9)= 3 3 factor (10) = 2 5 factor (12) = 2 2 3
Example 3
Find A=eye(3) , B=eye(4) by using Matlab
Solution:
1 0 0 0
1 0 0 0 0
A = 0 1 0
1 0
B=
0 0 1 0
0 0 1
0 0 0 1
* If we need change (1) to (2)
B=2*A by using Matlab
2 0 0 0
0 2 0 0
B=
0 0 2 0
0 0 0 2
If we need change any number chose it's number of row and column*
B (3, 3)= -4 by using Matlab
2 0 0 0
0 2 0 0
B=
0 0 4 0
0 0 0 2
Note: B(n,m) , n :number of row , m: number of column.
Example 4
Find A= ones (3) , B=ones(4) , C=ones(2) , Z = 3*C by using Matlab
Solution:
1 1 1 1
1 1 1 1
1 1 1 1 1 1 1 1 3 3
A= B= C= Z=
1 1 1 1 1 1 3 3
1 1 1
1 1 1 1
* To change the number that in first row and second column to zero Z(1, 2)=0
3 0
Z=
3 3
Example 5
Find A=zeros(3) , B=zeros(4) by using Matlab
Solution:
0 0 0 0
0 0 0 0 0
A = 0 0 0
0 0
B =
0 0 0 0
0 0 0
0 0 0 0
Example 6
Find A=diag (1:3) , B=diag (0:2) by using Matlab
Solution:
1 0 0 0 0 0
A= 0 2 0 B = 0 1 0
0 0 3 0 0 2
Example 7
Find Z=diag(2:3:13) H=diag(4:5:15)
Solution:
Note : (2:3:13)
Start = 2 Additional = 3 final = 13
Note :
If the value of additional is not given that value of additional equal=1 but 13 is not
found in the result?? Because if 11+3 =14 this value is out of the range [2→13] , so we stop
at 11
2 0 0 0
0 4 0 0
0 0
H= 0 9 0
5
Z=
0 0 8 0
0 0 14
0 0 0 11
Lecture 5
Differentiation and Integration
Example 1
Y=X3+3X2+X-5 , find Y' , Y" , Y'"
Solution:
Syms X by using Matlab
Y= X^(3)+3*X^(2)+X-5
diff(Y) or diff(Y,X)
Y' = 3*X^(2)+6*X+1
diff(Y,2) or diff(Y,X,2)
Y" = 6*X+6
diff(Y,3) or diff(Y,X,3)
Y'" = 6
Example 2
Y=5x +3 , find ∫ , ∫ by using Matlab
Solution:
Syms x
Y=5*X+3
Int(y) or int(y,x) ∫ =(5*X+3)^2/10
Or pretty (int(y)) ∫ =
int (y,0,2) or int (y,X,0,2) ∫ =16
Lecture 6
For , IF , While
For
for → expression statements Example 3
end for x=1:5
Example 1 A=x2+1;
for X=1:5 G(X,:)=A
A=X2+1; end
G(:,X)=A; G
end Solution:
G G= 2
Solution: 5
G= 2 5 10 17 26 10
Example 2 17
i=1; 26
for x=5:10 Example 4
a=3*x-5; i=1;
b(:,i)=a; for x=5:10
i=i+1 a=3*x-5;
end b(I,:)=a;
b i=1+i
Solution: end
b=10 13 16 19 22 25 Solution:
b=10
13
16
19
22
25
b(5)=22
Check if any error in the program: Correct answer
1- for x=5*y-2;
y=3:6; 1- for y=3:6;
end x=5*y-2;
2- for a=1:100; end
b=10-a;
c(a,:)=b 2- for a=1:100;
c b=10-a;
end c(a,:)=b
end
c
IF
If → expression statements Example 2
End x=[85 90 95 92];
a=mean(x);
Example 1 if a>90
x=[80 85 90 95]; disp (' Excellent')
a=max(x); elseif a<90 & a>80
if a>90 disp('very good')
disp('Excellent") elseif a<80 & a>70
end disp('good')
a end
Solution: Solution:
a= 95 b=90.5
disp=Excellent disp= Excellent
Example 3 Solution 3:
i=1; y=
for x=-2:3 0
if x>0 0
y(i,:)=2*x+1; 0
end 3
i=i+1; 5
end 7
Check if any error in the program: Correct answer
1- Qo=[500,800,1000]; 1- Qo=[500,800,1000];
a=min(Qo) a=min(Qo)
if a<600 if a<600
disp(formation damage) disp(formation damage)
end
2- for p=1000:500:3500 2- for p=1000:500:3500
If p>=2500 If p>=2500
Rs=900 Rs=900
elseif Rs =0.4*p elseif p<2500 & p>=1000
else p<2500 Rs =0.4*p
end else Rs =0.5*p
end
end
3- for a=1:5 3- for a=1:5
B=5 B=5
if a>B; if a>B;
x=a^2 x=a^2
elseif else
x=0; end x=0; end
end
4- a=5;b=6; 4- a=5;b=6;
if a>b if a>b
A =(2+a^2) A =(2+a^2)
else a<b elseif a<b
A =(2+b^2) A =(2+b^2)
elseif else
A=0 A=0
end end
While
while → expression statements Example 2
end c=0.5
while c>=0
Example 1 y=sqrt(c)
n=3; c==c-0.1
While n>0 end
x=2*n
n=n-0.5
end
Example 3
c=-2;
while c<0
x=c^(2);
c=c+1
end
Solution:
By using Matlab
Check if any error in the program: Correct answer
n=1; n=1;
while n>=0 while n>=0
x=2*n x=2*n
n=n-0.5 n=n-0.5
end
Exercises 1
Output
File Name Input Variables
Variables
Function [A, n]=Area(a, b, c) Area A, n a,b,c
1. Correct the program if any error:
Before After
For a=1:5 for a=1:5
B=5; B=5;end
If a >= B; X=a^2; if a >= B; X=a^2 ;
elseif else
X=0;end X=0;end
Command Window Find the Results of (G)
>> x = [1: 2 :8]; 1 0 0 0
0 3 0 0
>>G = diag (x)
0 0 5 0
0 0 0 7
>>x = [8 ; -4 ; 1]; 0 1 8
3 2 4
>>c=[0 1; 3 -2 ; 4 2];
4 2 1
>> G = [c x]
>>G=[1 3, sqrt(5)]; length(G) =3
>>length(G) size(G) = 1 3
>>size(G)
>> a= 2; b=4;c=5; G=6
>> G = 2*c+(a^3)/2-2*b
>> a = 4; b= [1 2; 3 4]; 4 2
>> G = a ./ b G= 1.3333 1
Matlab Code
x3 y , y is a matrix X= sum(sum(y))^(1/3)
x e1/ 3 X=exp(1/3)
A r2 A=pi*r^2
Exercises 2
1. If V1, V2 and V3 are the volume fraction of sand, clay and pore space. Let ФN denote
the neutron value (dimensionless) and ρ the density in (g/cm3). The log evaluation is
based on the
simultaneous 2 V1 37 V2 100 V3 N
application
2.65 V1 2.41 V2 V3
of the
V1 V2 V3 1
following
system of equation:
At certain depth , the measurements showed that ФN=19.33 and ρ=2.3245 g/cm3.
i. Write the above system in matrix form: AX = b, write the matrix A, the vector V or
X and the right hand side vector b.
ii. Find the values of V1, V2 and V3 using Matlab program.
Solution
2 37 100 V1 19.33
2.65 2.41 1 V2 2.3245
=
1 1
1 V3 1
AV=b
2 37 100 V1 19.33
A= 2.65 2.41 1 V= V2 b= 2.3245
1 1 1 V3 1
Matlab cod
clear all
close all
clc
A=[-2 37 100;2.65 2.41 1;1 1 1 ];
b=[19.33;2.3246;1];
V=inv (a)*b
Results:
0.7598
V= 0.0503
0.1899
2. Temperature dependency of density and viscosity of water is given by following
equations:
62.122 0.0122T 1.54 *10 4 T 2 2.65 *10 7 T 3 2.24 *10 10 T 4
1057.51
ln 11.0318
T 214.624
Where T is in oF, ρ is in 1bm/ft3, and μ is in 1bm/ft
Calculate the density ρ, viscosity μ and the kinematic viscosity (μ/ρ) between 35 and 200
o
F with a 5oF increase.
Solution
Matlab cod
clear all
close all
clc
i=1;
for T=35:10:200
ro(i,:)=62.122+0.0122*T-1.54*10^-4*T^2+2.65*10^-7*T^3-2.24*10^-10*T^4 ;
u(i,:)= exp (-11.0318+((1057.51)/(T+214.624)));
z(i,:)=u(i,:)/ro(i,:);
i=i+1; end
disp('==============================================')
disp(' ro u z ')
disp('===============================================')
dd=[ ro u z]
3. Solve the following set of equations using Matlab.
10 8 9 X 1 35
9 7 10 X 2 25
13 14 12 X 3 4
Solution
A=[10,8,9;9,-7,10;-13,-14,12]
b=[35;-25;4]
x=inv(m)*c
result
- 2.4513
X= 4.3467
2.7489