You are on page 1of 57

MATLAB & SIMULINK Tutorial for

MDB3103 Control System

Universiti Teknologi Petronas


Department of Mechanical Engineering
May 2016
Introduction

 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

More exercises are available at:


http://ctms.engin.umich.edu/CTMS/

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

 Please try also:


>> help elfun
>> help plot
>> doc plot
>> lookfor plot
>> which plot

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

Change an element: Transpose: Diagonal matrices:

>> 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

Multiple row column indexing


The syntax
A([m],[n]) Linear indexing
Row column indexing The syntax
The syntax >> A([2,3],[2,3]) A(I)
ans =
A(m,n) 5 6 >> A(9)
8 9 ans =
>> A(1,3) 9
ans = Extract the entire 3rd column
3 >> A(:,end) >> A([7,8,9])'
ans = ans =
>> A(1,end) 3 3
ans = 6 6
3 9 9
Cell arrays
To concatenate elements into a cell array , use curly bracket { } instead of square bracket [ ]

Create cell array


>> x={pi,[];'foo',42;ones(2), eye(2)} Accessing cell Accessing contain
x=
>> y=x(2,2)
y= >> y=x{2,2}
[ 3.1416] [] y=
'foo' [ 42] [42] 42
[2x2 double] [2x2 double]
Size and Shape Operators

This operators access basic array information


Find the length of a vector Reshape a matrix

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


>> b = [1 2 3 4]; >> reshape(A,4,1)
>> length(b)
ans =
ans =
1
4 3
2
4
Find the size of a matrix
Find the number of dimensions of a matrix

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


>> size(A) >> ndims(A)

ans = ans =

2 2 2
Matrix-Vector Operation
Practice

>> A = [1 2; 3 4]; >> A * B


>> B = [5 6; 7 8];
>> A + B ans =

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 =

0 + 1.0000i >> real(A)


>> A = rand(2) + i*rand(2), x = [1; 0], A x ans =
A= 0.9355 0.4103
0.9169 0.8936
0.9355 + 0.0579i 0.4103 + 0.8132i
0.9169 + 0.3529i 0.8936 + 0.0099i >> imag(A)

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 = load(‘data.txt')

data =

60 75 55 80 70

>> test = load('test.mat')

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 = [0.8 -0.2 0 1.5 -1.8]

x=

0.8000 -0.2000 0 1.5000 -1.8000

>> 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

F(i) = x(i)^2; 0.6

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

switch expression method = ‘linear';


case 1
do these statements switch (method)
case 2 case {'linear','bilinear'}
do these statements disp('Method is linear')
case n case 'cubic'
do these statements disp('Method is cubic')
end case 'nearest'
disp('Method is nearest')
otherwise
disp('Unknown method.')
end

29
How to Make a Function
Practice

function [output] = <name_of function>(input)

1 Type inside an m-file:


function [a,b] = <function_name>(c,d,e)
a = c+d+e
b = c*d*e
end

2 Save according to name of the function:


<function_name>.m

3 Try to run the function you just created in command window


>> [a,b] = function_name(7,2,5)

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

function [A,C] = test_function(r)


A = pi*(r^2) % Area of circle
C = 2*pi*r % circumference
end
>> [A,C] = test_function(6)

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

gtext('graph 1') gtext('graph 2')


Plotting and Graphics (2D)

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

legend('graph 1','graph 2') -15

-20
0 2 4 6 8 10 12 14 16 18 20
Time

>> helpwin plot


Subplot (2D)

figure
t=0:0.05:10; sinus 1/4 Hz cosinus 1/4 Hz
1 1

sinus=sin(2*pi*0.25*t); 0.5 0.5

cosinus=cos(2*pi*0.25*t); 0 0

kotak=square(2*pi*0.25*t); -0.5 -0.5

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

plot(t,sinus), title('sinus 1/4 Hz') 0.5 0.5


subplot(2,2,2); 0 0
plot(t,cosinus), title('cosinus 1/4 Hz') -0.5 -0.5
subplot(2,2,3);
-1 -1
plot(t,kotak), title(‘square 1/4 Hz') 0 5 10 0 5 10

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 + x5 );
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’;

Using the ode45 function for time span -1 to 1 and initial


values x1(-1)=1 and x2(-1)=1

43
Control System Toolbox

Collection of M-files built for solving problems in Control System

44
Transfer Functions
Differential Eq.:

Transfer Function:

Use “tf” which expects the


polynomial of the numerator
and denumerator

45
Transfer Functions
Differential Eq.:

Transfer Function:

Use “tf” which expects the


polynomial of the numerator
and denumerator

46
Transfer Functions
TFs in series:

Suppose:

Find E(s) using


“series” command

Try also “conv” command


47
Transfer Functions
TFs in series:

Using the same C(s) and D(s)

Find Y(s)/U(s)=Gcl(s) using “feedback” command

48
SIMULINK
OVERVIEW

49
Simulink

• A MATLAB Toolbox for modeling, simulating and


analyzing dynamic systems
• Tool for model-based control system design

50
Simulink Library Browser

51
Model Editor

• Creating a model: File->New->Model


• Drag blocks from Simulink Library Browser and drop
block in the Model Editor window
• Go to: /Sources; /Continuous; /Sinks
• Saving a model: File->Save As <modelname>.slx
(2011 version and older use .mdl)

Practice

52
Modeling: Block Parameters
Practice

53
Running Simulations

• Simulation->Model Configuration parameters


• Simulation->Run

54
Simulations Results

• Try different simulation stop time, frequency, Max step size


• Practice also with To Workspace, Step
• How to use parameters in .m script file for Simulink use?

55
Example: 1st Order Differential
Equation in Simulink
• From
http://ctms.engin.umich.edu/CTMS/index.php?example=CruiseContr
ol&section=SimulinkModeling

56
Example: 2nd Order Differential
Equation in Simulink

57

You might also like