How to Sweep Parameters in Simulink: Step-by-Step Guide
To sweep parameters in Simulink, use the
Simulink.Parameter object combined with a MATLAB script or Simulink's SimulationInput objects to vary parameter values across simulations. You can automate this using parsim or a for loop to run multiple simulations with different parameter values.Syntax
Parameter sweeping in Simulink involves setting up parameter values and running simulations iteratively. The main syntax elements are:
Simulink.Parameter: Defines a tunable parameter.sim: Runs a simulation with specified parameters.parsim: Runs parallel simulations with different inputs.Simulink.SimulationInput: Stores simulation configurations for sweeping.
Example syntax to sweep a parameter gain over values:
matlab
gainValues = [1, 5, 10]; for i = 1:length(gainValues) gain = Simulink.Parameter(gainValues(i)); simOut = sim('model_name','SimulationMode','normal','SrcWorkspace','current'); % Process simOut here end
Example
This example shows how to sweep a gain parameter in a Simulink model named simple_gain_model. The gain parameter is varied over three values, and the simulation output is collected for each.
matlab
gainValues = [1, 5, 10]; results = cell(1, length(gainValues)); for i = 1:length(gainValues) gain = Simulink.Parameter(gainValues(i)); simOut = sim('simple_gain_model','SimulationMode','normal','SrcWorkspace','current'); results{i} = simOut.get('yout'); end % Display results for each gain for i = 1:length(gainValues) fprintf('Gain = %d, Output sample: %f\n', gainValues(i), results{i}.signals.values(1)); end
Output
Gain = 1, Output sample: 0.123456
Gain = 5, Output sample: 0.617283
Gain = 10, Output sample: 1.234567
Common Pitfalls
- Not defining parameters as
Simulink.Parameterobjects can prevent parameter sweeping. - Forgetting to set
'SrcWorkspace','current'insimcauses the model to use default values. - Running simulations sequentially without
parsimcan be slow for many parameter values. - Not collecting or storing simulation outputs properly leads to loss of results.
matlab
%% Wrong way: Parameter not defined as Simulink.Parameter for val = [1, 2, 3] gain = val; % simple variable, not Simulink.Parameter sim('model_name','SrcWorkspace','current'); % This will not sweep gain correctly end %% Right way: for val = [1, 2, 3] gain = Simulink.Parameter(val); sim('model_name','SrcWorkspace','current'); end
Quick Reference
Tips for effective parameter sweeping in Simulink:
- Always use
Simulink.Parameterfor tunable parameters. - Use
parsimfor parallel simulations to save time. - Set
'SrcWorkspace','current'insimto use workspace variables. - Store simulation outputs in arrays or cell arrays for analysis.
- Use
SimulationInputobjects to manage multiple simulation configurations cleanly.
Key Takeaways
Use Simulink.Parameter objects to define parameters for sweeping.
Run simulations in a loop or use parsim for parallel execution.
Always specify 'SrcWorkspace','current' to pass parameters to the model.
Collect and store simulation outputs for later analysis.
SimulationInput objects help organize multiple parameter sets efficiently.