Vous êtes sur la page 1sur 15

SIMULINK MINIGUIDE

Simulation of Nonlinear Dynamic Systems Using S-Function Blocks

by
Prof. Oscar D. Crisalle
Chemical Engineering
University of Florida
crisalle@che.ufl.edu

Revision 06 - March 4, 2010

Contents
1
2
3
4
5
6
7
7.1
7.2
7.3
7.4

Nonlinear Dynamic System Used as Illustration . . . . . . . . . .


MATLAB S-Function Describing the Nonlinear Model . . . . . .
SIMULINK S-Function Block that Calls a MATLAB S-Function
Running a Nonlinear Simulation Using the SIMULINK Interface
Running a Nonlinear Simulation Using the MATLAB Interface .
Results Produced by the Simulation Software . . . . . . . . . . .
Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Driver File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Printing to the Screen . . . . . . . . . . . . . . . . . . . . . . . .
Plotting Results . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

1
2
4
5
6
7
9
9
11
12
14

Nonlinear Dynamic System Used as Illustration

The following nonlinear dynamic system, consisting of state and output equations, along with a set of initial
conditions, is used to illustrate how to carry out a simulation using S-functions with SIMULINK. The model
is simply denoted Nonlinear System and referred to by the acronym NLS.
State Equations
x 1

cos x2 (t) + u(t)

x 2

sin x1 (t) u(t)2

Output Equations
y1 (t)

q
x21 (t) + x22 (t)

y2 (t)

10 + e0.2t x1 (t)

MATLAB S-FUNCTION DESCRIBING THE NONLINEAR MODEL

Initial Conditions
x1 (0)

x2 (0)

= 25

where the model parameters are constrained to lie in the ranges

[1, 100]

[1, 10]

and the input signal is the step function


