how to construct if for vectors with positive and negative?

1 view (last 30 days)
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

Accepted Answer

Dennis
Dennis on 24 Oct 2018
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

More Answers (1)

madhan ravi
madhan ravi on 23 Oct 2018
Drehmomentbrauch1<= 0 & Drehmomentbrauch2 >= 0 %this condition is suffice I suppose

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!