how to construct if for vectors with positive and negative?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Shubham Mohan Tatpalliwar
el 23 de Oct. de 2018
Respondida: Dennis
el 24 de Oct. de 2018
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 comentarios
Respuesta aceptada
Dennis
el 24 de Oct. de 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
0 comentarios
Más respuestas (1)
madhan ravi
el 23 de Oct. de 2018
Drehmomentbrauch1<= 0 & Drehmomentbrauch2 >= 0 %this condition is suffice I suppose
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!