how to construct if for vectors with positive and negative?

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by