MATLAB Answers

Eliminate algebraic loop without using memory block

13 views (last 30 days)
Boru Wang
Boru Wang on 14 Aug 2019
Commented: Boru Wang on 15 Aug 2019
I was wondering if there is a way to eliminate the algebraic loop in Simulink model without using memory or delay blocks. These blocks tend to add slight overheads in the generated code associated with the creation and use of "state" variables.


Sign in to comment.

Answers (2)

Robert U
Robert U on 14 Aug 2019
Hi Boru Wang,
there is an article about "Remove Algebraic Loops" in the Matlab documentation:
Without knowing the actual problem our help is more generic.
Kind regards,


Boru Wang
Boru Wang on 14 Aug 2019
Hi Robert,
Thank you very much for your help...and my apology for not able to elaborate more on the issue.
I hope the following example could help:
** The code and blocks shown below are modified because the original information are under non-disclosure**
I was trying to generate code for a moving average function. The function runs a counter and checks the counter result to determine actions: if the counter is still running, the function accumulates the input and store the result in "inputSum"; if the counter runs down to zero (indicated in the code by a flag "learnDone"), the function calculate out the moving average of the input based on "inputSum". I have to insert a delay block for the "learnDone" flag because its value depends on the counter result which is updated some steps later.
The code is fine except for the overhead marked in the picture below. If I am hand-writing the code, I could have got rid of either 'learnDone' or 'movingAvgDone' flag because they are redundant to each other.
I have learned that inserting delay block is a standard way in Simulink to avoid algebraic loop and data dependency error. However, this kind of overhead associated with delay block really gets me. I was wondering if there is anything I could do in Simulink to avoid it?Capture.PNG
Additional information: Simulink Configuration
Robert U
Robert U on 15 Aug 2019
Is there a possibility that the algebraic loop is caused by the C-code you posted? How do you initialize your static variable "learnDone"?
Boru Wang
Boru Wang on 15 Aug 2019
Hi Robert. There would be a loop/data dependency issue if I had NOT insert this delay block. I initialized the static variable to zero.

Sign in to comment.

Jim Riggs
Jim Riggs on 14 Aug 2019
Edited: Jim Riggs on 14 Aug 2019
Here is an interesting article on algebraic loops and memory blocks. (pertaining to continuous time models)


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by