3

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

roflmaostc
  • 121
  • 6

2 Answers2

1

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])
*)
halirutan
  • 4,281
  • 18
  • 44
0

With all due respect to @halirutan, by itself MaxRecursion set to 6 was not enough in the following plot to adjust the peak values of a piecewise function to be a monotonic increasing sequence: enter image description here This improved, when, in addition, I set PlotPoints to 240, as follows. enter image description here However, that does not completely solve all the display problems. For example, note in the plots above, that the initial value y = 0, is not correctly plotted as a blue vertical line despite use of Exclusions->None. Moreover, the grid lines, which are Dotted, do not display as dots, but as dashes which run off below the x-axis. All of these problems can be solved, probably more efficiently, by generating the points or dots as list data and using ListPlot or, as in this case ListLogPlot and using Joined->True when appropriate. That is low level solution, but is needed in more complicated plots to obtain a plot with shorter run time and more accessible control over the display features.

Carl
  • 101
  • 4