Sie sind auf Seite 1von 54

An Introductory Course on

MATLAB and Simulink


Dr. Nik Rumzi Nik Idris
nikrumzi@ieee.org
Department of Energy Conversion
FKE, UTM.

Introduction to
MATLAB and Simulink

What is expected from the course ?


Know what is MATLAB/Simulink

Know how to start MATLAB/Simulink


Know basics MATLAB/Simulink environment
know how to solve simple problems
Be able to explore MATLAB/Simulink on
your own !

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

systems using block diagrams.


Fully integrated with MATLAB , easy and fast to
learn and flexible.
It has comprehensive block library which can be
used to simulate linear, nonlinear or discrete
systems excellent research tools.
C codes can be generated from Simulink models for
embedded applications and rapid prototyping of
control systems.

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

Main window where commands are entered

Example of MATLAB Release 13 desktop

Getting Started

Example Resonant circuit


Let us look at a simple series resonant circuit. We will use
this circuit through out the course
I

+
V

Getting started
Example Resonance circuit (cont.)

z Resonance

If the frequency of the voltage source is varied


from 0 to a very high value, we will observe that
at a specific frequency, known as resonant
frequency, the current is maximum. Why ?
Because at this particular frequency, the
magnitude of the impedance will be minimum it
will only contain the real part.
1

Z= R + /

&

Z = R2

Getting started
Example Resonance circuit (cont.)

z This occur when:

1
L =
C

1
o =
LC

Getting started
Example Resonance circuit (cont.)

z Given R = 10

C = 0.1 uF L = 0.01 H,

What is the resonant frequency ? i.e. at what

frequency of the supply will the current maximum ?


I

R = 10

C = 100uF

+
V

L = 0.01 H

Getting started
Example Resonance circuit (cont.)

We know that the resonant frequency is


given by:

1
o =
LC

How can we use Matlab to calculate this ?


Calculator style
Script Mfiles (+ analysis)
Function Mfiles (+ analysis)

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 (

Therefore the resonant frequency is 1000 rad/s


At any time, you can always get help from
Matlab by typing help at the prompt in
command window

Getting started
Example Resonance circuit (cont.)

Alternatively we can assign a variable to our


answer:
EDU res_frequency = 1/(sqrt(0.01*100e-6))
res_frequency =
1000
EDU

Variables and commands are placed in Matlab Workspace


These variables and commands can be recalled from
workspace, how ?

Getting started
Example Resonance circuit (cont.)

Variables

Alternatively we can assign


variableare
to stored
our as
All a
variables
answer:
arrays
EDU res_frequency = 1/(sqrt(0.01*100e-6))
They are casesensitive
res_frequency =
1000
EDU

Their names can contain up to


31 characters

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

To list the variables, use who or whos

To recall commands, use arrow keys np


To edit commands use arrow keys m o

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

Other than o, we are also interested in knowing the


reactance of the capacitor and inductor.
The reactances are given by

1
Xc =
C

XL = L

Script Mfiles
Example Resonance circuit (cont.)

At Matlab prompt type in edit to invoke M-file editor


C=100e-6;

% 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);

Save the file as m1.m

Script Mfiles
Example Resonance circuit (cont.)

To run the M-file, type in the name of the file at


the prompt it will be executed provided that
the saved file is in the known path
Type in matlabpath to check the list of directories
listed in the path
Use path editor to add the path: File Set path

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

workspace through input and output variables.

INPUT

FUNCTION
Commands
Functions
Intermediate variables

OUTPUT

Function M-files
z Function is a black box that communicates with

workspace through input and output variables.

INPUT

FUNCTION
Commands
Functions
Intermediate variables

OUTPUT

Function Mfiles (cont)


z

Function for our example resonant circuit

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

Function Mfiles (cont)


z

Function for our example resonant circuit


We can now add our function to our script M-file

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

Arrays and Plot


z Still referring to our resonant

circuit
suppose we want to display the variation of
impedances with frequency we want to plot
the impedances versus the frequency.

