Why Does This Definition of a Piecewise Function Produce a Warning Message?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kareem Elgindy
el 31 de En. de 2022
Comentada: Kareem Elgindy
el 2 de Feb. de 2022
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);
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 comentarios
Respuesta aceptada
Benjamin Thompson
el 31 de En. de 2022
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 comentarios
Benjamin Thompson
el 2 de Feb. de 2022
>> -3 <= -2 < -1
ans =
logical
0
>> (-3 <= -2) & (-2 < -1)
ans =
logical
1
Más respuestas (0)
Ver también
Categorías
Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!