Main Content

Run Fault Simulations and View Results

This example shows how to run fault simulations. When you model faults, you often need to run more than one simulation. You can interactively run simulations, or adjust the parameters in your fault behaviors programmatically and automate the simulations with MATLAB® code. You can inspect the results in the Simulation Data Inspector.

Video Walkthrough

For a walkthrough of the example, play the video.

Simulate the Model Interactively

Open the sldemo_fuelsys_fault_analyzer model and set up the faults with the helper function, myFuelSysCase.


After executing the function, the model contains faults on each input port to the To Controller subsystem. The model has one activated fault, throttle_fault, that triggers when the time is 25.

Simulate the model. The Scope block captures signal values during the most recent simulation.

model = "sldemo_fuelsys_fault_analyzer";

To see the status of the faults throughout simulation, you can inspect the results with the Simulation Data Inspector. In the Fault Analyzer tab, in the Review Results section, click Data Inspector. You can also open the Simulation Data Inspector with this command:


The Simulation Data Inspector logs the status of faults as either true or false. In this example, confirm that the fault was not injected until 25 seconds by expanding the Faults list for the first run and selecting the fault, throttle_fault, to plot the status versus time.

The simulation results in the Simulation Data Inspector. The faults section is expanded, and throttle_fault is selected. The injection status of throttle_fault is shown in the plot. It is false until 25 seconds, afterwards the injection status is true. The status is blue.

You can also confirm that the fault injected by opening the Fault Dashboard pane. In the Fault Analyzer tab, in the View section, click Fault Dashboard. The Simulation Results tab shows that the active fault, throttle_fault, triggered during the simulation.

The image shows the Simulations Results tab in the Fault Dashboard pane. The pane shows the number of simulated faults out of the number of active faults. The pane shows that one fault is active, and one fault simulated. The Triggered Faults section is expanded, and it lists throttle_fault as the triggered fault.

Use Scripts to Adjust Fault Properties and Simulate

Each fault corresponds to a Fault object that has properties that you can adjust programmatically. In this example, increment the trigger time of the fault, throttle_fault, and simulate after each adjustment. This fault is on the first input port to To Controller.

To Controller is the only block with faults. Use the Simulink.fault.findFaultedElements function to retrieve the paths of the faults on this block as a string array.

elements = Simulink.fault.findFaultedElements(model);

Each array element lists the model path to the input port in numerical order. For example, the first element of elements is the path to the first input port.

ans = 
"sldemo_fuelsys_fault_analyzer/To Controller/Inport/1"

Get the Fault object by using the Simulink.fault.findFaults function.

sweptFault = Simulink.fault.findFaults(model,...

Enable faults on the first input port to To Controller, and disable faults on the other ports by using the Simulink.fault.enable function.

for n = 1:length(elements)
    if n == 1

Set throttle_fault as the active fault by using the activate function.


Turn fault simulation on by using the Simulink.fault.injection function.


throttle_fault uses a timed trigger. When the simulation reaches the specified trigger time, the fault is injected. To adjust the trigger time, modify the StartTime property of the Fault object. In this example, start at a trigger time of 10, and increment the trigger time by 2.

numberOfSims = 6;
for n = 1:numberOfSims
    sweptFault.StartTime = 8+2*n;

Use Scripts to Change Fault Behaviors and Simulate

You can also modify the behavior of the faults in your Simulink® model by accessing the Fault Subsystem block in the fault model. In this example, adjust the behavior associated with the fault, ego_fault.

Get the Fault object for ego_fault.

sweptFault = Simulink.fault.findFaults(model,...

Get the paths to the fault model and Fault Subsystem block associated with ego_fault by using the Simulink.fault.getFaultModels and getBehavior functions.

faultModel = Simulink.fault.getFaultModels(model);
faultSubsystem = getBehavior(sweptFault);

Open the Fault Subsystem block ego_fault to view the behavior.


The behavior overrides the signal with a Constant block with a value equal to 12. In this example, adjust the value of this block. Enable faults on the third input port to To Controller and disable faults on the other input ports.

for n = 1:length(elements)
    if n == 3

Set ego_fault as the active fault.


Increment the Constant value parameter of the Constant block from 7 to 12 incrementing by 1 for each simulation.

sweptBlock = "Constant";
numberOfSims = 6;
for n = 1:numberOfSims
    constVal = 6 + n;
    set_param(faultSubsystem + "/" + sweptBlock,Value=string(constVal))

Additional Simulation Options

To capture the numerical results of simulation without saving changes to the model, simulate the model by defining a Simulink.SimulationInput object for the model and saving the simulation results to a Simulink.SimulationOutput object. After creating these objects, you can also run simulations by using parsim.

Automate Adding, Deleting, and Updating Faults

You can also create, delete, and update faults with programmatic options. For a list of programmatic options, see Fault Modeling and Fault Simulation. For an additional example, see Add and Configure Faults Programmatically.

See Also

Related Topics