(
0
u(t) =
M

t < ts
ts 0

of height M , where
0.1 M 25
A dynamic simulation study consists of solving the differential equations that define the state equations
and finding the value of the output variables as a function of time t. In turn the time variable ranges from
an initial value t = 0 to a final instant t = tf , where tf = 20.

MATLAB S-Function Describing the Nonlinear Model

An S-Function is a MATLAB M-function that has a special structure with sections that are executed depending on the value of a variable called flag. The function named NLS_Dynamics.m shown below is an
S-Function appropriately configured to represent the nonlinear system under consideration.
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%

NLS_Dynamics .m
This f i l e has t h e s t r u c t u r e o f an Sf u n c t i o n .
used i n a SIMULINK SBlock t o s i m u l a t e t h e
dynamics o f t h e f o l l o w i n g n o n l i n e a r system :

It is

S t a t e E q u a t i o n s
xdot ( 1 )
xdot ( 2 )

=
=

alpha cos ( x (2) ) + u


b e t a s i n ( x ( 1 ) ) u^2

Output e q u a t i o n s
y(1)
=
y(2)
=
I n i t i a l Conditions
xInitial =

s q r t ( x (1)^2 + x (2)^2 )
10 + exp ( 0.2 t ) x ( 1 )
[ x0_1 ; x0_2 ]

This SFunction i s u t i l i z e d i n t h e SIMULINK f i l e


named NLS_Sim . mdl

% Written by P r o f . Oscar D. C r i s a l l e , U n i v e r s i t y o f F l o r i d a
f u n c t i o n [ sys , x0 ] = NLS_Dynamics ( t , x , u , f l a g , p )
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% CALL WITH f l a g = 0
%= = = = = = = = = = = = = = = = = = = = = = = = = = =

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

MATLAB S-FUNCTION DESCRIBING THE NONLINEAR MODEL

i f f l a g == 0 % Return s i z e s and i n i t i a l s t a t e v e c t o r
size_cont_states
size_disc_states
size_outputs
size_inputs
size_disc_roots
size_feedthrough

=
=
=
=
=
=

2
0
2
1
0
0

;
;
;
;
;
;

sys = [ size_cont_states , size_disc_states , size_outputs , . . .


size_inputs , size_disc_roots , size_feedthrough ] ;
% Define i n i t i a l conditions
x0_1
x0_2

=
=

x0

p(3)
p(4)

;
;

[ x0_1 ; x0_2 ] ;

%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% CALL WITH f l a g = 1
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
e l s e i f abs ( f l a g ) == 1 % Return v e c t o r o f s t a t e d e r i v a t i v e
% Define parameters
alpha
beta

=
=

p(1)
p(2)

;
;

% D e f i n e t h e r i g h t hand s i d e o f each s t a t e e q u a t i o n
x1dot
x2dot
sys

=
=

alpha cos ( x ( 2 ) ) + u
b e t a s i n ( x ( 1 ) ) u^2 ;
=

[ x1dot ; x2dot ]

%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% CALL WITH f l a g = 3
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
elseif flag

== 3

y1
y2

=
=

sys

% Return output v e c t o r
s q r t ( x (1)^2 + x (2)^2 )
;
10 + exp ( 0.2 t ) x ( 1 )

[ y1 ; y2 ]

%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% CALL WITH o t h e r f l a g v a l u e s
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

SIMULINK S-FUNCTION BLOCK THAT CALLS A MATLAB S-FUNCTION

e l s e %a l l o t h e r f l a g s a r e i r r e l e v a n t t o t h i s problem
sys

[]

end
%
% End o f SFunction NLS_Dynamics .m.
Note that MATLAB makes available a number of S-Function demos taht can be called using the command
>> sfundemos

SIMULINK S-Function Block that Calls a MATLAB S-Function

The file NLS_Sim.mdl, shown in Figure 1, is created using icons available in the SIMULINK library. Of
particular relevance to this example is the icon entitled S-Function (shown in the figure as the block that
receives an input from the Step signal block). The S-Function block allows SIMULINK to call the S-function
entitled NLS_Dynamics.m.

t_sim

u_sim
u To Workspace

Clock

t To Workspace
y1_sim
y1 To Workspace

NLS_Dynamics
Step

S!Function

y2_sim
Demux

2
u

y2 To Workspace1

1
y

Fig. 1: SIMULINK file entitled NLS_Sim.mdl used for simulating the dyamics of a nonlinear system whose dynamics are described in the S-Function entited NLS_Dynamics.mdl. The S-Function block establishes
a connection between the S-Function NLS_Dynamics.mdl and SIMULINK.
To create a new S-Function block in SIMULINK, follow these steps:
1. Type >> simulink in the MATLAB command window. This opens the SIMULINK Library.
2. Select New from the SIMULINK menu. This creates a new SIMULINK file with no blocks in it.
3. Click on the User-Defined Functions library
4. Select and drag to the desired location the icon labeled S-Function. This is an S-Function block.

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

RUNNING A NONLINEAR SIMULATION USING THE SIMULINK INTERFACE

Clicking twice on the S-Function block reveals the dialog window shown below, with fields that relate to the
MATLAB S-Function. The Subsystem function name field is filled in with the name of the S-Function to
be called, and the Function parameters field is filled in with the parameters (in this case a vector p) that
appears as the last argument in the S-Fuction argument list.

Fig. 2: Dialog window obtained when double-clicking on the S-Fuction block of the SIMULINK file entitled
NLS_Sim.mdl shown in Figure 1.
Double-clicking on the Step block shown in 1 opens up a dialog box where it is possible to define the
paramteres of the step function, as shown in Figure .

Running a Nonlinear Simulation Using the SIMULINK Interface

To run the simulation file entitled NLS_sim.mdl using the SIMULINK interface, consider the following steps:
1. Type >> simulink in the MATLAB command window.
2. To open the SIMULINK file NLS_Sim.mdl select Open in the File menu.
Note: Make sure that you select Open or New after clicking with the mouse inside the SIMULINK
window, because in some versions of the sofware SIMULINK files ending with the extension .mdl
cannot be opened using the Open entry in the menu of the MATLAB command window.
3. To run the SIMULINK simulation
(a) Use the MATLAB command window to assign numerical values for the parameters t_s and M of
the Step of the block of the NLS_Sim.mdl file (for example, type >> t_s = 2, M = 3),
(b) Use the MATLAB command window to assign numerical values to the elements of the vector p
used the NLS_Dynamics.m S-Function (for example, type >> alpha = 3, beta = 10, x0_1 = 1,
x0_2 = -25 = 3, p = [alpha, beta, x0_1, x0_2]).
(c) Select Simulation . Configuration Parameters using the menu bar. Specify a Stop time (duration of the simulation), and select a Solver, i.e., a numerical algorithm used to solve the state
equations (for example ODE45 (Dormand Price), ODE113 (Adams), etc.), as well as the the maximum and minimum step sizes for the solver algorithm, as well as the tolerances used as a criterion
for convergence.
(d) Select Simulation . Start from the menu bar.
c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

RUNNING A NONLINEAR SIMULATION USING THE MATLAB INTERFACE

Fig. 3: Dialog box used for configuring the Step block of Figure 1 .

Running a Nonlinear Simulation Using the MATLAB Interface

To run the simulation file entitled NLS_sim.mdl using the MATLAB interface it is necessary to issue the sim
command in the command window, as follows,
>> sim( NLS_Sim)
which calls the SIMULINK program to run the NLS_Sim.mdl file, leading to identical results as indicated
in Section 4. By default, the syntax given above returns a time vector t and a two-column state matrix x
whose first column containts the values of x1 (t) and with a second column that contains the value of x2 (t).
A syntax that is preferred because it returns outputs and other signals such as the inputs in addition to
the default time and state vectors, is given is given by
>> [t,x, y, u] = sim( NLS_Sim)
The syntax given above returns the default time vector t and a two-column state matrix x, but in addition
returns the values of the variable connected to Port 1 in Figure 1 (namely two-column output matrix y
whose first column contains the values of y1 (t) and with a second column that contains the value of y2 (t)),
and values of the variable connected to Port 2 in Figure 1 (namely the one-column vector u whose column
contains the values of u(t)). For more input and output options, see the MATLAB manual entry for the sim
command.
Finally, it is noted that it is particularly convenient to run the nonlinear simulation from the MATLAB
command window using a suite of Matlab M-files that are executed sequencially by a main-program M-file
called driver. As an illustration, the simulation of the nonlinear system used in this miniguide can be carried
out by the sequential execution of the following files:

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

RESULTS PRODUCED BY THE SIMULATION SOFTWARE

MATLAB M-file that defines the values of the


dynamic system parameters and , as well as
SIMULINK parameters such as the final time for
simulation and the differential-equation
integration method specified.
MATLAB M-file that calls a number of other
files that display the inputs to the screen, run the
SIMULINK model file, and generate plots of the
results.

NLS_Input.m

NLS_Driver.m

The NLS_Driver.m M-file indicated above calls the following M-files in the order given below:

NLS_Display2Screen.m

MATLAB M-file that prints on the screen the


values of the dynamic system parameters and
as well as the SIMULINK parameters specified
in input file NLS_Input.m.

[t,x,y,u] = sim(NLS_Dynamics)

MATLAB command that executes the


SIMULINK file NLS_Dynamics.mdl. Note that
the first two output parameters (namely t and x,
are always available, while the next two
parameters (y and u) are respectively those
produced by ouput ports 1 and 2 of the
NLS_Dynamics.mdl file.

NLS_PlotResults.m

MATLAB M-file that generates plots of the


outputs of the simulation as a function of time.

The use of the SIMULINK command sim from a MATLAB M-file or from the MATLAB command
window to execute a SIMULINK model file is an instance of Programmatic Simulation. Listings of the
MATLAB code for all the M-Files discussed in this section are given in the Appendix.

Results Produced by the Simulation Software

The NLS_Input.m file is configured to run a simulation with the following input values:

and with the initial conditions


x=

1
25

After executing the MATLAB command >>NLS_Input, the following text is displayed in the screen:

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

RESULTS PRODUCED BY THE SIMULATION SOFTWARE

======================= NONLINEAR SYSTEM SIMULATION =====================


========================== I n i t i a l Parameters ===========================
Parameter a l p h a
= 3
Parameter b e t a
= 1
Step h e i g h t M
= 3
======================== S i m u l a t i o n Parameters ==========================
F i n a l time f o r t h e s i m u l a t i o n

= 2 sec

S o l v e r method t o advance time ( S o l v e r )


R e l a t i v e e r r o r t o l e r a n c e ( RelTol
A b s o l u t e e r r o r t o l e r a n c e ( AbsTol )
Output r e f i n e m e n t f a c t o r ( R e f i n e )
Upper bound on t h e s t e p s i z e ( MaxStep )
Suggested i n i t i a l step s i z e ( I n i t i a l S t e p )
Fixed s t e p s i z e ( F i x e d s t e p )
Determine output p o i n t s ( OutputPoints )
S e t output v a r i a b l e s ( O u t p u t V a r i a b l e s )
Decimation f o r output v a r i a b l e s ( Decimation )
Where t o e v a l u a t e e x p r e s s i o n s ( SrcWorkspace )
Where t o a s s i g n v a r i a b l e s ( DstWorkspace )
Location of zero c r o s s i n g s ( ZeroCross )

=
=
=
=
=
=
=
=
=
=
=
=
=

ode45
0.001
1 e 06
1
auto
auto
auto
all
txy
1
base
current
off

The screen report shown above indicates that the simulation was run with the parameter values = 3
and = 1, and using as input a step function with height M = 3. Then the simulation proceeds to open up
a graphic screen showing the plot given in Figure 4.

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

APPENDIX

= 3, = 1
y1(t) , y2(t)

60
y1
40

y2

20
0

10

12

14

16

18

20

10

12

14

16

18

20

10
t

12

14

16

18

20

x1(t) , x2(t)

60
x1

40

x2

20
0
20
0

u(t)

3
2
1
0
0

Fig. 4: Plots obtained when the SIMULINK file entitled NLS_Sim.mdl is run using the parameters and initial
conditions discussed in this section,

Appendix

This Appendix contains listings of the MATLAB M-files described in Section 5.

7.1

Defining Inputs

The MATLAB M-file NLS_Input.m defines the values of the dynamic system parameters and , as well as
SIMULINK parameters such as the final time for simulation and the specfication of the differential-equation
solution method. A listing is given below.
%
%
%
%
%
%
%
%

NLS_Input .m
Input f i l e f o r r u n n i n g a n o n l i n e a r s i m u l a t i o n used a s a t u t o r i a l f o r
l e a r n i n g how t o u s e SFunction f i l e s i n s i d e a SIMULINK model f i l e
and how t o s t r u c t r e an o v e r a l l s i m u a l t i o n u s i n g s e v e r a l MF i l e s .
This f i l e

s p e c i f i e s the f o l l o w i n g v a r i a b l e s :

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

7.1

%
%
%
%
%

Defining Inputs

1)
2)
3)
4)

