Sie sind auf Seite 1von 35

128

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

Mathematical description of the dynamic system is represented by block diagrams.


Examples of SIMULINK block diagrams are shown in Fig. 1.

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

i, vac, and s and are dependent variables


L, R, and au are independent variables
- Rewrite the integral equations so that the independent state variables are expressed
as some integral of sum of independent variables and dependent variables, including
the integral equations.
Convert oscillator equation from nth-order differential equation into n
first-order differential equations. All equations are in first-order form
- Arrange the mathematic description of the dynamic system using SIMULINK library
Blocks; R-L circuit model equations.
Gain
block
flipped

vac
i

R
Gain1
1/L

vac
Vac

Add

Gain

1/s
Integrator

Fig. 2 SIMULINK Block Diagram Describing the R-L Circuit Model

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

sSIMULINK_figure is SIMULINK screen


- MATLAB Generalized Command
>> print <-ddevicetype><options><fMATLAB_figure><filename>
>> print <-Pprinter_id><options><fMATLAB_figure><filename>
fMATLAB_figure> is MATLAB figure
-Pprinter_id is MATLAB printer id
- Some device types
-dill
-djpeg
-dtiff
-dmfile
-dps2
-deps

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

options = many different format of output are sent to a file or printer


e.g. dtiff
TIFF image file
filename = command plots to copy a current figure to a specified file. If
no file is included, the system printer becomes the default target.
- Simple 2-D plot is
>> plot (xvalues, yvalues, style-option)

xvalues = contains x-coordinates points


yvalues = contains y-coordinates points
style-option = specifies line color, (e.g. r red, y yellow, etc), line style

134

(e.g. solid, dashed, etc), and line point-marker style (e.g. o,


+, *, etc).
- Sometimes the variables are stored in the array yout (with time in 1st column,
and data in the other columns) of length N.
>> plot (yout(:,1), yout(:, 2), r, yout(:,1), yout(:, 3), --,
yout(:,1), yout(:, 4), b*, yout(:,1), yout(:, 5), -)

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)

yout(:,2), yout(:, 3), - = plot N data in column 2 of yout on x-axis and


column 3 of yout on y-axis
linetype = option such as line type, color, etc.
3-D Plots: MATLAB generalized command
>> plot3 (xvalues, yvalues, zvalues, style-option)

xvalues = contains x-coordinates points


yvalues = contains y-coordinates points
zvalues = contains z-coordinates points
style-option = specifies line color, (e.g. r red, y yellow, etc), line style
(e.g. solid, dashed, etc), and line point-marker style (e.g. o,
+, *, etc).
- Plotting variables stored in the array yout.
>> plot (yout(:,1), yout(:, 2), yout(:, 3), -)
>> plot (yout(:,2), yout(:, 3), yout(:, 4), r*)

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

2. Lines starting with % sign are comment lines.


RUN/START MATLAB
- Type and enter MATLAB file name at the MATLAB prompt >> in the MATLAB
command window.
>> file_matlab
OR click the SIMULINK file name from the file window and drop the file
name at the MATLAB prompt >> in the MATLAB command window
OR open the MATLAB editor (with file) and click the run bottom.
The plot of dynamic equation is generated by the following MATLAB plotting commands
in a MATLAB file.
>> plot (yout(:,1), yout(:, 2), -, yout(:,1), yout(:, 3), -.);
>> xlabel (time in sec);
>> ylabel (y1 and y2);
- Run MATLAB first (to get to SIMULINK)
- Run SIMULINK next
Case Study
SIMULINK Model Diagram:
Create this SIMULANT model diagram and save as simulink_name.mdl file (with .mdl
extension).

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.

Click on Simulation button then click on Configuration

***NOTE***: The following may (depending on integration method


selected) have to be specified in the simulation parameters to control the
integration:

144

Tolerance:

Step Sizes:

This is the limit of tolerance between integration steps. For


smaller tolerance, the integration will take smaller steps
thereby increasing the run time. The error tolerance range
between successive integration steps may be 10-3 to 10-6
(1e-3 to 1e-6). If not sure, try different values by targeting
accuracy and run-time for compromise.
try different values by targeting accuracy and run-time for
compromise.
Minimum Step Size For start/restart and after
discontinuity. Not affected if variable-step method
(example Adams) is used.
Maximum Step Size limits the step length. Affects the
smoothness of the output.

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

% 2nd term of i(t)


% 1st term of i(t)

% 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

This file is saved as rl_circuit.m (with .m extension).


1.

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

INTEGRATION ALGORITHMS IN SIMULINK


There are several integration methods in SIMULINK. The right integration method must
be selected and the appropriate run conditions are then modified; example tolerance,
maximum and minimum step size, etc.
Some Routines
- linsim:
- rk23:
- rk45:
- gear:
- adams:
- euler:

Used to solve linear dynamic equations; computes derivative at each output.


