1
\$\begingroup\$

I knew how to set a a while loop in Mikrocontroller, but when I tried to Implement it in matlab /simulink, my computer gets stuck and never get out of the while loop and does not do anything. here is my code:

function [tout,valueP,valueI] = fcn(tin,Vout)

persistent dp;          %local variable
persistent di;          %local variable

if isempty(di)
    di=0;
end
if isempty(dp)
    dp=0;
end

while 1                 % infinite loop

    if tin==30          %at time t=30s, take Vout value
        if Vout==300    %during this time, compare Vout to 300V
            di=10;
            dp=40;
        else
            di=20;
            dp=10;
        end
    end

    if tin==60            % break the loop at time 60s
        break
    end
end

tout=tin;               % output
valueP=dp;
valueI=di;

my objective is: run the model, then watch the Vout and then at time 30s, take the value of Vout, then use it for comparaison that will result in vlaueP,valueI. here is my model as pic(could not add it) enter image description here

\$\endgroup\$
12
  • \$\begingroup\$ Yes, you are right \$\endgroup\$ – Yaakov Sep 24 '20 at 7:29
  • 1
    \$\begingroup\$ Use a fixed timestep for the simulation. \$\endgroup\$ – Srinivasan M P Sep 24 '20 at 11:40
  • 1
    \$\begingroup\$ What t variable in the line t==60 ? where is it defined ? What changes its value ? Your code ? \$\endgroup\$ – AJN Sep 24 '20 at 13:28
  • 1
    \$\begingroup\$ Why is there a loop in a code that will be repeatedly called by simulink. The loop is already written inside Simulink. Your code probably doesn't need its own loop. Just remove the loop and try again. \$\endgroup\$ – AJN Sep 24 '20 at 13:30
  • 1
    \$\begingroup\$ Alternately try changing the line t==60 to t>=60. That way, if Simulink skips over exact 60s, the loop will still break. \$\endgroup\$ – AJN Sep 24 '20 at 13:33
2
\$\begingroup\$

tin is an argument to your function, as a result, it cannot change value during a call to the function, and the condition for loop termination cannot be satisfied, unless it was from the very beginning.

You are going to have rethink the whole design of your program such that it can actually become aware of changes in the value of whatever tin represents.

Probably you want the while loop to wrap whatever obtains the value, if you then still use a function at all, you would call it once per iteration of the while loop (it's possible that however this is hooked up in your software package would already effectively do that by continuously re-executing your function - ie, the mistake may be having your own while loop at all)

The same concern would go for your other input, Vout as well.

I'd suggest you take a step back and find a simpler example of a function that plugs into this sort of simulation system, and take time to understand how it operates on time-varying inputs. Once you really understand how that works, you can re-implement the logic you need in a way compatible with this framework.

\$\endgroup\$
5
  • \$\begingroup\$ tin represent time, I wanted this function be aware of the time \$\endgroup\$ – Yaakov Sep 24 '20 at 14:44
  • 1
    \$\begingroup\$ Obviously you want it to respond to change over time, but the way you've written this it can only act on a single value of tin and Vout. You need to find yourself a simple, generic example of how to make a system which responds to a time varying input in this software, take time to understand how it works, and only then adapt it into something implementing the behavior you need. \$\endgroup\$ – Chris Stratton Sep 24 '20 at 14:46
  • \$\begingroup\$ now I tried it with if and only for using greater or smaller than it works. I did not understand what you mean by 'Probably you want the while loop to wrap whatever obtains the value' \$\endgroup\$ – Yaakov Sep 24 '20 at 14:50
  • 1
    \$\begingroup\$ If your function and your diagram are the entirety of your "program" then I suspect the simulation framework is already providing the "while loop to wrap whatever obtains the value" meaning you yourself do not need a while loop. \$\endgroup\$ – Chris Stratton Sep 24 '20 at 14:52
  • \$\begingroup\$ actually this is just a small part of the program that I wanted to implement , my objective was to have di and dp changing according to the voltage Vout when my program reach 3s. \$\endgroup\$ – Yaakov Sep 24 '20 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.