How do I resolve the algebraic loop error in my model with Stateflow charts and Simscape blocks?

308 views (last 30 days)
I have a model to simulate a control system, and it includes a control subsystem and a physical model (plant) subsystem. I am using Simscape blocks in my physical model subsystem. I would like to use a Stateflow chart in my control subsystem, but once I add the Stateflow chart, I am getting the following algebraic loop error. How can I resolve this error?
ERROR: Error:'Model/Control Subsystem/Chart' updates persistent or state variables while computing outputs, therefore it cannot be used in an algebraic loop. However, it is in a loop with the following blocks. For more information, see Algebraic Loops with MATLAB Function and Stateflow Blocks.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Nov 2023
Edited: MathWorks Support Team on 17 Nov 2023
When a Simscape network has direct feedthrough, it creates an algebraic loop. Typically, the Simulink algebraic loop solver can solve these models without an error. However, the algebraic loop solver cannot solve algebraic loops that contain Stateflow charts. In this case, the Stateflow chart is involved in the algebraic loop.
For a system with discrete signals:
You could work around this error by introducing a delay to the outputs of the Stateflow chart with the "memory" block. The memory block outputs the input from the previous time set. This single-step delay breaks the algebraic loop, and eliminates the source of the error. To read more about the memory block, see the documentation page linked below:
Or, run below command in MATLAB 2017a for documentation:
web(fullfile(docroot,'simulink/slref/memory.html'))
It is worth noting that adding a delay to many signals could cause the simulation to slow down significantly and affect the results. Therefore, it is best to only add delay to the signals causing the algebraic loop. In this case, that means only putting a memory block after the outputs of the Stateflow chart, and not in the larger feedback signal from the physical model subsystem to the control subsystem.
For a system with continuous signals:
As recommended in the following article, do not break the loop with a memory or delay block.
This will introduce discrete signals into an otherwise continuous system, which can increase the potential for incorrect results as well as slow simulation speeds. Instead, use a simple first-order transfer function with a small time constant. The following documentation example also demonstrates this workaround (see the Cycle Manifold subsystem). 

More Answers (0)

Categories

Find more on Complex Logic in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!