The
The
The
The

APPENDIX

model p a r a m e t e r s a l p h a and b e t a
s t a t e e q u a t i o n s o l v e r p a r a m e t e r s
i n i t i a l c o n d i t i o n s of the s t a t e equation
height M of the input step f u n c t i o n

% Written by P r o f . Oscar D. C r i s a l l e , U n i v e r s i t y o f F l o r i d a

%= = = = = = = = = = = = = = = = = =
% Initialization
%= = = = = = = = = = = = = = = = = =
clear all
%= = = = = = = = = = = = = = = = = =
% Model p a r a m e t e r s
%= = = = = = = = = = = = = = = = = =
alpha =
beta =

3;
10;

% alpha = [ 1 . . . 100]
% b e t a = [ 1 . . . 10 ]

%= = = = = = = = = = = = = = = = = =
% I n i t i a l States
%= = = = = = = = = = = = = = = = = =
x0_1
x0_2

=
1;
= 25;

%
%

%= = = = = = = = = = = = = = = = = =
% F i n a l time f o r t h e s i m u l a t i o n
%= = = = = = = = = = = = = = = = = =
t_final = 20;

% End time f o r t h e s i m u l a t i o n

%= = = = = = = = = = = = = = = = = =
% SIMULINK S o l v e r p a r a m e t e r s
%= = = = = = = = = = = = = = = = = =
s o l v = ode45

