Attachment 1
Attachment 1
MatLab Software
Esaias Abera
esaias.abera@wu.edu.et
Wollo University
Kombolcha Institute of Technology
School of Electrical, Computer and Biomedical Engineering
Kombolcha, Ethiopia
Outlines
1 Introduction to MatLab
4 Transfer Function
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 1 / 52
Introduction to MatLab
MatLab Basics
More than 5000 colleges and universities around the world use
MatLab and Simulink for teaching and research
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 2 / 52
Introduction to MatLab
Advantages of MatLab
One can plot data easily, and then change colors, sizes, scales,
etc of graphical outputs.
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 3 / 52
Introduction to MatLab
Advantages of MatLab
One can perform operations from the command line or can create
functions that perform repetitive tasks, just as any other
computer language.
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 4 / 52
Introduction to MatLab
Disadvantages of MatLab
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 5 / 52
Introduction to MatLab
Applications of MatLab
Mathematical optimization
Signal processing
MatLab Window
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 7 / 52
Introduction to MatLab
Basic command
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 8 / 52
Introduction to MatLab
MatLab variables
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 9 / 52
Introduction to MatLab
» p_value = 10;
» x123 = sqrt(5);
» MyVariable = 1e-5;
» x-point = 70; % wrong, contains minus sign
» 123A = -29; % wrong, start with a number
» if = 5; % wrong, reserved keyword
» var) = 4; % wrong, contains ’)’
» y(z) = 4; % wrong, contains ’()’
» mean value = 21; % wrong, contains blank space
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 10 / 52
Introduction to MatLab
Arithmetic Operations
Operation Symbol
Addition +
Subtraction -
Multiplication *
Right Division /
Left Division \
Power ∧
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 11 / 52
Introduction to MatLab
Predefined variables
pi π = 3.1415926535897....
i or j Imaginary unit = 0 + 1i
eps Epsilon = 2−52 = 2.2204e-16
inf Infinity (∞)
NaN Not-a-Number
realmin Smallest positive floating point number
realmax Largest positive floating point number
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 12 / 52
Introduction to MatLab
» c1=1+2j » y= 2/x
c1= 1.000 + 2.000i y= Inf
» c2 = 6-9*i » q= sin(x)/x
c2= 6.000 - 9.000i q= NaN
» q= real(c2)
q= 6 » x= 0;
»mag= abs(c2) » y= sin(x)/x
mag= 10.8164 y= NaN
» x= eps; % near zero
» a= cos(2*pi) » q= sin(x)/x
a=1 q= 1
» x= 0;
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 13 / 52
Introduction to MatLab
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 14 / 52
Introduction to MatLab
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 15 / 52
Array and Manipulation
Creating Array
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 16 / 52
Array and Manipulation
Creating Array...
» w= [2:4]’
w=
2
3
4
» x= linspace(0,2,5)
x = 0 0.5000 1.0000 1.5000 2.0000
0 0.5 1 1.5 2
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 17 / 52
Array and Manipulation
» w= logspace(0,2,5)
w = 1.0000 3.1623 10.0000 31.6228 100.0000
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 18 / 52
Array and Manipulation
Example:
» w= logspace(-2,2,5);
» z= 3+j*w;
» magz= abs(z)
magz = 3.0000 3.0017 3.1623 10.4403 100.0450
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 19 / 52
Array and Manipulation
Example:
» w= logspace(-2,2,50); 100
» s= 9-j*w; 80
» mag= abs(s); 60
40
» semilogx(w,mag,’linewidth’,2)
20
» grid 0
10-2 10-1 100 101 102
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 20 / 52
Array and Manipulation
Array Manipulation
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 21 / 52
Array and Manipulation
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 22 / 52
Array and Manipulation
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 23 / 52
Array and Manipulation
Special Array
zeros(n), zeros(n,n) % nxn zero matrix
Special function
n= length(b) % max(row,column)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 25 / 52
Array and Manipulation
»b= [1 2 3 4 5];
»A= [2 5;-2 3;4 5]; » m= numel(b) % number of en-
»m= size(A,1) % number of rows tries
m= 3 m= 5
»n= size(A,2) % number of »A= [2 5;-2 3;4 5];
columns » m= numel(A) % number of en-
n= 2 tries
»n= length(A) % max(row,col- m= 6
umn) »B= reshape(A,1,6) % 1x5 vector
n= 3 B= 2 -2 4 5 3 5
»b= [1 2 3 4 5 6]; »B= reshape(A,2,3) % 2x3 ma-
»n= length(b) % max(row,col- trix
umn) B=
n= 6 2 4 3
-2 5 5
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 26 / 52
Array and Manipulation
Example:
Calculate sin(t) and cos(t) and save them with t in array buf, (0 ≤
t ≤ 4π).
buf= [];
for t= 0:pi/30:4*pi
buf= [buf; t sin(t) cos(t)];
end
t= buf(:,1);
y1= buf(:,2);
y2= buf(:,3);
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 27 / 52
Graphs and Figures
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 28 / 52
Graphs and Figures
’details’ in plot
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 29 / 52
Graphs and Figures
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 30 / 52
Graphs and Figures
0.4
0.2
t=0:0.1:4*pi; 0
x=cos(t); -0.2
y=sin(t); -0.4
-0.6
z=tan(t)./(72*pi);
-0.8
plot(t,x,’-.b’,’LineWidth’,1.5) -1
0 2 4 6 8 10 12 14
hold on
plot(t,y,’*g’,’LineWidth’,1)
plot(t,z,’:hr’,’LineWidth’,1)
legend(’x’,’y’,’z’)
plot3(x,y,z,’LineWidth’,1.5)
grid
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 31 / 52
Graphs and Figures
[x,y]= meshgrid(0:0.1:5);
z= sin(x).*cos(y);
surf(x,y,z)
title(’Surf’)
xlabel(’x coordinate’)
ylabel(’y coordinate’)
zlabel(’z coordinate’)
surfc(x,y,z)
title(’Surfc’)
xlabel(’x coordinate’)
ylabel(’y coordinate’)
zlabel(’z coordinate’)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 32 / 52
Graphs and Figures
[X,Y]= meshgrid(-8:0.25:8);
F = sqrt(X.ˆ2 + Y.ˆ2) + eps;
Z = sin(F)/2;
mesh(X,Y,Z)
title(’Mesh’)
xlabel(’x coordinate’)
ylabel(’y coordinate’)
zlabel(’z coordinate’)
meshc(X,Y,Z)
title(’Mesh + contour’)
xlabel(’x coordinate’)
ylabel(’y coordinate’)
zlabel(’z coordinate’)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 33 / 52
Graphs and Figures
[X,Y]=meshgrid(-2:.2:2,
-2:.2:3);
Z = X.*exp(-X.ˆ2-Y.ˆ2);
mesh(X,Y,Z)
title(’Mesh’)
xlabel(’x coordinate’)
ylabel(’y coordinate’)
Contour
zlabel(’z coordinate’) 3
2.5
1.5
contour(X,Y,Z) y coordinate 1
title(’Contour’) 0.5
xlabel(’x coordinate’) 0
-0.5
ylabel(’y coordinate’)
-1
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
x coordinate
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 34 / 52
Graphs and Figures
Exercise: 1
1
Hint: square root in MatLab command = sqrt(x)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 35 / 52
Transfer Function
Transfer Function
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 36 / 52
Transfer Function
Example:
Consider the polynomial p(t) = t2 − 2t + 1
1 Define the polynomial on MatLab.
2 Calculate the polynomials roots.
3 Calculate the value of the polynomial for t = 1
Example:
syms t
sym = tˆ3+2*t+6;
poly = sym2poly(sym) % Convert symbolic polynomial to polyno-
mial matrix
poly = 1 0 2 6
sym_again = poly2sym(poly,t) % Convert back to symbolic polyno-
mial
sym_again = tˆ3 + 2*t + 6
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 38 / 52
Transfer Function
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 39 / 52
Transfer Function
Example:
MatLab script
num=[1 2];
den=[1 4 3 0];
[c,p,k]=residue(num,den)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 40 / 52
Transfer Function
Example: ...
Output
c=
-0.1667
-0.5000
0.6667
p=
-3
-1
0
k=
[]
Exercise: 2
2
Hint: [num,den] = residue(c,p,k)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 42 / 52
Transfer Function
MatLab command
sys = tf(num,den) % num = [bm bm−1 ... b0 ] and den = [an an−1 ... a0 ]
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 43 / 52
Transfer Function
sys = zpk(z,p,k)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 44 / 52
Transfer Function
MatLab command
s = tf(’s’); s = zpk(’s’)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 45 / 52
Transfer Function
Example:
1 For the following transfer function
s+1
H(s) =
s2 + 3s + 1
MatLab script
num = [1 1];
den = [1 3 1];
sys = tf(num,den) % Create transfer function
sys_zpk = zpk(sys) % Convert to zpk model
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 46 / 52
Transfer Function
Exercise: 3
1 For the following transfer function
s+2
G(s) =
(s + 1)2 (s + 3)
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 48 / 52
Transfer Function
Example:
Find the poles and zeros of the following transfer function and
plot them in the Complex plane.
5(s2 + 6s + 10)
G(s) =
(s + 1)(s2 + 4s + 5)
s=tf(’s’);
sys=(5*(sˆ2 + 30*s + 50))
/(sˆ3 + 5*sˆ2 + 9*s + 5);
pole(sys)
zero(sys)
pzmap(sys);
grid
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 49 / 52
Transfer Function
Example:
Consider the following transfer function; and find poles and zeros of
the system then plot the step and impulse response.
s2 + 2s + 1
G(s) =
s3 + 3.8s2 + 8.76s + 5.96
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 50 / 52
Transfer Function
Exercise: 4
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 51 / 52
Transfer Function
Thank you!
Esaias. A (WU-KIoT) Introduction to Control System using MatLab March 19, 2024 52 / 52