You are on page 1of 8

ECE-P 352

Experiment 2: Simulation
Introduction to Matlab/Simulink
Modified 01-12-05

Introduction to Matlab/Simulink
Objectives: We have two main objectives in this experiment
Introduce Matlab/Simulink
Simulate three electrical circuits

Information on Matlab/Simulink:
1. General Information
SIMULINK is a toolbox extension of the Matlab program. It is a program for simulating dynamic systems. Briefly,
the steps of using SIMULINK involve first defining a model or mathematical representation and the parameters of
your system, picking a suitable integration method, and setting up the run conditions, such as run time and initial
conditions. In SIMULINK model definition is facilitated by the graphical interface and the library of templates or
function blocks that are commonly used in mathematical descriptions of dynamic systems. As mentioned before,
SIMULINK has block libraries shown in Fig. 1: Continuous, Discrete, Functions &Tables, Math, Nonlinear,
Signal & Systems, Sources, Sinks, Discrete, Linear, Nonlinear and Connections. To examine the properties of
SIMULINK, please follow the given steps below:
Start Matlab (Start/matlab): The Matlab program starts with a display indicating the version of Matlab
program after which it will display the Matlab prompt >> in a Matlab command window.
Type simulink after the Matlab prompt >> in the Matlab command window. You will see the block
libraries as shown in Fig.1 for MATLAB 6.0. Spend time exploring the components in the libraries.

Figure 1: SIMULINK Block Library

C.N./S.A./S.C

Figure 2: Contents of the Continuous Block Library

Jan. 05, 2004

ECE-P 352
Experiment 2: Simulation
Introduction to Matlab/Simulink
Modified 01-12-05

Double clicking on a menu heading will open up a display of menu commands under that heading. For example, if
you want to create a new SIMULINK model, click the create a new model box in Fig.1 to produce a blank
SIMULINK model screen onto which you can then select and drag components from the block library to assemble
your SIMULINK simulation. Note that the screen will initially be named Untitled. You must give it a name using
the Save As menu command under the File menu heading. The assigned filename is automatically appended with
an .mdl extension.
A variety of function blocks or templates are grouped under different library blocks. A template can be copied
from a library block onto the SIMULINK model screen by first selecting the template and then dragging it to the
desired location in the SIMULINK model screen, or by a sequence of Copy and Paste commands, which are found
under the Edit menu. The combined actions of selecting and dragging are carried out by clicking on the desired
template with left-most button, keeping that button depressed, and moving the pointer to the desired location on the
SIMULINK model screen.
To open the templates of each library block, please double-click the block. This will display all the templates
contained in that block. For example, if you need derivatives and integrators, you first open up the Continuous
library block by double clicking on the icon of the continuous block to display its contents. Figure 2 shows the
contents of the continuous library block, in which you will find the derivative, integrator, and many more
components.
Many templates have internal parameters which you must specify before you use these templates in a simulation.
To view or set up these parameters, you must double-click on the template, upon which a dialog window will appear
with boxes for parameter values to be inserted.

2. How to Choose an Integration Method


Before you start any simulation, you will need to choose an integration method and specify some run conditions.
Under the menu heading Simulation is a sub-menu Parameters, from which you can select one of several
integration methods and enter the values of simulation parameters, such as tolerance and minimum and maximum
step size. This version of the SIMULINK has five ODE solvers : ode45, ode23, ode113, ode15s, ode23t and
ode23tb. You can find much more detailed information on these integration methods in any numerical analysis book.

3. How to Run a Simulation


Other than the parameters for the integration method, you will also need to specify the start and stop time of the run
before you can start your simulation. These are also set in the Parameters menu. The simulation is then be started
by selecting Start under the main menu heading Simulation.

Exercises
Exercise 1: Variable Frequency Oscillator
In this exercise, you will go through the steps to construct a SIMULINK simulation to generate an orthogonal set of
sine and cosine time functions of fixed amplitude but adjustable frequency beginning with the following equation of
an oscillator:

d 2 y1
= 2 y1
2
dt
where

is the angular frequency.

C.N./S.A./S.C

Jan. 05, 2004

ECE-P 352
Experiment 2: Simulation
Introduction to Matlab/Simulink
Modified 01-12-05

To implement the oscilltor equation, we will first convert the second-order differential equation into two first-order
differential equations and then express them in integral form: Lets introduce a new state as:
y2 = ( 1 )

dy1

dt

and we obtain

dy2
= y1
dt

Rewriting the above equation and then defining y2 in integral form, we have:
y2 = y1dt and y1 = y2 dt

To simulate the variable frequency oscilltor, draw the block diagram shown in Figure 3. While building the diagram
answer the following questions:
1) Why does the constant block, , require a value of 377? What are the units associated with this number?
2) What is the function of the simout block?
3) The black vertical bar in the diagram below is a Mux. The Mux is located in the signal routing library.
What is the function of the Mux?

Figure 3: The Block Diagram of the Variable-Frequency Oscillator


