0
votes

I'm very new to Matlab Simulink and I'm trying to implement a loop in the Matlab Function Block that goes on non-stop during the whole simulation. Input:

"t" from a simple clock to make it go on until the simulation lasts.

"v" and "i" what gives a specific "p*" value, by changing the ouptut "D" a little bit I want to check if this "p*" value gets bigger or smaller (in the whole model "v" and "i" are influenced by "D")

function D = fcn(v,i,t)
%#codegen
p1 = v*i;
D = 0.5;
D = D + 0.05;

while t > 0

p2 = v*i;

if p1 > p2 
  D = D - 0.05;

else
  D = D + 0.05;

end;

p1 = p2;

end;

The Function Block says it is correct, but as I start to run it, it freezes. The next step I wanted to do (but never got there) is to put a little timer in it, to execute this loop lets say every 0.01 sec or something. Is that compliable with a Matlab Function Block or is there any way achieving this?

1
There no break, no T incrementation ?Vuwox
have you considered the If-action subsystem block and the While iterator subsystem blocks? They are very convenient to use.Robert Seifert
You're freezing because you have an infinite loop. At the first simulation time step when t > 0, when this block is called, it will never exit the while loop.Phil Goddard

1 Answers

0
votes

You probably want do do if t > 0 instead of while t > 0. Simulink already has the timer loop and it will call your function fcn at each time step t. You do not want to start your own loop inside that.