Professional Documents
Culture Documents
Mat Labs SiMuLink Tutorial
Mat Labs SiMuLink Tutorial
Introduction to
MATLAB and Simulink
What can you gain from the course ?
Know what MATLAB/Simulink is
Know how to get started with MATLAB/Simulink
Know basics of MATLAB/Simulink
know how to solve simple problems
Be able to explore MATLAB/Simulink on
your own !
Introduction to
MATLAB and Simulink
Contents
Introduction
Getting Started
Vectors and Matrices
Built in functions
MATLAB
SIMULINK
Introduction
MATLAB MATrix LABoratory
Initially developed by a lecturer in 1970s to help students learn linear algebra.
It was later marketed and further developed under MathWorks Inc. (founded in
1984) www.mathworks.com
Matlab is a software package which can be used to perform analysis and solve
mathematical and engineering problems.
It has excellent programming features and graphics capability easy to learn and
flexible.
Available in many operating systems Windows, Macintosh, Unix, DOS
It has several tooboxes to solve specific problems.
Introduction
Simulink
Used to model, analyze and simulate dynamic
Getting Started
Run MATLAB from Start Programs MATLAB
Depending on version used, several windows appear
For example in Release 13 (Ver 6), there are several windows
command history, command, workspace, etc
For Matlab Student only command window
Command window
Variables
Vectors and Matrices
ALL variables are matrices
e.g. 1 x 1Variables4 x 1
1x4
3
3 2 i.e1x 7X
4 They are casesensitive
2x4
2 1 5 6
9 3 2 4
2
Their names
can
contain
up
to
31
characters
9with
a letter
Must start
3
>>> whos
Name
Size
Bytes Class
1x1
8 double array
>>> i1=4
i1
1x1
8 double array
i1 =
v1
1x1
8 double array
>>> R=v1/i1
>>> who
R=
0.7500
>>>
R
>>>
i1
v1
A row vector
A
1are 2 3
values
separated by
spaces
10
12
A column
B 14
vector
values are
separated
16by
semicolon
18
(;)
4 5
Columns separated by
space or a comma
1 2 3
4 5 6
7 8 9
Rows separated by
semi-colon
>>>A(1,:)
ans=
123
>>>A(:,3)
ans=
3
6
9
>>>A(2,:)
ans=
456
beep
ans=
pi ()
Inf
>>>pi
ans=
3.1416
>>>i
ans=
0+1.0000i
>>>A/3
ans=
0.33330.66671.0000
1.33331.66672.0000
2.33332.66673.0000
5
8
6
9
A^2 = A * A
>>>A.^2
ans=
149
162536
496481
>>>A^2
ans=
303642
668196
102126150
A*B
A.*B
1 2 3
4 5 6
7 8 9
>>>B=[111;222;333]
B=
111
222
333
1 1 1
2 2 2
3 3 3
7 x 3 8x 3 9x 3
14 14 14
32 32 32
50 50 50
1
8
10 12
21 24 27
A./B
? (matrices singular)
1/ 1 2 / 1 3 / 1
4 / 2 5 / 2 6 / 2
7 / 3 8 / 3 9 / 3
A.^B
7 3 83
31
62
93
1
16
2
25
3
36
Example:
-j5
2-90o
10
j10
1.50o
+ j0.1V2 = 1.5
j
0
.
1
V1
V
2
j2
=
1
.
5
V1 = 16.1229.7o V
Built in functions
(commands)
Scalar functions used for scalars and operate
element-wise when applied to a matrix or vector
e.g.
sin
cos
tan
atan asin
abs
angle sqrt
round floor
log
length
>>>max(b)
>>>a=linspace(0,(2*pi),10);
ans=
>>>b=sin(a);
0.9848
>>>max(a)
ans=
6.2832
>>>length(a)
ans=
10
>>>
eye
size
inv
det
eig
>>> x*xinv
x=
0.9501 0.8913 0.8214 0.9218
ans =
1.0000
0.0000
1.0000 0.0000
0.0000 1.0000
>>>
0.0000
j
0
.
1
V1
V
2
>>>x=inv(A)*y
x=
14.0000+8.0000i
28.0000+1.0000i
j2
=
1
.
5
plot
polar loglog
semilog
plotyy
mesh
surf
eg1_plt.m
>>>y2=cos(x);
>>>plot(x,y1,'r')
title
>>>hold
Currentplotheld
>>>plot(x,y2,'g')
>>>
eg1_plt.m
sin(x)
cos(x)
0.8
0.6
y1 and y2
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
3
4
angular frequency (rad/s)
eg2_srf.m
for f = 2
eg2_srf.m
eg3_srf.m
>>> [x,y]=meshgrid(3:.1:3,3:.1:3);
>>>z=3*(1x).^2.*exp((x.^2)(y+1).^2)...
10*(x/5x.^3y.^5).*exp(x.^2y.^2)...
1/3*exp((x+1).^2y.^2);
>>>surf(z);
eg2_srf.m
M-files :
Script and function files
When problems become complicated and require re
evaluation, entering command at MATLAB prompt is
not practical
Solution : use M-files
Script
Function
Collections of commands
eg1_plt.m
eg4.m
eg5_exercise1.m
+
V
Exercise 1:
Write an mfile to plot Z, Xc and XLversus
frequency for R =10, C = 100 uF, L = 0.01 H.
Z R j L
When
X C XL
Z R
1
o
LC
eg4.m
eg5_exercise1.m
100
80
60
40
20
200
400
600
800
1000
1200
1400
1600
1800
2000
eg6.m
+
V
For a given values of C and L, plot the following versus the frequency
a)
b)
Xc and XL
c)
eg6.m
100
Mag imp
Xc
Xl
80
60
40
20
0
200
400
600
800
1000
1200
1400
1600
1800
2000
1200
1400
1600
1800
2000
Phase
100
50
0
-50
-100
200
400
600
800
1000
INPUT
FUNCTION
Commands
Functions
Intermediate variables
OUTPUT
Output variable
input variable
File must be saved to a known path with filename the same as the
function name and with an extension .m
Call function by its name and arguments
helpreact_C
impedance.m
eg7_fun.m
Simulink
Used to model, analyze and simulate dynamic
systems using block diagrams.
Provides a graphical user interface for constructing
block diagram of a system therefore is easy to use.
However modeling a system is not necessarily easy !
Simulink
Simulink
i
Varies
from 0 to
2000 rad/s
100 uF
+
v(t) = 5 sin t
0.01 H
Simulink
10
100 uF
+
v(t) = 5 sin t
0.01 H
di 1
v iR L
idt
dt C
Simulink
1 dv di R d i
i
2
L dt dt L dt LC
Taking Laplace transform:
sV R
I
2
sI s I
L L
LC
sV
1
2 R
I s s
L
L
LC
Simulink
I V
s(1/ L)
R
1
2
s s
L
LC
s(1/ L)
R
1
2
s s
L
LC
Simulink
It is where we
obtain the blocks to
construct our model
It is here where we
construct our model.
Simulink
s+1
Transfer Fcn
simout
To Workspace
Simulink
s(1/ L)
R
1
2
s s
L
LC
s(100)
2
6
s 1000s 1 10
100s
s2+1000s+1e6
Sine Wave
Transfer Fcn
v
To Workspace1
i
To Workspace
Simulink
eg8_sim.mdl
Ramp
v
To Workspace3
w
1
1000
Constant
To Workspace2
s
Integrator
Dot Product3
100s
s2+1000s+1e6
sin
Elementary
Math
Dot Product2
Transfer Fcn1
i
To Workspace
Simulink
1
0.5
0
-0.5
-1
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
-5
Simulink
eg9_sim.mdl
5
Constant1
2000
Constant
0.802
Slider
Gain
100s
sin
s
Dot Product2
Integrator Elementary
Math
s2+1000s+1e6
Transfer Fcn
Scope
Reference
Internet
search engine
Mastering MATLAB 6 (Prentice Hall)
Duane Hanselman
Bruce Littlefield