rel
abs
ref
maxs
istep
maxo
fix
oppts
maxr
dec

;%
%
%
%
= 1 . 0 0 0 0 e 03 ;%
= 1 . 0 0 0 0 e 06 ;%
= 1
;%
= auto
;%
= auto
;%
= 5
;%
= auto
;%
= all
;%
= 0
;%
= 1
;%

c
2010
by Oscar D. Crisalle

solver : [ VariableStepDiscrete |
ode45 | ode23 | ode113 | o d e 1 5 s |
o d e 2 3 s | F i x e d S t e p D i s c r e t e | ode5 |
ode4 | ode3 | ode2 | ode1 ]
R e l a t i v e e r r o r t o l e r a n c e [ p o s i t i v e s c a l a r {1 e 3} ]
A b s o l u t e e r r o r t o l e r a n c e [ p o s i t i v e s c a l a r {1 e 6} ]
Output r e f i n e m e n t f a c t o r [ p o s i t i v e i n t e g e r {1} ]
Steps i z e Upper bound [ p o s i t i v e s c a l a r { auto } ]
S u g g e s t e d i n i t i a l s t e p s i z e [ pos s c a l a r { auto } ]
Maximum o r d e r o f ODE15S [ 1 | 2 | 3 | 4 | {5} ]
Fixed s t e p s i z e [ p o s i t i v e s c a l a r ]
Determine output p o i n t s [ { s p e c i f i e d } | a l l ]
L im it nbr o f output rows [ nonn e g a t i v e i n t e g e r { 0 } ]
Decimation f o r output v a r s [ p o s i t i v e i n t e g e r {1} ]

