how to construct if for vectors with positive and negative?

1 回表示 (過去 30 日間)
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 23 日
回答済み: Dennis 2018 年 10 月 24 日
Kraft=500:1000:1510;
Geschwindigkeit=10:50:160;
DrehzahlICE=1484
DrehmomentICE=50
DrehmomentAchsesoll= Kraft*0.324;
DrehmomentAchseist=DrehmomentICE*1.54*2.64;
Drehmomentbrauch=DrehmomentAchsesoll-DrehmomentAchseist;
idx2 =Drehmomentbrauch<0;
idx3 =Drehmomentbrauch>=0;
Drehmomentbrauch1=Drehmomentbrauch.*idx2;
Drehmomentbrauch2=Drehmomentbrauch.*idx3;
while Drehmomentbrauch1< -1 | Drehmomentbrauch2 > 1
if Drehmomentbrauch1 < -1
DrehmomentICE = DrehmomentICE -0.4;
DrehmomentAchseist=DrehmomentICE*1.54*2.64;
Drehmomentbrauch=DrehmomentAchsesoll-DrehmomentAchseist;
elseif Drehmomentbrauch2 > 1
DrehmomentICE = DrehmomentICE + 0.4;
DrehmomentAchseist=DrehmomentICE*1.54*2.64;
Drehmomentbrauch=DrehmomentAchsesoll-DrehmomentAchseist;
end
this code works perfectly for only negative or only positive values but if the vector contains both then it skips the loop
the problem can be seen in the current example
basically i want to calculate it to get a vector of DrehmomentICE at end

採用された回答

Dennis
Dennis 2018 年 10 月 24 日
Small example to understand what is happening:
Lets take a vector and check if all values are >0.
test=1:5>0
In this case test is a logical array containing only ones. A while loop will only work as long ALL conditions in your vector are true. This loop will execute only once:
while test
disp(test)
test(1)=0;
end
Therefor in your code your loop will only work with all values <-1 or all values >1. An easy fix would be to use any.
while any(test)
disp(test)
test(randi(5))=0;
end

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 10 月 23 日
Drehmomentbrauch1<= 0 & Drehmomentbrauch2 >= 0 %this condition is suffice I suppose

カテゴリ

Help Center および File ExchangeFortran with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by