# Problem with DensityPlot Plot3d of Heavisde function

I have problem with heavisde pi or theta function. When I plot them densly i.e. w[x,y] like this

w[x_, y_] = Sum[HeavisidePi[x - i/100], {i, 1, 200}];


I get

DensityPlot[w[x, y], {x, -30, 10}, {y, -45, 45},  PlotRange -> {0, 1000}] or

Plot3D[w[x, y], {x, -5, 5}, {y, -5, 5}, PlotRange -> {0, 400}] It is not a problem with PlotRange.

What should I do to improve visualisation?

• Your definition doesn't make sense. Is this really what you entered in Mathematica? – Sjoerd C. de Vries Nov 23 '14 at 16:08
• Please copy all the actual code you used. Don't retype it, as you have clearly typos in the text (DenistyPlot instead of DensityPlot). – Sjoerd C. de Vries Nov 23 '14 at 16:16
• Where are my typos? In:= For[i = 1, i < 100, i++, w[x_, y_] = w[x, y] + HeavisidePi[x - i/100]] In:= Plot3D[w[x, y], {x, -5, 5}, {y, -5, 5}] – JSB Nov 23 '14 at 16:18
• See my comment above – Sjoerd C. de Vries Nov 23 '14 at 16:18
• I can not show you in comment that this definition works (too long output), but it makes sense. DensityPlot is a clear error, sorry, in Mathematica it is correct. – JSB Nov 23 '14 at 16:20

One problem is that HeavisidePi[x] is undefined for $x=\pm\frac{1}{2}$, and a second one is that Mathematica's function plot routines often do not like discrete functions. The following code tackles both problems (the odd step size takes care of the undefined positions):

DiscretePlot3D[w[x, y], {x, -2, 2, .10001}, {y, -5, 5, .10001},
PlotRange -> All, Filling -> None, ExtentSize -> Full] or as a density plot:

ListDensityPlot[
Flatten[
Table[{x, y, w[x, y]}, {x, -2, 2, .10001}, {y, -5, 5, .10001}], 1]] Alternatively, you could use UnitBox instead of HeavisidePi. This function is defined for $x=\pm\frac{1}{2}$.