Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica piecewise function bad plot rendering

I wanted to plot a user-defined Piecewise function (pagoda function) in Mathematica 10.2. It seems straightforward to me unfortunately the easy command leads to a bad result.

My first approach was:

f[x_] := Piecewise[{{0, x <= -1}, {-Abs[x] + 1, -1 < x < 1}, {0, 
x >= 1}}]

Plot3D[ 5*f[x]*f[y], {x, -1.5, 1.5}, {y, -1.5, 1.5}]

I also tried to set MaxRecursion which lead to more terrible results in a few cases (e.g. 2,3).

Can anybody tell me how to plot this function in a smooth nice way?

Thanks,

Felix

Example image from Mathematica 10.2

like image 307
roflmaostc Avatar asked Oct 23 '25 15:10

roflmaostc


1 Answers

As far as I can remember, making visible gaps was introduced as a feature. Before that, piecewise or discontinuous functions were plotted like this:

Plot[Piecewise[{{x, x <= 1}, {3, x > 1}}], {x, 0, 3}, Exclusions -> None]

Mathematica graphics

That behavior gives the wrong impression. I would have to check when this was default or if I'm completely off here. Anyway, as already noted in the comments, you can use the Exclusions option to get connected graphs.

You don't need to increase PlotPoints because Mathematica will (hopefully always) recognize the boundaries of the pieces as places where it needs to recursively increase points. Therefore, the MaxRecursion option is far more important to give a smooth plot. This example was rendered with only 10 points, but a recursion value of 5:

Mathematica graphics

Therefore, your function renders extremely well even with 10 plot-points when the recursion is high enough. Look how many subdivisions you get on the cracks

Plot3D[5*f[x]*f[y], {x, -1.5, 1.5}, {y, -1.5, 1.5}, PlotRange -> All, 
 Exclusions -> None, PlotPoints -> 10, MaxRecursion -> 6, Mesh -> All]

Mathematica graphics Mathematica graphics

Finally, note that the gaps are not restricted to Piecewise functions. As you can verify yourself, UnitStep will also show gaps. You can try it with your example by using an undocumented function to turn everything to UnitStep:

Simplify`PWToUnitStep[5*f[x]*f[y]]
(*
 5 (1 - Abs[x]) (1 - Abs[y]) (1 - UnitStep[-1 - x]) (1 - 
   UnitStep[-1 + x]) (1 - UnitStep[-1 - y]) (1 - UnitStep[-1 + y])
*)
like image 162
halirutan Avatar answered Oct 26 '25 11:10

halirutan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!