Explanation for the below equation containing the comparsion symbols

1 view (last 30 days)
I couldnt get these equations with comparison.
gbLoss = (fdTrq>0 & gbEff>eps) .* (1-gbEff) .* fdTrq ./ (gbEff .* gbSpRatio) ...
+ (fdTrq>0 & gbEff==eps) .* fdTrq .* gbSpRatio ...
+ (fdTrq<=0) .* (1-gbEff) .* fdTrq ./ gbSpRatio;
vehForce = (wheelSpd~=0) .* (rolling_friction + veh.aero_coeff.*w{1}.^2 + veh.mass.*w{2});
% Wheel torque (Nm)
wheelTrq = (vehForce .* veh.wh_radius + veh.axle_loss .* (wheelSpd~=0));
% Torque provided by engine
engTrq = (shaftSpd>0) .* (reqTrq>0) .* (1-u{2}).*reqTrq;
brakeTrq = (shaftSpd>0) .* (reqTrq<=0) .* (1-u{2}).*reqTrq;
% Torque provided by electric motor
emTrq = (shaftSpd>0) .* u{2} .* reqTrq;
  3 Comments
Laxmi Akshaya Thela
Laxmi Akshaya Thela on 5 Sep 2021
I want to know how to use the comparsion terms within the equatios without having to use them in the else if condition as done for the above code.
Usually we mention the while ,else if condition and if the conditio holds true then the statements in that would be executed.Instead of mentioning it in that way I want to mention the condition within the equations,as mentioned in the above code.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Sep 2021
Edited: Walter Roberson on 6 Sep 2021
When you are working with numeric values, then a comparison operator returns logical false or logical true. Logical false, in most circumstances, concerns to double 0.0 and logical true in most circumstances converts to double 1.0. Therefore you can impliment
%result = VALUE1 if CONDITION2, otherwise VALUE2 if CONDITION2, otherwise 0
result = piecewise(CONDITION1, VALUE1, CONDITION2, VALUE2, 0)
as
(CONDITION1) .* (VALUE1) + (~CONDITION1).*(CONDITION2) .* (VALUE2)
In cases where the conditions are mutually exclusive, this can often be simplified to
(CONDITION1) .* (VALUE1) + (CONDITION2) .* (VALUE2)
However, you have to be careful that the values never become infinity for the unselected case. For example you cannot do
(x ~= 0) .* (1/x) + (x == 0) .* 1
to use 1 for x == 0 and 1/x for other x. This is because if you write like that, then at 0 you would have (x ~= 0) .* (1/0) which would be 0 (false) * infinity which would give NaN for that term, not 0.

More Answers (0)

Categories

Find more on Scripts 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!