need help with this symbol
조회 수: 5 (최근 30일)
이전 댓글 표시
r_gear = [17 9.6 6.3 4.6 3.7 3.5];
Tv = (144 + 0.48.*inp.W{1}.^2 + 1800.*inp.W{2}) .* 0.3;
and w{3} is a gear number vector
w{1} is a speed vector
w{2} is a acceleration vector
Tg = (inp.W{3}>0) .* (Tv>0) .* Tv ./ r_gear(inp.W{3} + (inp.W{3}==0)) ./ 0.95...
+ (inp.W{3}>0) .* (Tv<=0) .* Tv ./ r_gear(inp.W{3} + (inp.W{3}==0)) .* 0.95;
i don't know what this mathematical symbol means
(inp.W{3}>0) .* (Tv>0) .* Tv ./ r_gear(inp.W{3} + (inp.W{3}==0))
does it mean every value from W{3} must be a positive? and what happen if the value is a negative?
Any help would be appreciate thank you.....
i'm using m.file from ETH Zurich with qss toolbox
댓글 수: 0
채택된 답변
Walter Roberson
2019년 5월 9일
In MATLAB, comparisons return logical values, false and true, which are nearly always convertable to numeric values 0 and 1.
If you wanted to express a piecewise condition,
first_condition -> first_formula
second_condition -> second_formula
third_condition -> third_formula
then there is a "trick" of expressing it as
first_condition .* first_formula + second_condition .* second_formula + third_condition .* third_formula
Thus, (inp.W{3}>0) .* (Tv>0) .* Expression1 + (inp.W{3}>0) .* (Tv<=0) .* Expression2 can be mentally translated as,
in the positions where inp.W{3}>0 and Tv>0 are both true, then the result is Expression1
in the positions where inp.W{3}>0 and Tv<=0 are both true, then the result is Expression2
This trick has a small catch: it only works in cases where the formula does not give NaN or inf for the positions where the condition is false. For example,
(x ~= 0) .* (1/x) + (x == 0) .* ones(size(x))
looks like it should express
x non 0 -> 1/x
x is 0 -> 1
but it doesn't do so. Both sides of the .* are calculated, so in the places where x is 0, then you calculate
(0 ~= 0) .* (1/0)
%which is
0 .* inf
which is nan rather than being 0. But in cases where the formula is never inf or nan, you can do fine:
(x ~= 0) .* (1/(x+1)) + (x == 0) .* ones(size(x))
is fine, and expresses
x non 0 -> 1/(x+1)
x is 0 -> 1
댓글 수: 3
Walter Roberson
2019년 5월 10일
inp.W{3} extracts the third cell array entry from the field named W of the structure or object named inp. == 0 compares that to 0, returning an array of logical false and logical true values that is the same size as inp.W{3} is. When used in a mathematical operation such as +, the false get converted to 0 and the true get converted to 1, so the part after the + is an array of 0 and 1. That array is then added to the inp.W{3} array.
The equivalent of this is:
keep inp.W{3} the same in each case where inp.W{3} is not 0
add 1 to inp.W{3} in the case where inp.W{3} is 0
in other words, use 1 as the subscript where the subscript would have been 0, and otherwise use the original value.
Now, because subscripts can only validly be positive integers, never negative integers, there is another way to express this:
r_gear( max(inp.W{3}, 1) )
max(0,1) is 1. max(1,1) is 1. max(2,1) is 2. So this max() expression has the effect of replacing 0 with 1 and otherwise leaving the value as-is.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!