Helps Sheet 01 Simulink and Simscape Debugging and Tips
Helps Sheet 01 Simulink and Simscape Debugging and Tips
HS01
School of Electrical, Electronic and Mechanical Engineering
Tips Sheet HS01: Simulink and Simscape Debugging and Tips (60 mins read time)
Aims
• To provide students using Simulink and Simscape with a high-level understanding of how Simulink
works
• To provide a range of options to explore when simulations don’t run effectively or fail to run at all
• To provide an overview of some of the tools available for diagnosing problems
Dealing with simulation errors and refining the simulation parameters for an improved response.
Simulink is a tool that solves differential equations by implementing numerical solvers (a wide range are
possible in Simulink) for the integration/differentiation by discretising the time variable. The discretisation of
time into discrete “time-steps” can be fixed or can vary as the simulation progresses. SIMULINK then uses
its solver to finding numerical solutions to the system at each of these “time-steps”. 1
The solving engine is sophisticated and is able, for most simulations, to determine the most appropriate
numerical solver AND the solver time-step without intervention by the user. Furthermore, it can change the
time-step used in the numerical integration solvers whilst the simulation is running. It will do this in a way
that balances the accuracy of the simulation against the computational effort required AND returns a stable
simulation. However, in some cases the simulation will either fail with an error, or return results that do not
reflect the simulated system accurately.
Why does a simulation fail or “Go unstable” – Some common simulation errors
Simulink is capable of simulating unstable systems successfully. However, a problem occurs with unstable
systems that have signals that have very high gradients. Let’s simulate a simple unstable system, 𝐺(𝑠),
where:
1
𝐺(𝑠) =
𝑠−1
1
For info on how SIMULINK does this - https://www.mathworks.com/videos/controlling-simulation-
performance-with-solver-settings-69031.html
If we simulate this system for 10 seconds we can see the unstable response as expected (shown in Figure 1)
with the gradient of the output increasing as time increases. However, if we now try to simulate this system
for a much longer period of time, it will fail with an error message related to a derivative. The thing to look for
here is a signal that looks like it is at zero (or a fixed value) until a point in time where it changes to a vertical
line.
This happens because at some point the rate of change (or derivative) of the signal becomes sufficiently large
that SIMULINK’s cannot accurately calculate the value of the output to the accuracy required, regardless of
the time step chosen. Notice that you are given the name of the block that SIMULINK is struggling to simulate
accurately in the error message.
You can overcome time, either by:
1. removing the unstable system
2. changing the tolerance of the simulated signals (see section 0)
3. selecting a different solver or using a fixed time step (won’t work in most cases, see section 0)
This is related to the previous error and normally happens because a block in the simulation has a dynamic
element that is very stiff which triggers a non-linear function (such as a saturation block or a backlash
function) normally related to needing to calculate when a signal crosses zero.
This kind of error is most likely to appear in a Simscape simulation of a lightly damped electrical system that
uses a switch of some kind, e.g. diode or MOSFET/switch, etc.
This means that a small input into a block would result in an output that has a very high gradient (or derivative).
Normally, what would happen under these circumstances is that, if using a variable step solver, SIMULINK
will try and reduce the time-step to a sufficiently small value to get an accurate output.
If the simulator cannot do this it will look like it has got stuck at a particular point in time and will not complete
the simulation but also MAY NOT return an error. Newer versions of Simulink are far better at detecting and
overcoming this error so a simple case study is not easy to demonstrate.
However, it is easy to spot by looking at the simulation time of the simulation in SIMULINK (see Figure 3) and
observing that the time is not increasing even though the simulation appears to be running, OR you receive an
error message similar to the following:
Nonlinear iteration is not converging with step size reduced to hmin (X.XXXXXe-XXX) at time XX.XXXX. Try
reducing the minimum step size and/or relax the relative error tolerance.
The system you are simulating has an input that directly relies on the current/next time step’s output – Algebraic
Loop error
SIMULINK uses numerical solvers to find accurate but approximate solutions to differential equations. To do
this accurately for complex simulations it needs to determine the correct order of execution of each block in
SIMULINK.
You can see the order of execution by clicking on the “Execution Order” item in the “Information Overlays”
drop-down button in the “Debug” tab (Figure 4). Figure 5 shows a simple simulation with the execution order
displayed:
1. Integrator uses its initial condition as a starting point to determine the initial output
2. It displays the output on the scope window
3. The output is subtracted from the input at the initial timestep (t=0) to give the input for the integrator
for the next time step.
4. Next timestep starts and the integrator calculates its output at the new timestep based upon its
previous value and the new input.
5. Steps 2 to 4 repeat until the end of the simulation.
Hence you can see that the output of the integrator doesn’t depend directly on the current output value.
However, if you have a system where the input depends directly on its own output that doesn’t have a closed
form solution that SIMULINK can solve you will get an “Algebraic Loop” error (Figure 7). This can be observed
if we remove the integrator in the simulation shown in Figure 5 and change the subtract block to a sum block.
A way of removing the error message is to force SIMULINK to use the previous value of the output from the
block as its input using a “Memory” block which can be found in “Simulink/Discrete/Memory” in the SIMULINK
library.
NOTE: This will, in most cases, the simulation to run BUT will mean that the simulation will have an inaccuracy
resulting from this approximation. In most cases the time step will be small enough such that this doesn’t
not have a significant impact.
More subtle errors related to aliasing, signals crossing the zero axis (zero crossing)
The source of some errors in simulation will not be obvious but will result in signals that you do not
understand. These won’t normally be visible in functional models (or models that don’t use switches).
Normally, the source of these errors is Simulink using a timestep that results in switching events from
behavioural models or other non-linear events that SIMULINK will “miss out” if it uses an incorrect timestep
or a timestep that is too big.
A good example of this can be seen if we try and use a sawtooth waveform to generate a Pulse-width
modulated (or PWM signal).
Figure 9 shows a SIMULINK simulation in which two different methods are being used to generate a square,
pulse-width modulated waveform that could be used to drive a power electronics switch, for example. The
two methods are:
1. A square wave generator implemented using a “Simulink/Sources/Pulse Generator” block, which results
in a PWM waveform directly with a fixed duty cycle as the simulation runs (parameters shown in Figure
11,
Figure 12: PWM signals from two different PWM generation methods compared
Figure 16 shows the dialog that will appear if you click Figure 16: Model Settings/Configuration Parameters
on the “Model Settings” menu item in the “Modeling” tab in the Simulink window Dialogue
(or “Model Configuration
Parameters” menu item in the “Simulation:” menu in earlier versions of SIMULINK).
We have three ways to improve the quality:
1. Manually select a different solver
2. Increase the tolerance that SIMULINK uses to determine the time-step OR set a limit on the maximum
time-step SIMULINK can use
3. Use a fixed step solver and select and specify a fixed time-step.
We will have a look at using each in turn and the impact on the output.
• Click on the drop-down list to the right of the “Solver” and change the solver from “auto (Automatic Solver
Selection)” to “ode23s (stiff/Mod./Rosenbrock)”.
This selects a 2nd order implementation of the modified Rosenbrock numerical integration solver2.
USEFUL NOTE: This solver is labelled “Stiff” as it is better at solving problems that have fast dynamic
responses (such as those encountered in electric drives). Details on the various solvers can be found on “The
Mathworks” help page3. If you are experiencing difficulty getting a simulation to run correctly and you are
certain that the component parts are correct (and have been tested) then try selecting different “Stiff” solvers
and observe the similarities and differences in the response.
• Re-run the simulation and examine the response.
If the simulation didn’t run previously, it may do now, or if it did run previously, the output may be of a better
quality. Check this for your simulation.
Increase the tolerance that SIMULINK uses to determine the time-step
An alternative to using a different solver is to place a higher tolerance value on the accuracy of the
signals/states being simulated. This is likely to force the simulation to use a smaller time-step in the
simulation and hence require more processing power (to compute) and memory (to store the higher
resolution simulation). Hence, the simulation will slow down and may get slower as it progresses.
• Change the “solver” back to “auto (Automatic solver selection)”,
• Open “Model Settings” and change the value next to the “Relative Tolerance” label from 1e-3 to 1e-5,
• Re-run the simulation with these parameters
You should see that the output has the same basic shape but that the response is smoother now as the
simulation is placing a higher priority on simulation accuracy.
Changing the tolerance is a better strategy for improving the quality/accuracy of the response than changing
the “Max Step Size” (next section) as it still allows Simulink to make optimisations between time step size and
accuracy.
NOTE: Relative tolerance specifies the allowable tolerance relative to the size of the state being simulated
(between 0 and 1) whereas Absolute Tolerance specifies the absolute magnitude of the error as the value
approaches zero. A relative tolerance of 1e-3 = 0.1%.
Use a fixed step solver and select and specify a fixed time-step.
If all else fails and you are unable to use either of the above strategies to stabilise/improve your simulation,
you can force SIMULINK to use an integration solver that uses a fixed time-step to solve the integration
problem AND also (if required) specify the time-step that SIMULINK uses for its time step.
Note, that whilst this removes the requirement for SIMULINK to determine the simulation time-step and
hence will force a simulation to run, incorrect specification of the simulation time-step will result in a
simulation that runs but that returns erroneous results.
• Change the “Relative Tolerance” back to 1e-3.
• Change the “Max step size” from auto to 1e-7. This will force SIMULINK to run with a time step that can
only be smaller than 1e-7 seconds and may result in a long simulation (depending on your machine
performance). If your machine is older than 4 years old you may want to try with 1e-6 first.
• Re-run the simulation.
You should notice that the simulation takes longer to run but that the quality of the output on the scope is now
much improved. Try it and see!
2 Rosenbrock, H. H. "Some general implicit processes for the numerical solution of differential equations." The Computer Journal 5.4 (1963): 329-330.
3
https://uk.mathworks.com/help/simulink/gui/solver.html
If you are interested in your simulation performance and want to see what blocks and what parts of the
simulation are running slowest, you can use the tools in the “Debug” tab in the “Performance Advisor” icon.
Simulation timestep
variation during
simulation
Information on
how long each
block takes to
execute Detailed
information for
each block
Figure 18: Simulink profiler example for the state space simulation