Beruflich Dokumente
Kultur Dokumente
MATLAB/SIMULINK
129
MATLAB/SIMULINK
- For the simulation of dynamic systems
- SIMULINK is a toolbox extension of MATLAB
- Run MATLAB first to get to SIMULINK in cases when there are parameters
to be passed onto SIMULINK.
CREATING SIMULINK PROGRAM
The following equations (R-L circuit With ac excitation) will be used to illustrate how a
SIMULINK program is created:
Circuit:
vac
Inputs:
Outputs:
vac
i
Equations:
di 1
vac i.R
dt L
au = L/R
L, R, s, Vmag
Parameters:
Values:
R = 0.4
L = 0.04 H
Vmag = 0.076
s = 314 rad/s
au = L/R = 0.04 / 0.4 = 0.1 s
1
Gain
1/s
Mux
Integrator
1
s 1
x = Ax + Bu
y = Cx + Du
Transfer Function
State-Space
Multiplexer
Fig. 1 Some Linear SIMULINK Library Block Diagrams
130
- The dynamic system equations may consist of integral and algebraic equations.
Eliminate all potential algebraic loops.
- Separate the variables into independent and dependent variables
vac
i
R
Gain1
1/L
vac
Vac
Add
Gain
1/s
Integrator
Monitoring and Saving Data: There are several ways of monitoring and saving data
variables. Two will be discussed here.
SCOPE: Output of signal generated viewed during simulation using an oscilloscope.
- A single input block
- A clock module is used to monitor display of variables.
- Scope is left floating.
- The SCOPE block can also accept multiplexed inputs as shown in Fig. 3.
131
y1
y2
Mux
ym
Scope
Multiplexer
Fig. 3 Using SCOPE to Monitor Data
- The multiplexer template (obtained from the Connections library) is used two or
more signals into one scope.
- In some SIMULINK versions the scope display is limited to signals (multiplexed
into one signal)
- If the multiplexer input can accommodate more than the permitted number of scope
displays, use a Selector to select up to the number permitted and fed through the
scope.
SINKS: Storing the desired output as MATLAB data. Data files can be saved for plotting
or processing later on.
- Use a clock during the run-time.
- Use the To File template in the SIINKS library block.
- Data is store temporarily in an array yout in the MATLAB workspace using
To Workspace template given in the Sinks.
- The data file and the array yout associated with the To File and To Workspace
templates can be renamed in the SIMULINK window.
- Make sure there sufficient data space in To Workspace before starting the
Simulation (data may be overwritten if data space is insufficient).
- In some SIMULINK versions the parameter field of the To Workspace dialog box
have three parameters.
[n, m_step, data_interval]
n = buffer length
m_step = if m is the number of integrations, the data is saved at the end of every
mth integration steps
data_interval = sample time interval to collect data
132
SAVE: SIMULINK data can also be saved in the MATLAB workspace using the
MATLAB save command
- Generalized Command
>> save filename <list of variables><options>
MATLAB saves the data file in the current workspace to a file named filename.mat.
- Saving into array yout
>> save yout
- Save data as ASCII file
>> save filename yout -ascii
Clock
y1
y2
Mux
yout
ym
To Workspace
Multiplexer
Fig. 4 Saving Data in MATLAB Workspace
>> save filename x -ascii
- Save variables from a file
>> save filename var1 var2 var3
LOAD: Loads data previously saved in MATLAB or SIMULINK session using
MATLAB command
>> load yout
PRINT: SIMULINK data can also be printed in the MATLAB workspace using the
SIMULINK print command (usually a button) and MATLAB print command
- SIMULINK Generalized Command
>> print <-ddevicetype><options><sSIMULINK_figure><filename>
133
Adobe Illustrator
JPEG image
TIFF image
M-file with graphic handlers
Level 2 Postscript
Encapsulated Postscript (EPSF)
PLOT: SIMULINK data can also be plotted in the MATLAB workspace using the
SIMULINK plot command (usually a button) and MATLAB plot command
- Plots can be two-dimensional (2-D) or three-dimensional (3-D).
- Plots can be either variables versus time or variables versus variables.
2-D Plots: MATLAB generalized command
>> plot (<options><filename>)
134
yout(:,1), yout(:, 2), r = plot N data in column 2 of yout versus time with
red solid line
yout(:,1), yout(:, 3), -- = plot N data in column 3 of yout versus time with
dashed line
yout(:,1), yout(:, 4), b* = plot N data in column 4 of yout versus time with
blue asterisk line
yout(:,1), yout(:, 4), - = plot N data in column 5 of yout versus time with
solid
>> plot (yout(:, 2), yout(:, 3), linetype)
135
RUN/START SIMULINK
- Type and enter SIMULINK file name at the MATLAB prompt >>
>> file_simulink
OR click the SIMULINK file name from the window and drop the file name
at the MATLAB prompt >> in the MATLAB command window
OR open the SIMULINK window (with file) and click the run bottom
Complete simulation diagram for dynamic equations of the R-L circuit
Clock
Gain
block
flipped
vac
MUX
yout
To
Workspace
Scope
Gain1
Multiplexer
vac
1/L
1/s
Gain
Integrator
Add
Vac
Fig. 5 Complete SIMULINK Block Diagram Describing the R-L Circuit Model
CREATING MATLAB PROGRAM
- Create a script file (user-created file with a sequence of MATLAB commands) by
using any of the following:
1. PC, Mac, Unix, etc. text editor
Or 2. Select New M-file from the File menu in MATLAB. A new edit window will
pop up.
- All M-Files must be saved with the extension .m to their names (when MATLAB
editor file is used, the script file is automatically saved with the extension .m to
its name)
- Two important things
1. In previous versions on MATLAB all statements must end with semicolon;.
136
vac
i
137
Source Block Parameters: vac Double click on the block or click (highlight) block,
then click on block parameters in the edit menu and the Source Block Parameters: vac
window will pop open.
138
Function Block Parameters: Sum - Double click on the block or click (highlight) block,
then click on block parameters in the edit menu and the Function Block Parameters: Sum
window will pop open.
Function Block Parameters: Gain - Double click on the block or click (highlight) block,
then click on block parameters in the edit menu and the Function Block Parameters: Gain
window will pop open. Gain parameters may be defined in the MATLAB program or
actual value can be used in the pop window.
139
Function Block Parameters: Integrator - Double click on the block or click (highlight)
block, then click on block parameters in the edit menu and the Function Block Parameters:
Integrator window will pop open.
Source Block Parameters: Clock - Double click on the block or click (highlight) block,
then click on block parameters in the edit menu and the Source Block Parameters: Clock
window will pop open.
140
Function Block Parameters: Mux - Double click on the block or click (highlight) block,
then click on block parameters in the edit menu and the Function Block Parameters: Mux
window will pop open.
Sink Block Parameters: To Workspace - Double click on the block or click (highlight)
block, then click on block parameters in the edit menu and the Function Block Parameters:
Mux window will pop open.
141
Scope Parameters -
Double click on the block and the Scope window will pop.
Parameters
Button
In the Scope window click on the parameters button. The Scope parameters window
will pop open with the General tab highlighted. Enter Number of Axes.
142
Click to highlight the Data History tab in the Scope parameters window.
Right Click on Scope window and select Axes properties. The Scope properties:
axis 1 window will pop open. Set Title to vac, i
143
Simulation Parameters:
Parameters.
144
Tolerance:
Step Sizes:
145
MATLAB Program:
In this case studies MATLAB program containing the parameters for the SIMULINK run.
%Defining Parameters
% MATLAB Try Exercise for ECE 4300/5950
% M-file
% input parameters and initial conditions
R = 0.4;
L = 0.04;
ws = 314;
vmag = 100;
iL0 = 0;
tstop = 0.5;
% R = 0.4 ohm
% L = 0.04 henry
% excitation frequency in rad/sec
% ac voltage magnitude in Volts
% initial value of inductor current
% stop time for simulation
pause
% derived constants
tau = L/R;
% time constant
E1 = ws*tau;
theta = atan(E1);
zmag = sqrt(R^2 + (ws*L)^2);
A2 = vmag/zmag;
A1 = A2*sin(theta);
% computation
t = 0:0.001:tstop;
% t from 0 to 0.05
vac = vmag*sin(ws*t);
% input voltage vac
iac = A1*exp(-t/tau) + A2*sin(ws*t+theta); % current I
%disp('run simulation, type ''return'' when ready to return')
%keyboard
% plot
xline = zeros(1, length(t));
subplot(221)
plot(t, vac, t, xline), grid
title('ac excitation voltage')
ylabel('Vac in V')
subplot(223)
plot(t, iac, t, xline), grid
title('mesh current')
xlabel('time in sec.')
ylabel('i in A')
% generate a null
146
2.
3.
Since there are parameters to be passed onto SIMULINK, MATLAB is run first to
get to SIMULINK. In the MATLAB program the first pause indicates that the
program stops after the parameters are made available to SIMULINK.
Run SIMULINK. The scope plots are shown as SIMULINK is running.
Hit any key to run the plots in MATLAB file: (i) all three plots and (ii) individual
plots after each pause is executed.
RESULTS:
(a) SIMULINK Scope Plots
Start simulation
button
147
(b)
MATLAB Plots
148
149
Inputs
Outputs
Equations
vac
i
CIRCUIT DIAGRAM
vac
di 1
vac i.R
dt L
*
Parameters L, R, s, Vmag
Values
R = 0.4
L = 0.04
Vmag = 100
s = 314
au = L/R
150
di 1
vac i.R
dt L
- Solution
Vmag
i (t )
where
*
sin( )e
2
R 2 s L
Vmag
tan
R s L
s L
R
sin(s t )
and = L/R
theta tan
- time constant:
= tau = L/R
st
- 1 Term of i(t):
- 2nd Term of i(t):
Vmag
R s L
Vmag
2
R s L
2
s L
R
sin( )
151
% R = 0.4 ohm
% L = 0.04 henry
% excitation frequency in rad/sec
% ac voltage magnitude in Volts
% initial value of inductor current
% stop time for simulation
152
153
% R = 0.4 ohm
% L = 0.04 henry
% excitation frequency in rad/sec
% ac voltage magnitude in Volts
% initial value of inductor current
% stop time for simulation
154
di 1
Circuit Differential Equation:
vac i.R
dt L
1
Circuit Integral Equation: i vac i.R dt
L
1 Summation blocks
1 Integral block
2 Gain blocks
Library Browser
155
1
vac i.R dt
L
Get the Gain (1), Integrator (1), and Summation
(1) Blocks
Gain: (Simulink>Math>Gain)
Integrator: (Simulink>Continuous>Integrator)
Summation:(Simulink>Math Operations>Add)
Sine Wave:(Simulink>Source>Sine Wave)
Flip block:(Format>Flip block)
R-L Circuit Equation
Gain
block
flipped
i
vac
156
i
vac
AC Excitation Values
Double-click on
block and enter
initial con
157
Get Mux:
(Simulink>Signal Routing>Mux)
Get Clock:
(Simulink>Sources>Clock)
Connect clock
vac
i
vac
158
Get Scope:
(Simulink>Sinks>Scope)
Parameters
159
vac
i
Using Scope
Connect To Workspace
160
Simulation Configuration
Simulation Parameters:
(Simulation>Configuration Parameters)
Solver
:Set Type to variable-step, Solver to ode45
Max step size : 0.01(sec)
Start time
: 0.0 (sec);
Stop time
: 0.5 (sec)
161
Simulating the RL Circuit Model
*
*
Save Model
Save file: (File>Save as)- rl_circuit_model
Start Simulation
Start simulation
Results
SIMULINK Plot
162
Results
MATLAB Plot