Combine multiple if statements for something more compact

Friends,
I'm trying to refine my code.It works fine but I have four if conditions which I want to make more efficient. Is there an alternative way to do it?
i=1;
while (VMPH<=60)
% Vehicle speed
t(i+1) = t(i)+delt;
Vmps(i+1) = Vmps(i)+((delt*(Facc(i)))/Vm);
VMPH(i+1) = Vmps(i+1)/0.44704;
% Vehicle forces
Fr(i+1) = Fr(1);
Fd(i+1) = 0.5*Af*Cd*(Vmps(i+1))^2;
% Speed conditions
ig(i+1) = 3.78;
N(i+1) = Vmps(i+1)*io*ig(i+1)*60/(pi*Dt);
if N(i+1) > 2150
ig(i+1) = 2.06;
N(i+1) = Vmps(i+1)*io*ig(i+1)*60/(pi*Dt);
end
if N(i+1) > 2150
ig(i+1) = 1.58;
N(i+1) = Vmps(i+1)*io*ig(i+1)*60/(pi*Dt);
end
if N(i+1) > 2150
ig(i+1) = 1.21;
N(i+1) = Vmps(i+1)*io*ig(i+1)*60/(pi*Dt);
end
if N(i+1) > 2150
ig(i+1) = 0.82;
N(i+1) = Vmps(i+1)*io*ig(i+1)*60/(pi*Dt);
end
% Power and performance
Tao_b(i+1) = interp1(Speed,Torque,N(i+1));
Tao_w(i+1)= Tao_b(i+1)*io*ig(i+1)*etadrive;
Ft(i+1) = Tao_w(i+1)/Dt*2;
Pb(i+1) = 2*pi*Tao_b(i+1)*N(i+1)/60;
% Acceleration force
Facc(i+1) = Ft(i+1)-Fd(i+1)-Fr(i+1);
i=i+1;
end
Thank You!

10 件のコメント

Moritz
Moritz 2018 年 2 月 19 日
Why don't you just delete all but the last one? The rest doesn't matter anyhow as it is overwritten by the last if statement.
DIP
DIP 2018 年 2 月 19 日
編集済み: DIP 2018 年 2 月 19 日
Moritz, im modelling a gear box. Hence the last if statement is the final drive gear. The gear ratios go from 3.78 , 2.06, 1.58, 1.21, 0.82.
DIP
DIP 2018 年 2 月 19 日
Im pretty sure there is a better way to code this although im getting the intended result.
Birdman
Birdman 2018 年 2 月 19 日
Does i stands for ith gear?
DIP
DIP 2018 年 2 月 19 日
編集済み: DIP 2018 年 2 月 19 日
No, i is just the while loop counter. It is a 5 speed gear box - each time I hit a particular RPM, I switch gear. Hence 4 if loops for each gear shifted.
Basil C.
Basil C. 2018 年 2 月 19 日
Why do all the four IF statements have the same condition that
N(i+1)>2150
DIP
DIP 2018 年 2 月 19 日
編集済み: DIP 2018 年 2 月 19 日
Basil, Each time the engine hits 2150 rpm, the gear shifts one step higher. i am an inefficient coder, somehow this gives me the right answer. However, im looking to modify the code and make it simple.
M
M 2018 年 2 月 19 日
Do you ant to execute something while
N(i+1)>2150 ?
Something like
while N(i+1)>2150 && count<4
count=count+1;
your code here
end
DIP
DIP 2018 年 2 月 19 日
So i define a while loop within a while loop?
M
M 2018 年 2 月 21 日
Why not ?

サインインしてコメントする。

 採用された回答

Jeff Miller
Jeff Miller 2018 年 2 月 19 日

0 投票

Maybe something like this:
IGVals = [3.78 2.06 1.58 1.21 0.82];
CurrentIG = 1;
i=1;
while (VMPH<=60)
...
% Speed conditions
ig(i+1) = IGVals(CurrentIG);
N(i+1) = Vmps(i+1)*io*ig(i+1)*60/(pi*Dt);
if N(i+1) > 2150
CurrentIG = CurrentIG + 1;
ig(i+1) = IGVals(CurrentIG);
N(i+1) = Vmps(i+1)*io*ig(i+1)*60/(pi*Dt);
end
% Power and performance
...

1 件のコメント

DIP
DIP 2018 年 2 月 21 日
編集済み: DIP 2018 年 2 月 21 日
Jeff, wouldnt the line
ig(i+1) = IGVals(CurrentIG);
throw dimension mismatch error ? Edit : It does throw a index exceeds bounds error.

サインインしてコメントする。

その他の回答 (1 件)

Roger Stafford
Roger Stafford 2018 年 2 月 19 日
編集済み: Roger Stafford 2018 年 2 月 19 日

1 投票

You can replace the part of the code after "%Speed Conditions" but before "% Power and performance" by these lines:
t = Vmps(i+1)*io*60/(pi*Dt);
x = [3.78,2.06,1.58,1.21,0.82];
ig(i+1) = x(sum(2150<(x(1:4)*t))+1);
N(i+1) = ig(i+1)*t;
They should produce an equivalent result.

2 件のコメント

DIP
DIP 2018 年 2 月 21 日
編集済み: DIP 2018 年 2 月 21 日
Roger, i get an error Matrix dimensions must agree for the third line of your suggestion.
Roger Stafford
Roger Stafford 2018 年 2 月 22 日
@DIP: That line should work. The expression
sum(2150<(x(1:4)*t))+1
should provide an integer value ranging from 1 to 5. This in turn should be a valid index in the vector x. You can do some checking by writing
ix = sum(2150<(x(1:4)*t))+1;
disp(ix)
to display the values of ix. By the way, it is assumed that the variable t is a scalar. If not, you would probably get a similar error message at the ix calculation.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

DIP
2018 年 2 月 19 日

コメント済み:

2018 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by