Simulate a Simulink model
A Simulink® model represents a dynamic system. Simulating a model lets you understand
the behavior of the system as a function of simulated time. The sim
command uses the specified model and applies the arguments to the model. At the end of
the simulation, the sim command reverts any arguments that it
applied.
simulates the specified model using existing model configuration parameters, and
returns the result as a simOut =
sim(model)Simulink.SimulationOutput object
(single-output format).
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 model using the inputs specified in the simOut = sim(simIn)Simulink.SimulationInput object
simIn . The sim command can be 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.
simulates the specified model using the parameter values specified in the structure
simOut =
sim(model,ParameterStruct)ParameterStruct.
This example modifies the block parameters of a model through
the SimulationInput object.
Open the model.
openExample('simulink/OpenTheModelExample'); open_system('ex_sldemo_househeat'); load_system('ex_sldemo_househeat')
Create a SimulationInput object for this
model.
mdl = 'sldemo_househeat';
in = Simulink.SimulationInput(mdl);Modify block parameter.
in = in.setBlockParameter('sldemo_househeat/Set Point','Value','300');
Simulate the model.
out = sim(in)
Simulate the model, vdp as it is in the present state, whether
it is not loaded or is loaded and has some unsaved changes.
On the Data Import/Export pane of the Configuration Parameters dialog
box,Single simulation output is selected by default, so
sim returns the simulation results using the single-output
format (simulation object). This selection overrides the Dataset
format used for signal logging.
simOut = sim('vdp')sim Command-Line Options in StructureSimulate the model, vdp, 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.SaveState = 'on'; paramNameValStruct.StateSaveName = 'xoutNew'; paramNameValStruct.SaveOutput = 'on'; paramNameValStruct.OutputSaveName = 'youtNew'; simOut = sim('vdp',paramNameValStruct)
simOut =
Simulink.SimulationOutput:
xoutNew: [64x2 double]
youtNew: [64x2 double]
SimulationMetadata: [1x1 Simulink.SimulationMetadata]
ErrorMessage: [0x0 char]
sim Command-Line Options in Configuration SetSimulate the model, vdp, 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) 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)
simOut =
Simulink.SimulationOutput:
xoutNew: [65x2 double]
youtNew: [65x2 double]
SimulationMetadata: [1x1 Simulink.SimulationMetadata]
ErrorMessage: [0x0 char]
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 To Workspace block such as the To Workspace block or the Scope block, or use Signal Logging. For more information, see Export Signal Data Using Signal Logging.
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'.