10
$\begingroup$

Bug introduced in 11.0 or earlier and fixed in 12.3.1 or earlier


I need to write this function piecewise and to make a graph for it, x[-3,3]:

Enter image description here

Enter image description here

f[x_] = Piecewise[{{1/(Cos[x^2 + 2]), x <= 0}, {Sqrt[x^2/(x + 1)],
0 < x <= 1}, {(x^2 - 2 x)/Abs[2 - x], True}}]
Plot[f[x], {x, -3, 3}]

Error:

Power::infy: Infinite expression 1/0 encountered.

Why does this happen, and how do I fix it?

$\endgroup$
9
  • 1
    $\begingroup$ People here generally like users to post code as Mathematica code instead of images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this this meta Q&A helpful $\endgroup$
    – Michael E2
    Jan 22, 2017 at 15:04
  • $\begingroup$ (1) Do you see the function going to infinity anywhere in your graph? And (2) Have you seen Quiet[]? $\endgroup$
    – Michael E2
    Jan 22, 2017 at 15:07
  • 1
    $\begingroup$ (Tip: use @MichaelE2 to make sure I'm notified.) Actually, I think it's more complicated than I first thought. It seems to be the Sqrt expression. I think x = -1. is being plugged into it. Why?, you might ask. Well, it does some symbolic analysis to determine discontinuities in the graph. It's probably a bug, but I haven't been able to track it down yet. You can use Quiet if it's annoying. $\endgroup$
    – Michael E2
    Jan 22, 2017 at 15:33
  • 1
    $\begingroup$ Anyone else agree it's a bug? $\endgroup$
    – Michael E2
    Jan 22, 2017 at 16:49
  • 1
    $\begingroup$ See (a136108) for further confirmation of the bug. $\endgroup$
    – Michael E2
    Jan 25, 2017 at 13:14

3 Answers 3

12
$\begingroup$

I think it's a bug in the symbolic analysis of function done to compute the discontinuities. It's a minor bug, in that it does not affect the result, but only allows in an internal test to come back to the user-level as an error message.

It is connected with the complexity of analyzing the expression with Sqrt. Here is a slightly simpler working example:

ClearAll[f];
f[x_] := Piecewise[{{1/(x + 2), x <= 0}, {Sqrt[1/(x + 1)], 0 < x <= 1}}, x];
Plot[f[x], {x, -3, 0}]

Power::infy: Infinite expression 1/0. encountered.

Mathematica graphics

ClearAll[g];
g[x_] := Piecewise[{{1/(x + 2), x <= 0}, {1/(x + 1), 0 < x <= 1}}, x];
Plot[g[x], {x, -3, 0}]

(* no error messages *)

Mathematica graphics

?NumericQ protection eliminates both symbolic analysis and error messages:

ClearAll[h];
h[x_?NumericQ] := Piecewise[{{1/(x + 2), x <= 0}, {Sqrt[1/(x + 1)], 0 < x <= 1}}, x];
Plot[h[x], {x, -3, 0}]

(* no error messages *)

Mathematica graphics

$\endgroup$
5
$\begingroup$

FullSimplify the function definition and cancellation will avoid the division by zero

f[x_] = Piecewise[{{1/(Cos[x^2 + 2]), x <= 0}, {Sqrt[x^2/(x + 1)], 
     0 < x <= 1}, {(x^2 - 2 x)/Abs[2 - x], True}}] // FullSimplify

enter image description here

Plot[f[x], {x, -3, 3}]

enter image description here

$\endgroup$
4
$\begingroup$

Related discussion is here. All of the clauses of conditional expressions are evaluated even if the conditions are false. It is a matter of taste to decide whether this is a bug or not. To me, it seems like a very strange feature, requiring additional processing.

We can see that if we look closer at the Michael E2's example:

ClearAll[f];
f[x_] := Piecewise[{{1/(x + 2), x <= 0}, {Sqrt[1/(x + 1)], 0 < x <= 1}}, x]
Plot[f[x], {x, -3, 0}]

enter image description here

Note the discontinuity of the upper curve at x = -1, which is an indication that MMA is evaluating all expressions of Piecewise before rendering.

We can wrap the function that causes this in Unevaluated to prevent such behavior.

ClearAll[f];
f[x_] := Piecewise[{{1/(x + 2), x <= 0}, {Unevaluated@Sqrt[1/(x + 1)], 0 < x <= 1}}, x]
Plot[f[x], {x, -3, 0}]

No errors

enter image description here

The same fix applies to the OP example:

f[x_] = Piecewise[{{1/(Cos[x^2 + 2]), x <= 0}, {Unevaluated[Sqrt[x^2/(x + 1)]],  0 < x <= 1}, {(x^2 - 2 x)/Abs[2 - x], True}}];
Plot[f[x], {x, -3, 3}]

No errors

enter image description here

What seems much more troublesome is the workaround that Bob Hanlon discovered and which does nothing more than taking 1 in the numerator out of the square root. In other words, there is still a possibility for the denominator to become 0 when evaluated unconditionally (at x == -1), so we should expect the same error, but for some reason there is no error now. This is a bug for sure, as it is at least inconsistent and therefore needs to be fixed.

g[x_] := Piecewise[{{1/(x + 2), x <= 0}, {1/Sqrt[(x + 1)], 0 < x <= 1}}, x]
Plot[g[x], {x, -3, 0}]

No errors!

For the sake of completeness, the mechanism that is probably glitching here is Exclusions, as Michael E2's example can also be fixed by explicitly indicating types of checks we want:

f[x_] := Piecewise[{{1/(x + 2), x <= 0}, {Sqrt[1/(x + 1)], 0 < x <= 1}}, x]
Plot[f[x], {x, -3, 0}, Exclusions -> "Singularities"]

No errors

This, however, wouldn't work nicely for the OP example, as it has more than one type of exclusions, so we need to use one of the alternative solutions in this post or thread.

$\endgroup$
1
  • $\begingroup$ Nice commentary, but there is a bug in Plot to the effect that the values of the independent variable are only in machine precision, which can cause errors in those cases in which machine precision causes a divide by zero error, whereas extended precision would not, however, WorkingPrecision does not effect the machine precision x-value selection, so an error occurs. As this is slightly different from the above, I posted it separately mathematica.stackexchange.com/questions/274676/… $\endgroup$
    – Carl
    Oct 13, 2022 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.