Beruflich Dokumente
Kultur Dokumente
Introduction to
MATLAB and Simulink
Introduction
MATLAB MATrix LABoratory
Initially developed by a lecturer in 1970s to help students
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
Getting Started
+
V
Getting started
Example Resonance circuit (cont.)
z Resonance
Z= R + /
&
Z = R2
Getting started
Example Resonance circuit (cont.)
1
L =
C
1
o =
LC
Getting started
Example Resonance circuit (cont.)
z Given R = 10
C = 0.1 uF L = 0.01 H,
R = 10
C = 100uF
+
V
L = 0.01 H
Getting started
Example Resonance circuit (cont.)
1
o =
LC
Special variables
As a calculator
beep
EDU 1/(sqrt(0.01*100e-6))
ans =
Default
1000
variable used to
EDU
store results
pi ()
inf (e.g. 1/0)
i, j (
Getting started
Example Resonance circuit (cont.)
Getting started
Example Resonance circuit (cont.)
Variables
Must
startinwith
a letter
Variables and commands are
placed
Matlab
Workspace
These variables and commands can be recalled from
workspace, how ?
Getting started
Example Resonance circuit (cont.)
Script Mfiles
Example Resonant circuit (cont.)
When problems become complicated and require re
evaluation, entering command at MATLAB prompt is
not practical
Solution : use M-files
M-files a collection of MATLAB commands saved
in a text file with an extension .m
Script Mfiles
Example Resonance circuit (cont.)
1
Xc =
C
XL = L
Script Mfiles
Example Resonance circuit (cont.)
% capacitance
L=0.01;
% inductance
w_o = 1/(sqrt(L*C));
% resonant frequency
Xc = 1/(w_o*C);
Xl = w_o*L;
clc;
fprintf(\n The capacitor reactance is: %.1f \n, Xc);
fprintf( The inductor reactance is: %.1f \n, Xl);
Script Mfiles
Example Resonance circuit (cont.)
Script Mfiles
Example Resonance circuit (cont.)
Exercise
Write an extension to the program so that
it will display:
a) Magnitude of the impedance
b) Angle of the impedance
at any frequency entered.
Function M-files
z Function is a black box that communicates with
INPUT
FUNCTION
Commands
Functions
Intermediate variables
OUTPUT
Function M-files
z Function is a black box that communicates with
INPUT
FUNCTION
Commands
Functions
Intermediate variables
OUTPUT
function x=impedance(r,c,l,w)
%IMPEDANCE calculates Xc,Xl and Z(magnitude) and
%Z(angle) of the RLC connected in series
%IMPEDANCE(R,C,L,W) returns Xc, Xl and Z (mag) and
%Z(angle) at W rad/s
%Used as an example for IEEE student, UTM
%introductory course on MATLAB
if nargin <4
error(not enough input arguments)
end;
x(1) = 1/(w*c);
x(2) = w*l;
Zt = r + (x(2) - x(1))*i;
x(3) = abs(Zt);
x(4)= angle(Zt);
R=input(Enter R: );
C=input(Enter C: );
L=input(Enter L: );
w=input(Enter w: );
y=impedance(R,C,L,w);
fprintf(\n The magnitude of the impedance at %.1f
rad/s is %.3f ohm\n, w,y(3));
fprintf(\n The angle of the impedance at %.1f rad/s is
%.3f degrees\n\n, w,y(4));
circuit
suppose we want to display the variation of
impedances with frequency we want to plot
the impedances versus the frequency.
Arrays (cont)
10
12
14
16
18
Arrays (cont)
A row vector
values are
separated by
spaces
B=
10
12
14
16
18
EDU
A column
vector
values are
separated by
semicolon
(;)
Arrays (cont)
A = [1 2 3 4 5]
10
12
B = 14
16
18
Arrays (cont)
Size
1x100
Bytes Class
800 double array
Arrays (cont)
Size
Bytes Class
c1
1x100
c2
1x100
Arrays (cont)
Arrays (cont)
z Arrays Mathematics
1 1 1 1
1 2 3 4
2 2 2 2
b
=
a = 5 6 7 8
3 3 3 3
9 10 11 12
Scalar-array
EDU a-3
ans =
-2
-1
Arrays (cont)
z Arrays Mathematics
1 1 1 1
1 2 3 4
2 2 2 2
b
=
a = 5 6 7 8
3 3 3 3
9 10 11 12
Scalar-array
EDU 2*a/4 - 3
ans =
-2.5000 -2.0000 -1.5000 -1.0000
-0.5000
1.5000
2.0000
0.5000
2.5000
1.0000
3.0000
Arrays (cont)
z Arrays Mathematics
Array-array
1 1 1 1
1 2 3 4
2 2 2 2
b
=
a = 5 6 7 8
3 3 3 3
9 10 11 12
EDU a-b
ans =
0
EDU
Arrays (cont)
z Arrays Mathematics
1
2
3
4
Array-array
a = 5 6 7 8
9 10 11 12
EDU a.*b
EDU a./b
ans =
ans =
10
12
14
27
30
33
EDU
1 1 1 1
b = 2 2 2 2
3 3 3 3
1.0000
2.0000
3.0000
16
2.5000
3.0000
3.5000 4.0000
36
3.0000
3.3333
3.6667
EDU
4.0000
4.0000
Arrays (cont)
z Arrays Mathematics
Array-array
1 1 1 1
1 2 3 4
2 2 2 2
b
=
a = 5 6 7 8
3 3 3 3
9 10 11 12
INCORRECT
CORRECT
a*b
a.*b
a^b
a .^b
2^b
2.^b
Arrays (cont)
z Arrays Mathematics
Array-array
EDU size(a)
EDU ones(4)
EDU eye(4)
Arrays (cont)
Z= R + /
&
1
Xc =
C
XL = L
Arrays (cont)
Exercise:
Write an mfile to plot Z, Xc and XLversus
frequency for R =10, C = 60 uF, L = 0.05 H.
Arrays (cont)
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
10
100 uF
+
v(t) = 5 sin t
0.01 H
Simulink
It is where we
obtain the blocks to
construct our model
It is here where we
construct our model.
Simulink
10
100 uF
+
v(t) = 5 sin t
0.01 H
di 1
idt
v = iR + L +
dt C
Simulink
1 dv di R d i
i
=
+ 2+
L dt dt L dt LC
Taking Laplace transform:
I
sV R
2
= sI + s I +
LC
L L
sV 2 R
1
= I s + s +
L
L
LC
Simulink
s(1/ L)
I = V
R
1
s2 + s +
L
LC
s(1/ L)
1
2 R
s + s+
L
LC
Simulink
sim ou t
To Workspa ce
Simulink
s(1/ L)
1
2 R
s + s+
LC
L
s(100)
2
6
s + 1000s + 1 10
100s
s 2+1 0 0 0 s+1 e 6
S in e Wa ve
i
To Wo rksp a ce
Simulink
Ra m p
v
To Wo rkspa ce 3
w
To Worksp a ce 2
1
10 00
Co n sta n t
s
Inte
g
ra to r
Do t P ro d uct3
10 0s
s 2+10 0 0s+1 e 6
sin
Ele m e n ta ry
Ma th
Do t P ro d uct2
i
To Wo rksp a ce
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
2000
Constant
0.802
Slider
Gain
100s
sin
s
Dot Product2
Integrator Elementary
Math
s 2+1000s+1e6
Scope
Transfer Fcn
Simulink
Masking
Enable a user to customize a dialog for a block
Change numerical values to variables
Select the block edit edit mask initialize
Simulink
Masking
Reference
z Internet
search engine
z Mastering MATLAB 6 (Prentice Hall)
Duane Hanselman
Bruce Littlefield