Why Does This Definition of a Piecewise Function Produce a Warning Message?
1 回表示 (過去 30 日間)
古いコメントを表示
MATLAB issues a warning when defining the below piecewise function
syms y(x); y(x) = piecewise(x<1, x+1, 1<=x<2, x-1);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/879980/image.jpeg)
But when I write
y(x) = piecewise(x<1, x+1, (1<=x) & (x<2), x-1);
the warning message disappears. Could someone point to an example where those two ways of defining a piecewise function produce different results? It seems here that they give identical results.
0 件のコメント
採用された回答
Benjamin Thompson
2022 年 1 月 31 日
This is just a warning about good programming syntax. Good programming style is to never leave order of operations in doubt, and add parenthesis where needed to clarify order of operations. While a <= b <= c is good mathematical notation, it may no be clear to a compiler or interpreter how the expression must be evaluated.
3 件のコメント
Benjamin Thompson
2022 年 2 月 2 日
>> -3 <= -2 < -1
ans =
logical
0
>> (-3 <= -2) & (-2 < -1)
ans =
logical
1
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Scope Variables and Generate Names についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!