SIMULINK MINIGUIDE

10

7.2

Driver File

istate = []
fsn
=
debug = o f f
trace =
swork = base
dwork = c u r r e n t
zero = off
OutputVars = txy

;%
;%
;%
;%
;%
;%
;%
;%

APPENDIX

I n i t i a l c o n t and d i s c s t a t e s [ v e c t o r { [ ] } ]
Name o f f i n a l s t a t e s v a r i a b l e [ s t r i n g { } ]
Debug [ o f f | on ]
Comma s e p l i s t o f [ minstep , s i m i n f o , compile , ]
Where t o e v a l e x p r e s s i o n s [ { b a s e } | c u r r e n t | p a r e n t ]
Where t o a s s i g n v a r i a b l e s [ b a s e | { c u r r e n t } | p a r e n t ]
Enable / d i s a b l e l o c a t i o n o f z e r o c r o s s i n g s [ on | o f f ]
Output v a r i a b l e s [ txy | tx | ty | xy | t | x | y ]

%= = = = = = = = = = = = = = = = = =
% Step i n p u t p a r a m e t e r s
%= = = = = = = = = = = = = = = = = =
t_s = 2
M
= 3

; % Time a t which t h e s t e p t a k e s p l a c e
; % Step h e i g h t M = [ 0 . 1 . . . 2 5 ]

%= = = = = = = = = = = = = = = = = =
% call driver f i l e
%= = = = = = = = = = = = = = = = = =
NLS_Driver
%
% End o f MF i l e NLS_Input .m.

7.2

Driver File

The MATLAB M-FILE NLS_Driver.m calls a number of other files that run display the inputs to the screen,
run the SIMULINK model file, and that generate plots of the results.
%
%
%
%
%
%
%
%
%
%
%
%
%
%

NLS_Driver .M

This i s a d r i v e r f i l e t o run a n o n l i n e a r s i m u l a t i o n used a s a t u t o r i a l


f o r l e a r n i n g how t o u s e SFunction f i l e s i n s i d e a SIMULINK model f i l e
and how t o s t r u c t u r e an o v e r a l l s i m u l a t i o n u s i n g s e v e r a l MF i l e s
The u s e r must e d i t t h e f i l e NLS_Input .m b e f o r e r u n n i n g t h i s f i l e .
This program c a l l s t h e f o l l o w i n g f i l e s :
1 . MATLAB f i l e
NLS_Display2Screen .m
2 . SIMULINK f i l e NLS_Sim . mdl
3 . MATLAB f i l e
NLS_PlotResults .m

% Written by P r o f . Oscar D. C r i s a l l e , U n i v e r s i t y o f F l o r i d a
%= = = = = = = = = = = = = = = = = = = = = =
% D i s p l a y program i n p u t s t o s c r e e n
%= = = = = = = = = = = = = = = = = = = = = =
NLS_Display2Screen

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

11

7.3

Printing to the Screen

7 APPENDIX

%= = = = = = = = = = = = = = = = = = = = = =
% D e f i n e t h e model parameter v e c t o r
%= = = = = = = = = = = = = = = = = = = = = =
p(1)
p(2)
p(3)
p(4)

=
=
=
=

