Algebraic Loop Caused by MATLAB Function Block. How to Resolve Without Time Delay?

Hello, I have a block diagram of a closed loop control system that uses a MATLAB Function Block to describe a plant model. In this function block I use MEX functions to acquire data from another software, so that I can generate outputs for the function block. I also use memory blocks to create persistent variables that are inputs and outputs from the function block. Because I am using some of the outputs as feedback in the control loop, it creates algebraic loops. To resolve this I was using delay blocks and memory blocks with initial conditions, but this shifts the signal by one time step which is undesirable.
My sim is a fixed time step of 1 second.
I tried to use the IC block as suggested from another post and a Mathworks page, but this doesn't resolve the algebraic loop for some reason. If I simply ignore the algebraic loop warnings, I get the desired response, but I am unsure if this has negative impacts on my simulation, such as step computation time, incorrect output values, etc.

1 个评论

Adding an IC block doesn't remove an algebraic loop. The IC block is intended to help the algebraic loop solver solve the algebraic loop.

请先登录,再进行评论。

 采纳的回答

If you suspect your MATLAB Function block or your MEX function caused the algebraic loop, consider setting this "Direct Feedthrough" flag false and try again.
This should only be done when it's realy hard to analyze where is the algebraic loop. Wether there is a direct feedthrough thus causing algebraic loop is a fact. It is not something that can be resolved by setting a flag. Inserting a delay is the only solution.
"If I simply ignore the algebraic loop warnings, I get the desired response, but I am unsure if this has negative impacts on my simulation, such as step computation time, incorrect output values, etc."
There shouldn't be any significant negative impacts. In fact, I think somewhere in the diagnostics you can set to not report this algebraic loop warning.
Simulink will automatically insert delays behind the scene to resolve the algebraic loop. If you get the desired response, then take it. You got different results when you add delays or memory blocks by yourself. It only means you added more delays or the delays are not added at the right place.

7 个评论

@Fangjun Jiang Thank you for the reply. How would I set this flag to flase for the MATLAB Function block? I saw in other mathworks pages they said to uncheck the "Allow Direct Feedthrough" in the function block properties, but I can't see that as an option in the block properties.
What is the basis for this statement: "Simulink will automatically insert delays behind the scene to resolve the algebraic loop."
This doc page indicates that Simulink tries to solve the loop explicitly through iteration, not by adding invisible delays. This iteration can slow down model execution.
@Paul, I remember sometimes seeing message popup by Simulink, para-phrase like "Simulink detects an algebraic loop in ..., a delay has been automatically added in ...". That is what I meant.
You brought up a good document reference on this topic. This could be relavant and helpful to the OP's problem. Keep in mind though, this "iteration" approach only applies to this "artifical" or "fake" algebraic loop. In that case, setting the "direct feedthrough" flag to be false could eliminate the fake algebraic loop thus remove the need for "iteration".
In a true algebraic loop, there is no solution to break the loop other than inserting a delay somewhere. Simulink might do a better job inserting the dealy at the right place than the user.
I've never seen that message about "a delay has been automatically added in," though it's been a long time since I've played with algebraic loops. Maybe it's a model setting or something, but I can't find anything on point in the documentation.
I disagree that the iterative, algebraic loop solver only applies to artificial algebraic loops. The linked doc page section does not say that. In fact, the linked doc page section discusses how the iterative solver solves a simple algebraic loop with feedback to a sum block. That example is not an artificial algebraic loop.
This doc page section talks about how Simulink can potentially eliminate artificial algebraic loops (and other concepts related to artificial algebraic loops).
@Paul, thanks for the comment. I definitely experienced the cases myself where delay bocks were added automatically by Simulink to resolve the algebraic loops.
I read the doc page again and it furthered my understanding of algebraic loops. Thanks!
I think there is a difference between us regarding the intepretation of "solve the algebraic loop".
In the "Sum block" example you mentioned, there is a loop, and there is a theoritically correct solution (Xa(t)=u(t)/2) for it. Simulink is able to find the solution through iteration. That is amazing. I never realized it. Notablly, it emphasizes that "The solver performs iterations to determine the solution to the algebraic constraint, if there is one."
When I mentioned it here, or what the OP is asking for here regarding "solve/resolve the algebraic loop" problem, is to "break the loop" or "remove the loop". For that, we are tying to set the flags, add the delays in the right place, etc. It is close to the first example described on this page.
I wasn't trying to answer the OP's question, just commenting on other comments in the thread.
In summary, I think we are now in agreement on almost everything.
Simulink models can contain two types of algebraic loops: true and artificial.
Either type of algebaic loop can be removed. The speciific options for the user for doing so depend on the type of algebraic loop and the blocks contained in the loop.
If not removed, Simulink tries to solve either type of algebraic loop during execution using an iterative solver.
I think our only area of disagreement is if there are any circumstances under which Simulink will automatically add delay (or memory?) blocks to remove an algebraic loop. I've searched the 2021b doc and can't find anything about that. If you come across that in the future and remember this thread, I hope you'll come back here and provide more details, example, etc.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by