0% found this document useful (0 votes)
2 views

Helps Sheet 01 Simulink and Simscape Debugging and Tips

This document provides a comprehensive guide for students using Simulink and Simscape, focusing on debugging and improving simulation performance. It outlines common simulation errors, such as instability, convergence issues, and algebraic loops, along with strategies to resolve these problems. Additionally, it offers tips for enhancing simulation accuracy, including solver selection and tolerance adjustments.

Uploaded by

samuelajones
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Helps Sheet 01 Simulink and Simscape Debugging and Tips

This document provides a comprehensive guide for students using Simulink and Simscape, focusing on debugging and improving simulation performance. It outlines common simulation errors, such as instability, convergence issues, and algebraic loops, along with strategies to resolve these problems. Additionally, it offers tips for enhancing simulation accuracy, including solver selection and tolerance adjustments.

Uploaded by

samuelajones
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of Bristol

HS01
School of Electrical, Electronic and Mechanical Engineering

Electro-mechanical Design and Manufacture Principles

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

A simulation can fail for a number of reasons:


1. The system that you are simulating is unstable (e.g. system with pole that has positive real parts) - Non-
finite derivative error
2. The simulator can’t set a time-step small enough or run accurately enough to simulate your system
accurately – Convergence error
3. The system you are simulating has an algebraic loop (an input that directly relies on the current/next
time step’s output) – Algebraic Loop error
4. More subtle errors related to aliasing, signals crossing the zero axis (zero crossing)
Let’s look at each of these in turn with an example.
Simulating a system that is unstable - Non-finite derivative error

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

EENG30013 - Simulink and Simscape Debugging and Tips HS01-1


ISSUE 01, REVISION 2
Exponential, unstable
response with
(a) Simulated system increasing gradient over
time

(b) Simulated response

Figure 1: Simulating an unstable system

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.

Error message that confirms


this suspicion

Clues that our simulation has


failed as it is trying to simulate an
unstable system

2. SIMULINK Error message


1. Simulated Response

Figure 2: Simulating an unstable system – simulation fails

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)

EENG30013 - Simulink and Simscape Debugging and Tips HS01-2


ISSUE 01, REVISION 2
The simulator can’t set a time-step small enough or run accurately enough to simulate your system
accurately - Convergence error

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.

You can overcome this, either by:


1. changing the tolerance of the simulated signals
(see section 0)
2. changing the maximum time step for the
simulation (may result in simulation inaccuracy,
see section 0) Look here
3. selecting a different solver or using a fixed time
step (see section 0)
4. Identifying the block causing the error and Figure 3: Using the simulation time to spot convergence
implementing the function you want in a different error
way

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.

EENG30013 - Simulink and Simscape Debugging and Tips HS01-3


ISSUE 01, REVISION 2
Select
this Figure 5: Order of execution for simple system
option

Figure 4: Displaying order of execution

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.

Figure 6: System that returns


Figure 8: Measure to remove
algebraic error
algebraic error

Figure 7: Error message for algebraic loops

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,

EENG30013 - Simulink and Simscape Debugging and Tips HS01-4


ISSUE 01, REVISION 2
2. A sawtooth waveform implemented using a “Simulink/Sources/Repeating Sequence” block, whose output
is compared to a constant and results in a 1 if the waveform is greater than the constant and can be
used to generate a duty cycle that varies during a simulation (parameters shown in Figure 10).
Both blocks should generate a PWM signal (0=off, 1=on) with a 60% duty cycle and a period of 100ms (or
switching frequency of 10kHz). If we run these together we can see that there is a problem (Figure 12).
We can see that the output from the fixed PWM block is fine, but that the output from the variable duty PWM
block is missing some elements. This is due to the fact that the time-step selection that SIMULINK is
performing is “missing” the zero-crossing required to do the comparison between the output of the repeating
sequence block and the constant used to specify the duty cycle. It would result in a output waveform if used
to drive a power electronic switched converter that would not make sense and have incorrect values.

Figure 9: Generating a PWM signal in two different ways

Figure 11: Parameters used in square wave block to


Figure 10: Parameters used in repeating sequence block
generate fixed duty cycle PWM signal
to generate variable duty cycle PWM signal

1. Output signals zoomed to 10ms 2. Output signals zoomed to 1ms

Figure 12: PWM signals from two different PWM generation methods compared

EENG30013 - Simulink and Simscape Debugging and Tips HS01-5


ISSUE 01, REVISION 2
In order to resolve this issue:
1. Build up your blocks in small steps and test each one to check that they do what you expect for simple
conditions (unit testing)
2. Try out different variable step solvers to see if this fixes the issue (see section 0)
3. Use a fixed step solver with a timestep that is at least 10x smaller than the switching period to ensure
that it is captured correctly. (see section 0)
4. Try implementing the block that is not working correctly in a different way
None of the methods for resolving the issue present are guaranteed to work and so unit testing (testing each
small element of your simulation) becomes a critical part of your simulation workflow.
In the case presented, the problem can be fixed by either:
• switching to a fixed timestep solver (ode4) and specifying a timestep of 1e-5 (section 0)
• implementing the “PWM Generator” block using the built in “Simulink/Simscape/Specialized Power
Systems/Fundamental Blocks/Power Electronics/Pulse & Signal Generators/PWM Generator (DC-DC)” block and
using the parameters shown in

Figure 13: Corrected PWM


Generator
Figure 14: PWM Generator
Figure 15: PWM outputs compared
Parameters

How to improve the quality/accuracy of the simulation

If you look at the signal in Error! Reference source not


found., you may notice the discretisation of the signals
in the time axis (at 𝑡 = 0.0102, 0.0103, 0.0104, etc).
Also, some of the issues listed above indicate that
SIMULINK’s ability to automatically select the right
solver and timestep is not always correct and may not
take into account specification aspects of a simulation
that are important.
If this happens you need to change these manually. This
is a process that will require a bit of trial-and-error to get
right and require you to test your simulation (and its Parameters that we might
various parts) in a methodical manner. change

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.

EENG30013 - Simulink and Simscape Debugging and Tips HS01-6


ISSUE 01, REVISION 2
Manually select a different solver

• 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

EENG30013 - Simulink and Simscape Debugging and Tips HS01-7


ISSUE 01, REVISION 2
Profiling your simulation (or checking its timing and performance)

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

Figure 17: Solver Profiler example for state space simulation

The two most useful are:


1. The “Solver Profiler” (Figure 17) which gives information on how the solver has chosen the time step
over the simulation run – useful for manually selecting solvers and timesteps
2. The “Simulink Profiler” (Figure 18) which allows you to how CPU time the simulation has taken and how
much time each block consumes – useful for optimising model structure

Information on
how long each
block takes to
execute Detailed
information for
each block

Figure 18: Simulink profiler example for the state space simulation

EENG30013 - Simulink and Simscape Debugging and Tips HS01-8


ISSUE 01, REVISION 2

You might also like