Before you run the simulation, perform the following steps:
Set the initial condition of the two integrators by double clicking each integrator block:
Integrator: External reset-none, initial condition source-internal, initial condition= 5
Integrator 1: External reset-none, initial condition source-internal, initial condition= 0
Set the save format of the simout block as array. This block is located in the Sinks library.
Set the simulation parameters-Simulation/Parameters as:
Start time = 0.0, stop time = 0.05, solver options: Type =variable-step ode45, max step size=auto, initial

C.N./S.A./S.C

Jan. 05, 2004

ECE-P 352
Experiment 2: Simulation
Introduction to Matlab/Simulink
Modified 01-12-05

step size = auto, relative tolerance =1e-5, absolute tolerance =1e-6, output options = refine output and Refine factor
is equal to one (1).
Run simulation-Simulation/Start
Double-click scope to see the plots.

Once you finish the simulation, input the following Matlab code in the Matlab command window to get the plot and
label the axis:

plot(tout,simout(:,1),tout,simout(:,2))
xlabel('time in sec')
ylabel('y1 and y2')

You should obtain the following plot. Save your plots

Figure 4: Simulation Results for Variable Frequency Oscillator

Exercise 2: RLC Circuit


The second exercise is to simulate the dynamic response of a parallel RLC circuit as shown in Figure 5. The
SIMULINK model of a Parallel RLC circuit is already designed for you. The objective is to simulate the dynamic
response of a parallel circuit. Figure 6 shows the SIMULINK diagram of the RLC circuit. To run the simulation,
follow the steps given below:
Set your Matlab path to the folder C2 on the desktop :
C:\Documents and Settings\student\Desktop \C2

C.N./S.A./S.C

Jan. 05, 2004

ECE-P 352
Experiment 2: Simulation
Introduction to Matlab/Simulink
Modified 01-12-05

If this folder is not located on the desktop have the TA retrieve it for you from:
Network Neighborhood\ Entire Network\Microsoft Windows Network\Ece\Ece dept server\course
ware\CEPE files\Simulink\C2
Open the simulation file called S2 by typing S2 in the Matlab command window
Check the connections of the various blocks in S2 to confirm that the simulation agrees with the mathematical
model of the circuit that will be provided during the experiment.
The simulation parameters and initial conditions are already set up using the box initialize m2SIM. Double-click
on this box. This box calls an M-file, M2init.m. Open the M-File M2 and record the simulation and circuit
parameters.
Run simulation-Simulation/Start
Plot the graph by double clicking Plot results box. This will give the plot of source and inductor currents and
voltage across the capacitor, as shown in Figure 7.
In your report, supply the differential equations required to solve the circuit in Figure 5.

iL

is
ic

Rs
Vs

Vc

Figure 5: RLC Circuit

C.N./S.A./S.C

Jan. 05, 2004

ECE-P 352
Experiment 2: Simulation
Introduction to Matlab/Simulink
Modified 01-12-05

Figure 6: Simulink Diagram of RLC Circuit

Figure 7: Simulation Result for RLC Circuit

C.N./S.A./S.C

Jan. 05, 2004

ECE-P 352
Experiment 2: Simulation
Introduction to Matlab/Simulink
Modified 01-12-05

Exercise 3: RL Circuit with an AC Voltage Source


In this exercise, we will examine the transient response of a simple RL circuit to an ac voltage energization, Fig. 8.
In particular, we will look at the dc offset in the response current from energizing at different points of the ac
voltage wave. The Simulink diagram is given in Fig. 9 for the integral equation:

i( t ) =

1t
( Vac iR )dt + i( 0 )
L0
R
i
L

Vac

Figure 8: RL Circuit with AC Excitation

Figure 9: Simulink Diagram for the RL Circuit

To simulate the RL circuit follow the steps below:

C.N./S.A./S.C

Jan. 05, 2004

ECE-P 352
Experiment 2: Simulation
Introduction to Matlab/Simulink
Modified 01-12-05

Open the Simulink file S3.


Open the M-file M3 to find out the circuit and simulation parameters. Record these parameters.
Double-click on Vac in S3 to see its voltage magnitude, phase angle () and frequency
Simulation parameters are already set up for you.
Double click on the box called, Initialize m3SIM to load the parameters into MATLABs workspace
Run the simulation.
A typical simulation result is shown in Figure 10.

Repeat the simulation for two different values of ac voltage phase angle,
(use function atan( ) ) where

e = 314 .

and

= tan -1

e L
R

What are the values of these two phase shifts in degrees?

Compare the simulation results for the current.


ac excitation voltage
100

Vac in V

50
0
-50
-100

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

0.5

0.35

0.4

0.45

0.5

mesh current
15

i in A

10
5
0
-5
-10
0

0.05

0.1

0.15

0.2
0.25
0.3
time in sec.

Figure 10: Current and Voltage Waveforms for the RL circuit,

C.N./S.A./S.C

=0

Jan. 05, 2004

You might also like