The problem is that to find the discontinuities, Plot
internally calls
PiecewiseExpand[p3, Method -> {"OrderlessConditions" -> True}]
but with a time constraint of 0.2
seconds. (See PiecewiseExpand
for an explanation of the option.) Because of the somewhat complicated conditions on x
-- ok, inequalities in terms of E^x
may not seem that complicated to a good high school student, but Mathematica apparently thinks so -- it takes much, much more time than that. The time constraint 0.2
seems to be hard-coded, so we cannot easily approach the problem from that angle. A better way is to simplify the function first using Reduce
to get the inequalities in terms of x
:
PiecewiseExpand[p3, Method -> {"ConditionSimplifier" -> (Reduce[#, x, Reals] &)}]
Then we get
Plot[Evaluate@
PiecewiseExpand[p3, Method -> {"ConditionSimplifier" -> (Reduce[#, x, Reals] &)}],
{x, -3, 3}, AspectRatio -> Automatic,
PlotPoints -> 200, Exclusions -> All]

Evaluating the PiecewiseExpand
before passing it to Plot
, either using another variable to store the result or using Evaluate
as above, is necessary. Otherwise the internal call to PiecewiseExpand
will be made on the input PiecewiseExpand[p3,...]
, which will take longer than 0.2
seconds. In that case, the discontinuity processing fails and you get the annoying vertical lines.
Update:
The point of the option "OrderlessConditions" -> True
is the reduce the conditions to non-overlapping intervals. The irony is that p3
already satisfies that criterion. So we can mimic the internal processing of the discontinuities, which is faster than simplifying them with reduce:
excl = With[{p = p3[[1, All, 2]]},
Thread[
(LogicalExpand /@ p /.
{f_ <= g_ :> f - g, f_ < g_ :> f - g, f_ >= g_ :> g - f, f_ > g_ :> g - f,
And -> Max}) == 0
]
];
Plot[p3, {x, -3, 3}, AspectRatio -> Automatic, PlotPoints -> 200,
Exclusions -> excl]
The plot looks the same.