alpha
beta
x0_1
x0_2

;
;
;
;

%= = = = = = = = = = = = = = = = = = = = = =
% Define the s o l v e r parameters
%= = = = = = = = = = = = = = = = = = = = = =
% Obtain t h e c u r r e n t v e c t o r o f s o l v e r o p t i o n s
o p t i o n s = s i m g e t ( NLS_Sim )
% Redefine s p e c i f i c paramters o f the vector o f s o l v e r options
options = simset ( solver , solv , r e l t o l , rel , . . .
a b s t o l , abs , r e f i n e , r e f , . . .
maxstep , maxs , i n i t i a l s t e p , i s t e p , . . .
maxorder , maxo , f i x e d s t e p , f i x , . . .
o u t p u t p o i n t s , oppts , o u t p u t v a r i a b l e s , OutputVars ,
maxrows , maxr , d e c i m a t i o n , dec , . . .
i n i t i a l s t a t e , i s t a t e , finalstatename , fsn , . . .
debug , debug , t r a c e , t r a c e , . . .
s r c w o r k s p a c e , swork , dstworkspace , dwork , . . .
zerocross , zero )

...

%= = = = = = = = = = = = = = = = = = = = = =
% C a l l s i m u l i n k and e x e c u t e t h e model f i l e
%= = = = = = = = = = = = = = = = = = = = = =
[ t , x , y , u ] = sim ( NLS_Sim , [ 0 t _ f i n a l ] , o p t i o n s ) ;
%= = = = = = = = = = = = = = = = = = = = = =
% Plot the r e s u l t s
%= = = = = = = = = = = = = = = = = = = = = =
NLS_PlotResults
shg
%
% End o f MF i l e NLS_Driver .m.

7.3

Printing to the Screen

MATLAB M-FILE NLS_Display2Screen.m generates plots of the outputs of the simulation as a function
of time.

% NLS_Display2Screen
c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

12

7.3

%
%
%
%
%
%

Printing to the Screen

7 APPENDIX

Rica rdo GomezGonzalez 02/12/02 Rev 2


t h i s f i l e d i s p l a y s t h e p a r a m e t e r s and v a r i a b l e s needed
t o run t h e s i m u l a t i o n .
A l l t h e p a r a m e t e r s and v a r i a b l e s a r e d e f i n e d i n POP_SIM_A3.M

% Written by P r o f . Oscar D. C r i s a l l e , U n i v e r s i t y o f F l o r i d a
msg1 {1} = s p r i n t f ( \ n==== NONLINEAR SYSTEM SIMULATION ======================\n ) ;
msg1 {2} = s p r i n t f ( ==== I n i t i a l Parameters ===============================\n )
;
f o r i = 1 : 2 ; d i s p ( msg1{ i } ) ; end
msg2 {1}
msg2 {2}
msg2 {3}
msg2 {4}
msg2 {5}
msg2 {6}

=
=
=
=
=
=

sprintf (
sprintf (
sprintf (
sprintf (
sprintf (
sprintf (

Parameter a l p h a
Parameter b e t a
Step h e i g h t M
F i r s t s t a t e
i n i t i a l condition
Seconds t a t e i n i t i a l c o n d i t i o n
Step h e i g h t M

=
=
=
=
=
=

%g
%g
%g
%g
%g
%g

, alpha ) ;
, beta ) ;
,M)
;
, x0_1 ) ;
, x0_2 ) ;
,M)
;

f o r i = 1 : 6 ; d i s p ( msg2{ i } ) ; end
msg3 {1} = s p r i n t f ( \ n==== S i m u l a t i o n Parameters ===========================\n )
;
msg3 {2} = s p r i n t f ( F i n a l time f o r t h e s i m u l a t i o n
= %g s e c \n , t _ f i n a l ) ;
f o r i = 1 : 2 ; d i s p ( msg3{ i } ) ; end
msg4 {1}
;
msg4 {2}
;
msg4 {3}
;
msg4 {4}
;
msg4 {5}
;
msg4 {6}
;
msg4 {7}
;
msg4 {8}
;
msg4 {9}
msg4 {10}
;
msg4 {11}
;
msg4 {13}
;

= s p r i n t f ( S o l v e r method t o advance time ( S o l v e r )

= %s , s o l v )

