How to enable function is condition is true and ignore its updates till another condition is true

3 ビュー (過去 30 日間)
i need to write this code if input1-input2>5 if condition is true, the output should equal to zero even when input1-input2<5 till input1=input3 then it equal 1 and return to if condition again. in other words if input1-input2>5 is true its update will be ignored till input1=input3

回答 (1 件)

Bob Thompson
Bob Thompson 2018 年 3 月 6 日
output = 0;
if input1 == input3;
output = 1;
if input1-input2 > 5;
output = 0;
end
end
You can precondition the output however you want, but by nesting the input1-input2 condition inside of the input1=input3 condition it prevents the condition from being considered until input1=input3.
Alternatively, if you want the condition to be based on the output something like the following could work.
if output == 0;
if input1 == input3;
output = 1;
end
elseif output == 1;
if input1 - input2 > 5;
output = 0;
end
end
This will check for the output condition specifically and ignore the inputs unless the output is a certain value. Fair warning that in both of these conditions the output only updates once per loop, assuming you have a loop, and so will not check for input1=input3 and then input1-input2>5. You could copy the input1input2 if condition into the first output=0 condition (following the input1input3 condition) if you needed it to be considered in the same loop.
  4 件のコメント
rowida said
rowida said 2018 年 3 月 6 日
the simulink didnt work with while, it is variable step.
Bob Thompson
Bob Thompson 2018 年 3 月 6 日
You're going to need to run some kind of variable loop inside your if statement if you want to consider multiple situations where your variables do or don't meet a condition. If you would be willing to post some of your code that you are working with I might be able to help more.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMultibody Modeling についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by