Have you ever had a numerical discrepancy in your model
that you didn't know how to investigate?
Is there something you can do besides log all your signals
and then search for the needle in the haystack of data
that might give you insight into the problem?
There has to be a better way.
Hi, my name is Sara.
In this video, I'll show you how to debug your model
by using Conditional Breakpoints,
stepping through major time steps,
and visualizing how signal values change as you
step through the simulation.
Let's start with an example.
This model has a numerical issue I need to investigate.
Before we start debugging, let's enable Fast Restart
to keep the model in a compiled state,
and avoid costly compilation times, since I
know I'll run multiple simulations while debugging.
The issue in this model is in the algorithm
the referenced model implements.
The Scope displays the signal of interest,
which I expect to be sinusoidal with an amplitude of 0.1.
But as you can see here, at around 5 seconds,
there's a huge negative spike that
makes the signal appear nonsinusoidal.
So where do we start trying to diagnose the issue?
Well, we can add a Conditional Breakpoint
to the signal to detect when the value makes
this large deviation from our expectations.
To set a Conditional Breakpoint, select a Signal.
Then, on the Debug tab, click Add Breakpoint.
In the window that appears, specify the condition
and value.
For this case, let's configure the Breakpoint
to pause the simulation when the signal value becomes smaller
than negative 1,000.
You can also add Breakpoints to other parts of a model,
including code inside a MATLAB Function block
and in Stateflow charts.
Now, let's open the Breakpoints List,
so we can monitor and edit our set of Breakpoints.
You can easily navigate to a Breakpoint
by clicking the hyperlink in the Breakpoints List.
For the numerical issue we're investigating,
I don't expect the issue to be in the MATLAB Function
code or the Stateflow chart.
So I'll disable those break points.
Now, let's add port value labels,
so we can see the signal values.
To add port value labels, select the blocks or signals
of interest.
Then, on the Debug tab, click Output Values.
We are almost ready to start debugging.
Before we simulate, let's configure the simulation
stepper to allow stepping back.
This way we can move backward and forward in time,
around the Breakpoint, to investigate
what is happening in the model.
Now, let's rerun the simulation.
Because we already ran one simulation with Fast Restart
enabled, the model is still compiled,
and restarting the simulation takes minimal time.
As expected, the simulation pauses on the breakpoint.
At the time when the value becomes smaller
than negative 1,000.
The Breakpoint on which we paused
is highlighted in the Breakpoints List.
For this example, we added port value labels
before starting the simulation.
You can also add port value labels
while debugging, including now, while we're
paused on a Breakpoint.
When you add a new port value label,
it shows the message, no data yet.
The label updates to display the signal value the next time
the source block executes.
Now, I can step backwards and forwards
around this time of interest, and observe how the signal
values change with each step.
The denominator input for the divide block
has a value of almost 0 at T equals
5, which is likely the root cause of the issue.
Armed with this information, I can check the parameter values
for the upstream blocks.
Now, I realize the slope value for this ramp block
is missing a minus sign.
After adding the missing minus sign, when we simulate again,
the simulation does not pause on the Breakpoint condition,
and the Scope displays the expected sinusoidal output.
To summarize, conditional break points
are an effective tool you can use, in conjunction
with the simulation stepper and port value labels,
to diagnose numerical problems in your models.
So give these tools a try.
Doc links are provided in the video description
below, for further details.