Runge-Kutta 3rdorder method for nonlinear and discontinuities that takes 3
internal steps [0, , 1] between output points.
Runge-Kutta 5thorder method for nonlinear and discontinuities that takes 6
internal steps between output points.
Predictor-corrector for numerically stiff systems. Takes variable steps and
may not work well with inputs that change quickly.
Predictor-corrector that uses variable number of steps between output points
One-step that calculates derivative and output ate each output point.

Some ODE Solvers


- ode45 (Dormand-Prince (4, 5)): One step Runge-Kutta.
- ode23 (Bogacki-Shampine (2, 3): One step Runge-Kutta for wide tolerances.
- ode113 (Adams):
Multi-step for time-consuming and tight
T\tolerances.
- ode15s (Stiff/NDF):
Multi-step, variable-order solver based on
Backward differentiation
- ode23s (Stiff/Mod. Rosenbrock); One-step solver based on order 2. A-stable.
- ode23t (Mod. Stiff/Trapezoidal): One-step solver based on trapezoidal rule
- ode23tb (Stiff/TR-BDF2):

149

R-L Circuit With AC Excitation


*
*
*

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

Two Solution Alternative Procedures


- MATLAB SCRIPT ONLY: Solves equation for i(t)
- COMBINES MATLAB AND SIMULINK:
Solves a integral equation

150

Solution to the RL Circuit Differential Equation

Solution of Differential Equation:

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

Calculations in Equation i(t) Independent of Time


- load angle:

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

New MATLAB Script File


*

MATLAB Script File


Open a New File
(File > New > M File)

Type Program in pop-up window


- Text File
% 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

152

Continue New MATLAB Script File


Continue to type program in pop-up window
- Text File
% derived constants
tau = L/R;
% time constant
E1 = ws*tau;
theta = atan(E1);
zmag = sqrt(R^2 + (ws*L)^2);
A2 = Vmag/zmag;
% 2nd term of i(t)
A1 = A2*sin(theta);
% 1st term of i(t)
% computation
t = 0:0.0001:tstop;
% t from 0 to 0.5
vac = Vmag*sin(ws*t);
% input voltage vac
iac = A1*exp(-t/tau) + A2*sin(ws*t+theta); % current I
% plot
xline = zeros(1, length(t));
% generate a null
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')

Save file with name


rl_circuit
(File > Save As)

153

Parameters in New MATLAB Script File


*

Defining the Parameters in MATLAB Script File


Open a New File
(File > New > M File)

Type Parameters in pop-up window


- Text File
% 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

Save file with name


rl_circuit_parameters
(File > Save As)

154

Integral Equations and Blocks


*
*

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

Starting a Simulink New Model


*

SIMULINK Script File


Open a new file: (File > New > Model)
Open Library Browser: (View > Library Browser)

Library Browser

155

Connecting and Naming


*
*

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

Connect and Name


For connecting blocks move mouse from source to
destination while holding the left-button down
Name connections by double clicking on them
Name blocks by double clicking on the names
Resize blocks similar to resizing a window
Flip block: Select block, right click, (Format>Flop
block)

156

The Integrator Block


*

Integrator Initial Conditions


Double-click on block and enter initial conditions

i
vac

AC Excitation Values
Double-click on
block and enter
initial con

157

Outputs Through a Mux (Multiplexer)

Get Mux:
(Simulink>Signal Routing>Mux)

Connect voltage vac and


current i

Get Clock:

(Simulink>Sources>Clock)
Connect clock

Drag this icon into model to insert Mux

vac
i

vac

The Mux Block


*

Double click to open mux


Set Number of inputs: 3
Set Display option: none

158

The Scope Block


*

Get Scope:
(Simulink>Sinks>Scope)

Double click to open scope

Drag this icon into model to insert Scope

Click Parameters icon:


Set Number of axes: 1
Data History tab:
Check Limit data points to last


Parameters

The To Workspace of MATLAB

Get To Workspace: (Simulink>Sinks>To Workspace)


Double click to open workspace
Name Variable name: y; Limit data points to last: 3000

Add plot routine to MATLAB Script File


Open the file rl_circuit_parameters: Plot routine
R = 0.4; L = 0.04; ws = 314; Vmag = 100;
iLo = 0; tstop = 0.5;
subplot(2,1,1), plot(y(:,1),y(:,2)), grid
title('ac excitation voltage'), ylabel('Vac in V')
subplot(2,1,2), plot(y(:,1),y(:,3)), grid
title('mesh current')
xlabel('time in sec.')
ylabel('i in A')

159

Complete RL Circuit Model

vac
i

Using Scope

Connect To Workspace

Connect voltage vac and


current i through
multiplexer to scope
Name plot: Right click on
plot, select Axes properties
Scope properties:
Set Title: vac and i

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

Simulating the RL Circuit Model

Results

MATLAB Plot

Das könnte Ihnen auch gefallen