Académique Documents
Professionnel Documents
Culture Documents
Presented by
Dr. M. Nuruzzaman
Electrical Engineering Department, KFUPM
Office: 59-0077, Email: nzaman@kfupm.edu.sa, Ph: 2830
Under the auspice of ETS sem 152
Overview
What is MATLAB?
7
Enter 8
10
11
>>C=[7 8 10 -11]' or
>>C=[7;8;10;-11]
6
7
20
Enter 5 12 3
1 1
0
19
3
2
Space gap
between
elements,
semicolon
between rows,
and housed in
third bracket
R=1:3:10
C=[0:2: 10]'
Functional codes
for A OR B,
>>A=[1 1;0 1];
>>B=[0 1;1 1];
>>A|B
for A AND B,
>>A&B
ans =
0 1
0 1
for A XOR B,
>>xor(A,B)
ans =
1 0
1 0
ans =
1
1
1
1
>>z=zeros(3,1)
Syntax: ones(row
number,column number)
>>C=3*ones(3,2)
Matrix arithmetic
A=[0 2 3]
B=[-1 4 5]
A/5
1./A
3./A
Matrix inverse
2
0
0
1 5
2 1
0 2
12 14
1
2
0
0
0
89
14
1
2
A+5
A.^2
A./B
A^2
x
x y
y
1
y 2 yx x
x y
y 2 yx x
y yx x
x
2
y yx x
2
>>syms x y
>>A=[x y;x+y 1]; B=inv(A); pretty(B)
1/A
Prep. by: Dr. M. Nuruzzaman
8 64
64 216
1 47
3 87
C=A(3:8)
27 56
729 40
45 72
82
29
E=A([4 1])
Syntax: Array(required row,required column)
Submatrix selection
A([3 4],[1 2])
Consecutive element selection
A(2:4,2:4)
Column delete
A(:,3)=[ ]
Two row multiplication
A(:,1)=A(:,1).*A(:,2)/4
Particular colm division
A(:,1)=A(:,1)/2
Prep. by: Dr. M. Nuruzzaman
Appending rows
1 3 5
2 6 8
[9 5
0]
[4
7 8]
B=[1 3 5;2 6 8]
B=[B;[9 5 0]]
A=[B;[4 7 8]]
B=
B=
A=
1
2
Appending
1
2
3
6
5
8
columns
3 5
6 8
5 0
1
2
9
3
6
5
5
8
0
1
2
9
4
3
6
5
7
D=[D [9 0 1]']
C=[D [3 1 9]']
D=
D=
C=
3
6
5
5
8
0
1
2
9
3
6
5
5
8
0
5
8
0
8
5
8
0
8
1
2
9
3
6
5
7
1 3 5 9 3
2 6 8 0 1
9
5
0
1
9
3
1
9
9
0
1
1
2
9
4
9
0
1
1
2
9
3
6
5
5
8
0
9
0
1
3
1
9
f=[f k]
f=[f;k]
For-loop syntax
for counter = starting value:increment or decrement of the counter value : final value
end
for k=10:10:70
y(k/10)=cosd(k);
end
for the right shifting,
>>f=[ ]; for k=1:3 f=[f k^2]; end
>>f
f=
1
4
9
minimum/maximum/sum
/product/sort of data
max
min
sum
sort
prod
>>B=[1 3 7 5 -8]
>>max(B)
>>B=[1 3 5;2 6 8]
>>max(max(B))
Index returning
>>B=[1 3 7 5 -8]
>>[M,I]=max(B)
Prep. by: Dr. M. Nuruzzaman
y (12x4 4 x 1)(9x3 5)
x=[0 1 2]
y=(12*x.^4-4*x+1).*(9*x.^3-5)
Compute
y (12x4 4 x 1)(9x3 5)
1 x 2
x 0.5
x=-1:0.5:2;
y=(12*x.^4-4*x+1).*(9*x.^3-5)
Compute
f ( x, y) (12x4 xy 1)(9 y3 5)
for x=[0 1 2]
and y=[3 -2 1]
f ( x, y) (12x4 xy 1)(9 y3 5)
1 x 2
x 1
4 y 8
y 2
[x,y]=meshgrid(-1:1:2,4:2:8)
y=
x=
-1
-1
-1
0
0
0
1
1
1
4
6
8
2
2
2
4
6
8
4
6
8
4
6
8
f=(12*x.^4-x.*y+1).*(9*y.^3-5)
f=
9707
36841
96663
571
1939
4603
5139
105635
13573
350959
23015
814731
Prep. by: Dr. M. Nuruzzaman
Symbolic differentiation
d
dx
d
dx
(sin x) cos x
(cot1 x)
1
1 x2
d
[(12x 4 4x 1)(9x3 5)]
dx
d3
4
3
[(
12
x
4
x
1
)(
9
x
5)]
3
dx
>>syms x
>>y=diff(sin(x))
>>syms x
>>y=diff(acot(x))
>>syms x
>>y=diff((12*x^4-4*x+1)*(9*x^3-5))
>>syms x
>>y=(12*x^4-4*x+1)*(9*x^3-5)
>>yn=diff(y,3)
x(x2 1)2
>>syms x
>>y=int(sin(x))
>>syms x
>>y=int(1/x/(x^2+1)^2)
Definite symbolic
integration
02 sin xdx
x3
1
dx
2
2
x2 x( x 1)
(ln x) 4
dx
x
x 3
5
(ln x)4
dx
x
>>syms x
>>syms x
>>y=int(log(x)^4/x)
>>y=int(sin(x),0,pi/2)
>>syms x
>>y=int(1/x/(x^2+1)^2,2,3)
>>syms x
>>y=int(log(x)^4/x,3,5)
Prep. by: Dr. M. Nuruzzaman
Simple if
x=2;
if x>=1
y=sin(x);
end
if logical expression
end
If-else
x=1;
if x==1
y=sin(x*pi/2);
else
y=cos(x*pi/2);
end
if logical expression
end
Nested-if
if logical expression
else
end
elseif
elseif
elseif
elseif
N=77;
if N>=90
g='A';
(N<90)&(N>=80)
g='B';
(N<80)&(N>=70)
g='C';
(N<70)&(N>=60)
g='D';
(N<60)&(N>=50)
g='E';
else
g='F';
end
Prep. by: Dr. M. Nuruzzaman
Executable command(s)
end
where while and end are the reserve words
Switch-case-otherwise syntax
x y 8
9 x 8 y 5
x 2 y 2 z 2 8
2 y x 4
>>e1='x-y=-8';
>>e2='9*x+8*y=5';
>>s=solve(e1,e2)
>>[s.x s.y]
>>e1='x^2-y^2-z^2=-8';
>>e2='2*y+x=4';
>>e3='z-x=2';
>>s=solve(e1,e2,e3)
>>[s.x s.y s.z]
3 cos x 2 sin x 2
F ( )
t
jt
e
f (t) dt
f (t )
1
2
it
F
(
)
e
dt
F (s)
t
st
f
(
t
)
e
dt
t 0
f (t )
1 s c ist
e F (s) ds
2 i s c i
Z transform forward
F (z )
n
f [n] z
n0
Z transform inverse
f [n]
F (z) z
2 i C
n 1
dz
Z transform inverse
iztrans(function in
transform variable)
string
form,
independent
variable,
11
2
(x 2x)
x 2
1
x2
1
3 (x 1) 3 (x 2 x 1)
x3 1
>>syms x
>>f=1/(x^3+1);
>>R=maple('convert',f,'parfrac',x);
>>pretty(R)
1
0.3333 0.1667 j 0.2887 0.1667 j 0.2887
x3 1
x 1
x 0.5 j 0.866
x 0.5 j 0.866
>>syms x
>>N=1;
>>D=sym2poly(x^3+1);
>>[R P K]=residue(N,D)
R=
-0.1667 - 0.2887i
-0.1667 + 0.2887i
0.3333
P=
0.5000 + 0.8660i
0.5000 - 0.8660i
-1.0000
K=
[]
Prep. by: Dr. M. Nuruzzaman
Polynomial roots
x3 1 0
Polynomial multiplication
( x3 x 1)( x2 3)
Vo
V1 Vo
x2 4 x 7
?
Vo AV1
(Vi V1 ) sC
Vi
R
syms x
syms s R C V1 Vo Vi
sRCA
y=1/(x^2+4)+5/(x-7);
RCs A 1
e1=(Vi-V1)*s*C-(V1-Vo)/R;
[N,D]=numden(y)
e2='V1=-Vo/A';
pretty(N)
O1=maple('algsubs',e2,e1)
pretty(D)
O2=solve(O1,Vo)
Vi=1;
Use expand if
subs(O2)
necessary
Prep. by: Dr. M. Nuruzzaman
f ( x) x 2 x 8
f ( x1 , x2 , x3 ) x1 2 x1 x2 x3
2
p1 x1 2 x1 x2 x3
2
p2 x1 x2 x3
first derivative Dy
Second derivative D2y
Third derivative D3y
dy y 2
2x
4y
dx x
2x2
y
Cx
d2y
dy
2
15y 0
dt
dt 2
y(t)
S=dsolve('2*x*Dy=y^2/x+4*y','x');
pretty(S)
C1e5t C2e3t
S=dsolve('D2y-2*Dy-15*y=0');
pretty(S)
y y 2y 0
'''
'
y(x) C1e
x
7
x
7
x
e C2 cos
C3 sin
2
2
x
2
>>S=dsolve('D3y+Dy-2*y=0','x');
pretty(S)
dy1
5
y
2
y
1
2
dt
dy
2 y1 2 y2
dt
y1(0) 1
y (0) 2
y1
y2
3 t 8 6t
e e
5
5
6 et 4 e 6t
5
5
From data
ezplot(function,interval
as a row matrix)
y 2x 2 3x 5
Tabular data
3 x 3
y='2*x^2-3*x+5';
Command to plot y vs x:
-6
-4
-3
-5
>>x=[-6 -4 0 4 5 7];
>>y=[9 3 -3 -5 2 0];
>>plot(x,y)
ezplot(y,[-3,3])
Multiple y
Multiple y
hold
ezplot(x+4',[-3,3])
ezplot(sin(x)',[-3,3])
-6
-4
y1
-3
-5
y2
-2
7.7
y3
-1
-3
plot(x,y1,x,y2,x,y3)
Surface plot
from expression
ezsurf(function code,[x min x max y min ymax])
default
2 x, y 2
2
2
f ( x, y) 8(x y )
ezsurf('-8*(x^2+y^2)')
Parametric equation
x 2 cost
y sint
2 x 2
0 t 2
1
y
2.5 x 5.1
0 y4
f ( x, y) x 2 xy y 2
x='2*cos(t)'; y='sin(t)';
ezplot(x,y,[0,2*pi])
Implicit function
Surface plot
from data
Use surf or mesh
surf(functional data as a
retangular matrix,x
variation as a row matrix, y
variation as a row matrix)
Needs computing or
sample generation
3 y 2
ln y ln(x y 1) 1 y 2
E='1/y-log(y)+log(-1+y+x)+y^2-1';
D=[-2.5 5.1 -3 2];
ezplot(E,D)
x=-2:0.2:2;
y=0:0.2:4;
[X,Y]=meshgrid(x,y);
f=X.^2+X.*Y+Y.^2;
surf(x,y,f)
f ( x, y)
Syntax: ezcontour(code of f(x,y )under quote, x interval bounds as two
element row matrix, y interval bounds as two element row matrix)
1 2 2
f ( x, y) x y e3 x 4 y
2
1.2 x 1.2
1 y 1
Random numbers
Decimal random numbers:
unifrnd(lower bound,upper bound,row number,column number)
Generation of a single random
number X between 4 and 5,
X=unifrnd(-4,5)
X=randint(1,1,[-3 5])
Generation of a rectangular matrix X of order 33 in which each element is in
between -3 and 5,
X=randint(3,3,[-3,5])
Data statistics
datastats(y)