mardi 15 janvier 2019

Persistent variable in angebric loop Simulink

I have a closed loop in simulink that looks as follows:

enter image description here

The block Tgo (marked in red) is matlab function that basically calculates polynomial roots and returns the largest real root and looks as following:

function Tgo = fcn(g,r,v,vw,rho_u,rho,a,cd,m)

persistent Tgo2
Real_tgo = [0 + 0i,0 + 0i,0 + 0i,0 + 0i];

more lines for polynomial...

rr = roots(poly);

for k = 1:4
    if abs(imag(rr(k))) < 0.001
        Real_tgo(k) = rr(k);
    else
        Real_tgo(k) = 0;
    end
end

Tgo2 = real(max(Real_tgo));

Tgo = Tgo2;

At this block I had to use persistent variable since it would not let me get the real answer. (It kept saying that I need to determine the type and size of the output and only persistent variable solved it for me).

However, when I would like to use a closed loop using if block/ if action subsystem I get the following error:

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.

So basically from what I understand there can be two solutions:

1) Someway to avoid using persistent variable and still to get a value from Tgo block which wont be the one I had to pre-define.

2) To make if condition in some other way that will allow persistent variable.

Both of them I tried few times without success.

Any help would be appreciated.

Thank you.

Aucun commentaire:

Enregistrer un commentaire