How to Run Simulation in Simulink: Step-by-Step Guide
To run a simulation in
Simulink, open your model and click the Run button on the toolbar or use the command sim('modelname') in MATLAB. This starts the simulation and shows results based on your model configuration.Syntax
The basic syntax to run a simulation in Simulink using MATLAB command line is:
sim('modelname'): Runs the simulation of the model namedmodelname.sim('modelname', 'StopTime', '10'): Runs the simulation and stops at time 10 seconds.simOut = sim('modelname'): Runs the simulation and stores output insimOutvariable.
You can customize simulation time and capture outputs using these options.
matlab
sim('modelname') sim('modelname', 'StopTime', '10') simOut = sim('modelname')
Example
This example shows how to run a simple simulation of a model named simple_model and get the simulation output.
matlab
model = 'simple_model'; load_system(model); % Load the model into memory simOut = sim(model, 'StopTime', '5'); % Run simulation for 5 seconds simData = simOut.get('yout'); % Get simulation output data disp(simData);
Output
[Simulation output data displayed here, e.g., timeseries or array]
Common Pitfalls
Common mistakes when running simulations in Simulink include:
- Not loading the model before running
sim, which causes errors. - Using incorrect model name or path, leading to 'model not found' errors.
- Forgetting to set the
StopTime, which may cause very long or infinite simulations. - Not capturing output if you want to analyze results programmatically.
Always verify your model is loaded and the simulation parameters are set correctly.
matlab
%% Wrong way (model not loaded) sim('simple_model') % May cause error if model is not loaded %% Right way load_system('simple_model'); sim('simple_model', 'StopTime', '5')
Quick Reference
Here is a quick cheat sheet for running simulations in Simulink:
| Command | Description |
|---|---|
| sim('modelname') | Run simulation of the model |
| sim('modelname', 'StopTime', '10') | Run simulation and stop at 10 seconds |
| load_system('modelname') | Load model into memory before simulation |
| simOut = sim('modelname') | Run simulation and save output to variable |
| close_system('modelname') | Close the model after simulation |
Key Takeaways
Use
sim('modelname') to run your Simulink model simulation from MATLAB.Always load your model with
load_system before simulating to avoid errors.Set
StopTime to control how long the simulation runs.Capture simulation output by assigning
sim result to a variable for analysis.Check model name spelling and path to prevent 'model not found' errors.