0
0
SimulinkHow-ToBeginner · 4 min read

How to Verify Simulink Model: Steps and Best Practices

To verify a Simulink model, run simulations to check if outputs meet expected results, use Model Advisor for automated checks, and apply Test Harnesses to isolate and test components. These steps help ensure your model behaves correctly before deployment.
📐

Syntax

Here are key commands and tools used to verify a Simulink model:

  • sim('model_name'): Runs the simulation of the model.
  • ModelAdvisor.checkModel('model_name'): Runs automated checks on the model.
  • sltest.harness.create('model_name','block_path'): Creates a test harness for a specific block.
  • assert(condition, 'message'): Used in MATLAB scripts to verify expected conditions.

Each helps verify different aspects: simulation tests behavior, Model Advisor checks design rules, and test harnesses isolate parts for focused testing.

matlab
sim('myModel')
ModelAdvisor.checkModel('myModel')
sltest.harness.create('myModel','myModel/Subsystem')
assert(abs(output - expectedOutput) < tolerance, 'Output mismatch')
💻

Example

This example shows how to simulate a model, check it with Model Advisor, and create a test harness for a subsystem.

matlab
% Load and simulate the model
load_system('myModel')
simOut = sim('myModel');

% Run Model Advisor checks
results = ModelAdvisor.checkModel('myModel');
disp(results);

% Create a test harness for a subsystem
harness = sltest.harness.create('myModel','myModel/Subsystem');
disp(['Test harness created at: ', harness]);
Output
ModelAdvisor results displayed in MATLAB Command Window Test harness created at: myModel/Subsystem_harness
⚠️

Common Pitfalls

  • Not running simulations with varied inputs can miss errors.
  • Ignoring Model Advisor warnings leads to design flaws.
  • Forgetting to isolate components with test harnesses makes debugging harder.
  • Not comparing simulation outputs to expected results causes unnoticed bugs.

Always validate outputs explicitly and use test harnesses to focus on parts of the model.

matlab
% Wrong: Running simulation without checking outputs
sim('myModel')

% Right: Check output against expected value
simOut = sim('myModel');
assert(abs(simOut.yout{1}.Values.Data(end) - expectedValue) < 1e-3, 'Output does not match expected value')
📊

Quick Reference

Command/ToolPurpose
sim('model_name')Run model simulation
ModelAdvisor.checkModel('model_name')Run automated model checks
sltest.harness.create('model_name','block_path')Create test harness for component testing
assert(condition, 'message')Verify expected conditions in scripts

Key Takeaways

Run simulations and compare outputs to expected results to verify behavior.
Use Model Advisor to automatically check model design and compliance.
Create test harnesses to isolate and test individual components effectively.
Always validate outputs explicitly to catch errors early.
Address Model Advisor warnings to improve model quality.