z We need to represent and Z (or XC or XL) in

arrays and we will use the function plot to plot


versus Z, Xc and XL

Arrays (cont)

z Before we try to solve this problem let us look a

little closer on arrays

In order to plot a graph of B versus A, we need


to have a list of values of A (or an array of A) and
a list of values of B (or an array of B)
e.g
A

10

12

14

16

18

Arrays (cont)

z How do we construct arrays ?


EDU A = [1 2 3 4 5]
A=
1 2 3 4 5
EDU
EDU B = [4;6;8;12;14;16]

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)

z How do we construct arrays ?


EDU A = [1 2 3 4 5]
A=
1 2 3 4 5
EDU
EDU B = [4;6;8;12;14;16]
B=
10
12
14
16
18
EDU

A = [1 2 3 4 5]
10
12

B = 14

16
18

Arrays (cont)

z How do we construct arrays ?

If we want to construct an array of, say, 100


elements between 0 and 2 linspace
EDU c1 = linspace(0,(2*pi),100);
EDU whos
Name
c1

Size
1x100

Bytes Class
800 double array

Grand total is 100 elements using 800 bytes


EDU

Arrays (cont)

z How do we construct arrays ?

If we want to construct an array of, say, 100


elements between 0 and 2 colon notation
EDU c2 = (0:0.0201:2)*pi;
EDU whos
Name

Size

Bytes Class

c1

1x100

800 double array

c2

1x100

800 double array

Grand total is 200 elements using 1600 bytes


EDU

Arrays (cont)

z How do we access elements in an array ?

Arrays can be accessed in several ways.


a=linspace(0,40,21)

Try the following:


a(2)
a(1:3)
a(7:1:3)

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)

Gives the size of matrix a

EDU ones(4)

Generates 4 x4 matrix containing 1s

EDU eye(4)

Generates 4 x4 identity matrix

Arrays (cont)

Resonant circuit our problem is to plot the magnitude


of the impedances versus the frequency
We need to create arrays of and generate the
corresponding Z, Xc and XL arrays based on:
1

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)

It is also possible to study the resonant circuit using a


Bode plot using the function bode comes with control
system toolbox.
If we are interested in the relation between VR and V
we can obtain the Bode plot of the transfer function
VR/Vo as follows:
num = [(R/L) 0];
den = [1 R/L 1/(L*C)];
sys = tf(num,den); %
bode(sys);

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

Problem: We need to simulate the resonant circuit


and display the current waveform as we change the
frequency dynamically.
i
Varies Z
from 0 to
2000 rad/s

10

100 uF

+
v(t) = 5 sin t

0.01 H

Observe the current. What do we expect ?

The amplitude of the current waveform will become


maximum at resonant frequency, i.e. at Z = 1000 rad/s

Simulink

Start Simulink by typing simulink at Matlab prompt


Simulink library and untitled windows appear

It is where we
obtain the blocks to
construct our model

It is here where we
construct our model.

Simulink

How to model our resonant circuit ?


i

10

100 uF

+
v(t) = 5 sin t

0.01 H

Taking KVL around the loop,

di 1
idt
v = iR + L +
dt C

Simulink

Differentiate wrt time and re-arrange:


2

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

Thus the current can be obtained from the voltage:

s(1/ L)
I = V

R
1
s2 + s +
L
LC

s(1/ L)
1
2 R
s + s+
L
LC

Simulink

Constructing the model using Simulink:


Drag and drop block from the Simulink library
window to the untitled window
1
s+1
S ine Wa ve

Tra nsfe r Fcn

sim ou t
To Workspa ce

Simulink

Constructing the model using 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

Tra n sfe r Fcn


v
To Wo rksp a ce 1

i
To Wo rksp a ce

Simulink

We need to vary the frequency and observe the current


5
Am p litud e

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

Tra n sfe r Fcn1

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

The waveform can be displayed using scope similar


to the scope in the lab
5
Constant1

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

Das könnte Ihnen auch gefallen