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
댓글 수: 0
채택된 답변
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
댓글 수: 0
추가 답변 (1개)
madhan ravi
2018년 10월 23일
Drehmomentbrauch1<= 0 & Drehmomentbrauch2 >= 0 %this condition is suffice I suppose
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!