2017-02-04 6 views
0
function [Li_current] = light_influent [SimulationTime] 
switch flag_light 
     case 0 
      %Light intensity is set to a constant value for the whole simulation 
      Li_current= pKt.Li 
     case 1 
      %Light intensity periodically alternates between 'on' at a set value or 'off' 
      get_param (MyModel, SimulationTime) 
+1

私はあなたのコードを読むことができるように編集しましたが、あなたが何をしようとしているのかを説明するために、シミュレーションによって何を意味し、最小限の例を提供するのですか? – bla

+0

ねえ、ありがとう!小さな例を使って説明するのは難しいですが、私はいくつかのパラメータに基づいて反応速度のいくつかの異なる速度方程式をモデリングするsimulinkでシミュレーションを実行しています。そのうちの1つは反応混合物に入る光です。私ができることをしたいのは、光が入ってくるいくつかの構成があるからです。構成の「モード0」を使用すると、シミュレーション時間全体にわたって一定の値でライトが点灯します。私が選ぶように、オフになる光のための「モード1」を作成し、シミュレーション内で –

+0

を定期的に作成します。それで、200時間のシミュレーション時間内に、光を12時間オンにしてから12時間オフにしたい、またはライトが16時間オンになってから8時間オフにしたいとしましょう。 –

答えて

0

ここでは、永続変数を使用するのが基本です。ライトがオンかオフかに関係なく、毎秒ライトのオンとオフを切り替える最小の実例があります。

function [Li_current] = light_influent(SimulationTime) 

% Declare and initialize the persistent variable during the first run 
persistent lastTime 
persistent lightOn 
if isempty(lastTime) 
    lastTime = SimulationTime; 
end 
if isempty(lightOn) 
    % Start with the light off 
    lightOn = false; 
end 
disp([ lastTime, SimulationTime, lightOn ]) 

% Declared here for testing purposes... 
flag_light = 1; 
switch flag_light 
case 0 
    %Light intensity is set to a constant value for the whole simulation 
    Li_current = 1; 
case 1 
    %Light intensity periodically alternates between 'on' at a set value or 'off' 
    % Switch every one second. 
    switchFrequency = 1; 
    if (SimulationTime - lastTime) > switchFrequency 
     % If the light is on, turn it off. If off, turn it on 
     lightOn = ~lightOn; 
     lastTime = SimulationTime; 
     disp('switched') 
    end 
    if lightOn 
     Li_current = 1; 
    else 
     Li_current = 0; 
    end 
end 
+0

これは私の愚か者。このアプリケーションには永続変数ではなく、モジュロ関数を使用してください –

関連する問題