Explanation for the below equation containing the comparsion symbols

1 次查看(过去 30 天)
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 个评论
Laxmi Akshaya Thela
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-9-6
编辑:Walter Roberson 2021-9-6
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by