= s p r i n t f ( R e l a t i v e e r r o r t o l e r a n c e ( RelTol

= %g , r e l )

= s p r i n t f ( A b s o l u t e e r r o r t o l e r a n c e ( AbsTol )

= %g , abs )

= s p r i n t f ( Output r e f i n e m e n t f a c t o r ( R e f i n e )

= %g , r e f )

= s p r i n t f ( Upper bound on t h e s t e p s i z e ( MaxStep )

= %s , maxs )

= s p r i n t f ( Suggested i n i t i a l step s i z e ( I n i t i a l S t e p )

= %s , i s t e p )

= s p r i n t f ( Fixed s t e p s i z e ( F i x e d s t e p )

= %s , f i x )

= s p r i n t f ( Determine output p o i n t s ( OutputPoints )

= %s , o p p t s )

= s p r i n t f ( S e t output v a r i a b l e s ( O u t p u t V a r i a b l e s )
= %s , OutputVars ) ;
= s p r i n t f ( Decimation f o r output v a r i a b l e s ( Decimation ) = %g , dec )
= s p r i n t f ( Where t o e v a l u a t e e x p r e s s i o n s ( SrcWorkspace ) = %s , swork )
= s p r i n t f ( Location of zero c r o s s i n g s ( ZeroCross )

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

= %s \n , z e r o )

13

7.4

Plotting Results

APPENDIX

f o r i = 1 : 1 3 ; d i s p ( msg4{ i } ) ; end
% End o f MF i l e NLS_Display2Screen .m.

7.4

Plotting Results

MATLAB M-FILE NLS_PlotResults.m generates plots of the outputs of the simulation as a function of
time
% NLS_PlotResults .m
%
% P l o t s a s a f u n c t i o n o f time t h e f o l l o w i n g v a r i a b l e s :
%
%
o u t p u t s y1 and y2
%
input u
%
% Written by P r o f . Oscar D. C r i s a l l e , U n i v e r s i t y o f F l o r i d a
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% Clear a l l previous f i g u r e s
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
clf
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% R e c o n s t r u c t each s t a t e and output v e c t o r
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
x1 = x ( : , 1 )
x2 = x ( : , 2 )

y1 = y ( : , 1 )
y2 = y ( : , 2 )
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% P l o t t h e o u t p u t s y1 and y1
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
subplot (311)
p l o t ( t , y1 , b . )
h o l d on
p l o t ( t , y2 , r )

;
;

y l a b e l ( y_1 ( t ) , y_2 ( t ) )

t i t l e _ s t r = [ \ a l p h a = , num2str ( a l p h a ) ]
title_str = [ title_str , ,
\ b e t a = , num2str ( b e t a ) ] ;
title ( title_str )
l e g e n d ( y_1 , y_2 , 2 )
hold o f f
grid

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

;
;
;

14

7.4

Plotting Results

APPENDIX

%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% P l o t t h e o u t p u t s y1 and y1
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
subplot (312)
p l o t ( t , x1 , b . )
h o l d on
p l o t ( t , x2 , r )

;
;

y l a b e l ( x_1 ( t ) , x_2 ( t ) )

l e g e n d ( x_1 , x_2 , 2 )
hold o f f
grid

%= = = = = = = = = = = = = = = = = = = = = = = = = = =
% Plot the input f u n c t i o n u
%= = = = = = = = = = = = = = = = = = = = = = = = = = =
subplot (313)
s t a i r s ( t , u , g )
umin = min ( u )
umax = max( u )
uavg = ( mean ( [ umin umax ] ) )

umin = ( umin 0 . 4 uavg )


umax = ( umax + 0 . 4 uavg )

;
;

a x i s ( [ 0 t _ f i n a l umin umax ] )
xlabel ( t )
ylabel ( u( t ) )
grid

%
% End o f MF i l e NLS_PlotResults .m.

Acknowledgements
Several gradaute students at the University of Florida contributed to earlier versions of this document,
including Dr. Ricardo Gomez-Gonzalez, and doctoral cnadidates Rafe Biswas and Shyym Prasad Mudiraj.
The author is grateful for their valuable contributions.

c
2010
by Oscar D. Crisalle

SIMULINK MINIGUIDE

15

Vous aimerez peut-être aussi