Simulate dynamic system
simulates the specified model using existing model configuration parameters, and
returns the result as either a simOut
=
sim(model
)Simulink.SimulationOutput
object
(single-output format) or as a time vector compatible with Simulink version R2009a
or earlier. See Backward-Compatible Syntax.
To return simulation results using the single-output format (simulation object),
Single simulation output on the Data Import/Export pane of
the Configuration Parameters dialog box is selected by default. This selection
overrides the Dataset
format used for signal logging.
To return simulation results using the backward-compatible format (time vector), see Backward-Compatible Syntax
simulates the specified model using parameter name-value pairs.simOut
=
sim(model
,Name,Value
)
simulates
the specified model using the parameter values specified in the structure simOut
=
sim(model
,ParameterStruct
)ParameterStruct
.
simulates a
model using the inputs specified in the simOut
= sim(simIn
)SimulationInput
object,
simIn
. The sim command is also used with an array of
SimulationInput
objects to run multiple simulations in a
series. If simIn
is an array of
Simulink.SimulationInput
objects, output is returned as an
array of Simulink.SimulationOutput
objects.
sim
Command-Line Options in StructureSimulate the model, vdp
, in Rapid Accelerator mode for an absolute tolerance of 1e-5
and save the states in xoutNew
and the output in youtNew
.
Specify parameters using a name-value pairs structure paramNameValStruct
for the sim
command:
paramNameValStruct.SimulationMode = 'rapid'; paramNameValStruct.AbsTol = '1e-5'; paramNameValStruct.SaveState = 'on'; paramNameValStruct.StateSaveName = 'xoutNew'; paramNameValStruct.SaveOutput = 'on'; paramNameValStruct.OutputSaveName = 'youtNew'; simOut = sim('vdp',paramNameValStruct)
### Building the rapid accelerator target for model: vdp ### Successfully built the rapid accelerator target for model: vdp Build Summary Top model rapid accelerator targets built: Model Action Rebuild Reason ====================================================================================== vdp Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 7.1872s
simOut = Simulink.SimulationOutput: xoutNew: [65x2 double] youtNew: [65x2 double] SimulationMetadata: [1x1 Simulink.SimulationMetadata] ErrorMessage: [0x0 char]
sim
Command-Line Options in Configuration SetSimulate the model, vdp
, in Rapid Accelerator mode for an absolute tolerance of 1e-5
and save the states in xoutNew
and the output in youtNew
.
Specify parameters as name-value pairs in configuration set mdl_cs
for the sim
command:
mdl = 'vdp'; load_system(mdl) simMode = get_param(mdl, 'SimulationMode'); set_param(mdl, 'SimulationMode', 'rapid') cs = getActiveConfigSet(mdl); mdl_cs = cs.copy; set_param(mdl_cs,'AbsTol','1e-5',... 'SaveState','on','StateSaveName','xoutNew',... 'SaveOutput','on','OutputSaveName','youtNew') simOut = sim(mdl, mdl_cs);
### Building the rapid accelerator target for model: vdp ### Successfully built the rapid accelerator target for model: vdp Build Summary Top model rapid accelerator targets built: Model Action Rebuild Reason ====================================================================================== vdp Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 7.8027s
set_param(mdl, 'SimulationMode', simMode)
Simulate the model vdp
using default model
configuration parameters.
simOut = sim('vdp','ReturnWorkspaceOutputs','on')
Parameters specified using the sim
command
override the values defined in the Model Configuration Parameters dialog
box. The software restores the original configuration values at the
end of simulation.
In the case of a model with a model reference block, the parameter specifications are applied to the top model.
When simulating a model with infinite stop time, to stop the simulation, you must press Ctrl+C. Ctrl+C breaks the simulation and the simulation results are not saved in the MATLAB workspace.
To specify the time span for a simulation, you must
specify the StartTime
and StopTime
parameters.
To log the model time, states, or outputs, use the Data Import/Export pane of the Model Configuration Parameters dialog box.
To log signals, either use a block such as the To Workspace block or the Scope block, or use the Viewers and Generators Manager to log results directly.
To get a list of simulation parameters for the model vdp
,
in the MATLAB Command Window, enter:
configSet = getActiveConfigSet('vdp') configSetNames = get_param(configSet, 'ObjectParameters')
This command lists several object parameters, including simulation parameters such as
'StopTime'
, 'SaveTime'
,
'SaveState'
, 'SaveOutput'
, and
'SignalLogging'
.