# Explanation for the below equation containing the comparsion symbols

조회 수: 3 (최근 30일)
Laxmi Akshaya Thela 2021년 9월 5일
편집: Walter Roberson 2021년 9월 6일
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이전 댓글 1개 표시이전 댓글 1개 숨기기
Laxmi Akshaya Thela 2021년 9월 5일
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.
Laxmi Akshaya Thela 2021년 9월 5일
There is no error in the code.

댓글을 달려면 로그인하십시오.

### 채택된 답변

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.

댓글을 달려면 로그인하십시오.

### 카테고리

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