sim

Simulate dynamic system

Syntax

sim(model,timespan,options,ut);
[t,x,y] = sim(model,timespan,options,ut);
[t,x,y1, y2, ..., yn] = sim(model,timespan,options,ut);

Description

The sim command causes the specified Simulink® model to be executed. The model is executed with the data passed to the sim command, which may include parameter values specified in an options structure. The values in the structure override the values shown for block diagram parameters in the Configuration Parameters dialog box, and the structure may set additional parameters that are not otherwise available (such as DstWorkSpace). The parameters in an options structure are useful for setting conditions for a specific simulation run.

Use the simset command to create an options structure for use by the sim command. The simset command inputs name-value pairs and sets each named parameter to the value indicated. You do not need to specify values for all block diagram parameters that simset accepts. In most cases, an unspecified parameter defaults to the block diagram value that is current when sim executes the model, but some exceptions exist. See the simset command documentation for details.

With one exception, the default workspace for a simulation executed by the sim command is the MATLAB® workspace. The exception is that the default workspace for To Workspace blocks is the workspace of the function that invoked the sim command.

Superseding the Base Workspace

When you run a simulation interactively, the Simulink software tries to resolve any symbols used in the model to appropriate workspace items, as described in Resolving Symbols and Hierarchical Symbol Resolution, and it writes any exported or logged data to the MATLAB base workspace, as described in Importing and Exporting Simulation Data and Logging Signals.

When you use the sim command to run a simulation programmatically, you have two options that do not exist with interactive simulation: you can specify a workspace other than the MATLAB base workspace as the last workspace searched in hierarchical symbol resolution, and a workspace other than the MATLAB base workspace as the destination for any data logged or exported during simulation.

Most simulation is interactive, so most Simulink documentation does not mention these possibilities: it unconditionally describes the MATLAB base workspace as the final workspace searched during hierarchical symbol resolution, and the workspace to which any exported or logged data is written.

To supersede the base workspace for symbol resolution, data output, or both, provide an options structure to the sim command and set one or both of the following two structure fields:

Each of these fields can take any of these three values:

If you execute the sim command without providing an options structure, hierarchical resolution, data logging, and data export occur exactly as they do for interactive simulation. When you supersede the base workspace, the change is effective only for the duration of the sim command. After the command completes, the base workspace is accessible just as it was previously. You cannot supersede the base workspace for a sequence of programmatic simulations by setting a global state: each execution of the sim command must specify its own SrcWorkspace or DstWorkspace as needed.

Arguments

t

Returns the simulation's time vector.

x

Returns the simulation's state matrix consisting of continuous states followed by discrete states.

y

Returns the simulation's output matrix. Each column contains the output of a root-level Outport block, in port number order. If any Outport block has a vector input, its output takes the appropriate number of columns.

y1,...,yn

Each yi returns the output of the corresponding root-level Outport block for a model that has n such blocks.

model

Name of a block diagram.

timespan

Simulation start and stop time. Specify as one of these:

tFinal to specify the stop time. The start time is 0.

[tStart tFinal] to specify the start and stop times.

[tStart OutputTimes tFinal] to specify the start and stop times and time points to be returned in t. Generally, t includes more time points. OutputTimes is equivalent to specifying Configuration Parameters > Data Import/Export > Output options > Produce additional output.

options

Optional simulation parameters specified as a structure created by the simset command (see simset).

ut

Optional external inputs to top-level Inport blocks. ut can be a MATLAB function (expressed as a string) that specifies the input u = UT(t) at each simulation time step, a table of input values versus time for all input ports, or a comma-separated list of tables, ut1, ut2, ..., each of which corresponds to a specific port. Tabular input for all ports can be in the form of a MATLAB array or a structure. Tabular input for individual ports must be in the form of a structure. See Importing Data from a Workspace in the online documentation for a description of the array and structure input formats.

Examples

This command simulates the Van der Pol equations, using the vdp model that comes with Simulink software. The command uses all default parameters.

[t,x,y] = sim('vdp')

This command simulates the Van der Pol equations, using the parameter values associated with the vdp model, but defines a value for the Refine parameter.

[t,x,y] = sim('vdp', [], simset('Refine',2));

This command simulates the Van der Pol equations for 1,000 seconds, saving the last 100 rows of the return variables. The simulation outputs values for t and y only, but saves the final state vector in a variable called xFinal.

[t,x,y] = sim('vdp', 1000, simset('MaxRows', 100, 
          'OutputVariables', 'ty', 'FinalStateName', 'xFinal'));

See Also

simset, simget

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS