Professional Documents
Culture Documents
Background
Developed by MathWorks since 1990
UTP has floating network license for Matlab ver. 2013b
to 2015a + many toolboxes
Benefits
• Matlab is a very powerful and popular software in
engineering
• Simplicity and complete ready used tools
• Computation of dynamical systems and mathematical
• Numerical computation
• Modeling, Simulation, Visualization and Control System
Design.
2
Topics
Matlab Review
Simulink Overview
Control System Design Using ‘sisotool’
Exercise 1: Cruise control
Exercise 2: DC Motor Speed
Exercise 3: DC Motor Position
Exercise 4: Inverted Pendulum
3
MATLAB REVIEW
4
MATLAB Environment
5
MATLAB Help
Matlab provides excellent online help. Just try to be a little creative and
you can find help on almost anything Matlab can do. You may use Help
Browser or in command window
>> help
>> helpwin
>> doc
6
A Matlab Program
7
Script Editor
8
Variables
9
Operators
10
Numeric Data Type
11
Who, Whos and Clear
These Matlab commands are extremely useful to determine the available
Matlab variable, and to clear those variables.
>> who
>> whos
>> clear
Practice
>> x = 2
>> whos
x= Name Size Bytes Class >> time = 10
2 x 1x1 8 double array time =
y 1x1 8 double array
>> y = 5 z 1x1 8 double array 10
y= Grand total is 3 elements using 24 bytes >> velocity = 50
5 velocity =
>> z = 10 50
z=
10
Matrices and Vectors
Practice
>> A = [1 2 3] >> C = [1 2 3; 4 5
6] E=
?
A=
C= 1 4
1 2 3 2 5
1 2 3 3 6
4 5 6
>> B = [1; 2;
3] >> D = [1 2; 3 4; 5 6
]
F= ?
B=
D=
1 3 5
1 2 4 6
2 1 2
3 3 4
5 6
13
Matrices and Vectors
>> A = [1 2 3; 4 5 6; 7 8
>> A = [1 2 3; 3 4 5] >> A = [1 2 3; 4 5 6]
9]
A= A=
A=
1 2 3 1 2 3
1 2 3
3 4 5 4 5 6
4 5 6
7 8 9
>> A(2,1) = 6 >> B = A'
>> B = diag(A)
A= B=
B=
1 2 3 1 4
6 4 5 2 5
1
3 6
5
9
Matrices and Vectors
Create an empty array Calculate the determinant Augmented matrix
>> C = [-1 2 1; 3 4 5; -2 1 7]
C=
>> A = [1 2 3; 4 5 6; 7 8 9] >> B = [3 5 2; 6 4 5; 1 4 2]
-1 2 1
A= B= 3 4 5
-2 1 7
1 2 3 3 5 2
4 5 6 6 4 5 >> D = [12; 10; 11]
7 8 9 1 4 2
D=
>> A(2,:) = [] >> det(B)
12
A= ans = 10
11
1 2 3 -31
7 8 9 >> E = [C D]
E=
-1 2 1 12
3 4 5 10
-2 1 7 11
Range Operators
The range operators allows one to set up a vector of equally spaced entries
Defining vectors with the range operator Accessing data from arrays with the range operator
>> A = [1 2 3; 4 5 6; 7 8 9]
>> x = 1:4
A=
x=
1 2 3
1 2 3 4 4 5 6
7 8 9
>> y = 1:2:10
>> B = A(:,1)
y=
B=
1 3 5 7
9 1
4
7
Matrix indexing
>> A=[1 2 3; 4 5 6;7 8 9]
A=
1 2 3
4 5 6
7 8 9
ans = ans =
2 2 2
Matrix-Vector Operation
Practice
ans = 19 22
43 50
6 8
10 12
>> A - B
>> x = [1 2];
ans = >> A * x
??? Error using ==> mtimes
-4 -4 Inner matrix dimensions must agree.
-4 -4
20
Array Operation
Element-wise operations
>> A = [1 2; 3 4];
>> B = [5 6; 7 8]; Left matrix divide
>> A.*B
>> A = 4;
ans = >> B = 2;
>> C = A/B
5 12
21 32 C=
2
>> A./B
>> D = A B
ans =
D=
0.2000 0.3333
0.4286 0.5000 0.5000
21
Complex Arithmatics
>> i
ans =
ans =
x=
0.0579 0.8132
1 0.3529 0.0099
0
ans =
0.5292 + 0.6014i
-0.3145 - 0.8225i
22
Data Handling (Save and Load)
Save
savefile = 'test.mat';
>> data = [60 75 55 80 70]; p = rand(1, 10);
>> save data.txt data -ascii q = ones(10);
save(savefile, 'p', 'q')
Load
data =
60 75 55 80 70
test =
p: [0.8147 0.9058 0.1270 0.9134 0.6324 0.0975 0.2785 0.5469 0.9575 0.9649]
q: [10x10 double]
Built-in Functions
24
Example: Built-in Functions
• abs(x) :
>> abs(-5)
Practice
ans =
• sign(x)
x=
>> y = sign(x)
y=
1 -1 0 1 -1
25
Example: Built-in Functions
Practice
• roots(x) : to find polynomial root of x
y = x2 + 5x + 6 p = x3 – 6x2 – 72x - 27
>> p = [1 -6 -72 -27]
>> y = [1 5 6]
p=
y=
1 -6 -72 -27
1 5 6
>> r = roots(p)
>> r = roots(y)
r=
r=
12.1229
-3.0000
-5.7345
-2.0000
-0.3884
26
Polynomials
Practice
27
For Loop
Practice
1
0.9
x = -1:0.01:1;
0.8
for i=1:length(x)
if x(i) < 0.5 0.7
else 0.5
F(i) = 0.25; 0.4
end
0.3
end
plot(x,F) 0.2
0.1
0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
28
Switch Statements
29
How to Make a Function
Practice
a? b?
Another Practice for Creating a Function
Create a Matlab function to calculate Area and Circumference
of a Circle by providing the radius of the circle
A=
113.0973
C=
37.6991
While Statements
figure;
x=linspace(0,4,500);
A=0.5:0.5:2; 4
i=2;
while i <= length(A) 3.5
y = A(i)* x.^(1/2); 3
plot(x,y); hold on;
i=i+1; 2.5
end 2
grid on;
1.5
0.5
0
0 0.5 1 1.5 2 2.5 3 3.5 4
32
Plotting and Graphics (2D)
Practice
t = 0:0.01:20; t = 0:0.01:20;
x = 20*exp(-0.05*t).*sin(t); y = 14*exp(-0.5*t).*sin(10*t);
plot(t,x) plot(t,y)
20 15
15
graph 1
10
graph 2
10
5
5
0 0
-5
-5
-10
-10
-15
-20 -15
0 2 4 6 8 10 12 14 16 18 20 0 2 4 6 8 10 12 14 16 18 20
Practice
20
15
t = 0:0.01:20; 10
x = 20*exp(-0.05*t).*sin(t);
plot(t,x) 5
0
hold on
-5
y = 14*exp(-0.5*t).*sin(10*t); -10
plot(t,y)
-15
-20
0 2 4 6 8 10 12 14 16 18 20
Plotting and Graphics (2D)
t = 0:0.01:20;
x = 20*exp(-0.05*t).*sin(t); 20
graph 1
plot(t,x,'r') 15 graph 2
hold on 10
5
y = 14*exp(-0.5*t).*sin(10*t);
Amplitude
plot(t,y,'b') 0
-5
xlabel('Time')
ylabel('Amplitude') -10
-20
0 2 4 6 8 10 12 14 16 18 20
Time
figure
t=0:0.05:10; sinus 1/4 Hz cosinus 1/4 Hz
1 1
cosinus=cos(2*pi*0.25*t); 0 0
gigi=sawtooth(2*pi*0.25*t); -1 -1
0 5 10 0 5 10
subplot(2,2,1); 1
kotak 1/4 Hz
1
gigi gergaji 1/4 Hz
subplot(2,2,4);
plot(t,gigi), title(‘saw tooth 1/4 Hz')
Plotting and Graphics (3D)
Practice X = [10 20 20 10 10];
Y = [5 5 15 15 5];
Z = [0 0 70 70 0];
>> t = 0:pi/50:10*pi; plot3(X,Y,Z); grid on;
>> plot3(sin(t),cos(t),t); xlabel('X axis'); ylabel('Y axis');
zlabel('Z axis');
title (‘Example of plot 3-D');axis([0 25 0 20 0 8
0])
40 Contoh plot 3-D
30
80
20
60
10
sumbu Z
40
0
1 20
0.5 1
0 0.5 0
0 20
-0.5 -0.5 15 25
-1 -1 20
10 15
5 10
5
sumbu Y 0 0
sumbu X
Plotting and Graphics (3D)
sin(r ) r x2 y2
z
r
1
x = linspace(-10,10,40); 0.5
y = x;
[X,Y] = meshgrid(x,y); 0
R = sqrt(X.^2+Y.^2);
Z = sin(R)./(R+eps);
surf(X,Y,Z); -0.5
10
5 10
0 5
0
-5 -5
-10 -10
Plotting Symbolic Equations
x 2 5x 6 0 x >> syms x
y >> y=x/(x+5
>> y = 'x^2 + 5*x + x5 );
6'; >> ezplot(y)
>> ezplot(y) 2 >> grid
x +5x+6
x/(x+5)
80
3
70
2
60
1
50
0
40
-1
30
-2
20
-3
10
-4
0
-6 -4 -2 0 2 4 6
-6 -4 -2 0 2 4 6
x
x
39
Laplace Transforms
Practice
40
Linear Differential Equation
41
Solving ODE
42
Solving ODE
Practice
Given the ODEs:
function dxdt=mydiff(t,x)
dxdt(1)=x(2);
Create a function for the ODEs dxdt(2)=x(1)
dxdt= dxdt’;
43
Control System Toolbox
44
Transfer Functions
Differential Eq.:
Transfer Function:
45
Transfer Functions
Differential Eq.:
Transfer Function:
46
Transfer Functions
TFs in series:
Suppose:
48
SIMULINK
OVERVIEW
49
Simulink
50
Simulink Library Browser
51
Model Editor
Practice
52
Modeling: Block Parameters
Practice
53
Running Simulations
54
Simulations Results
55
Example: 1st Order Differential
Equation in Simulink
• From
http://ctms.engin.umich.edu/CTMS/index.php?example=CruiseContr
ol§ion=SimulinkModeling
56
Example: 2nd Order Differential
Equation in Simulink
57