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}]

enter image description here


Plot3D[w[x, y], {x, -5, 5}, {y, -5, 5}, PlotRange -> {0, 400}]

enter image description here

It is not a problem with PlotRange.

What should I do to improve visualisation?

  • 2
    $\begingroup$ Your definition doesn't make sense. Is this really what you entered in Mathematica? $\endgroup$ – Sjoerd C. de Vries Nov 23 '14 at 16:08
  • $\begingroup$ Please copy all the actual code you used. Don't retype it, as you have clearly typos in the text (DenistyPlot instead of DensityPlot). $\endgroup$ – Sjoerd C. de Vries Nov 23 '14 at 16:16
  • $\begingroup$ Where are my typos? In[166]:= For[i = 1, i < 100, i++, w[x_, y_] = w[x, y] + HeavisidePi[x - i/100]] In[165]:= Plot3D[w[x, y], {x, -5, 5}, {y, -5, 5}] $\endgroup$ – JSB Nov 23 '14 at 16:18
  • $\begingroup$ See my comment above $\endgroup$ – Sjoerd C. de Vries Nov 23 '14 at 16:18
  • $\begingroup$ 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. $\endgroup$ – 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]

Mathematica graphics

or as a density plot:

     Table[{x, y, w[x, y]}, {x, -2, 2, .10001}, {y, -5, 5, .10001}], 1]]

Mathematica graphics

Alternatively, you could use UnitBox instead of HeavisidePi. This function is defined for $x=\pm\frac{1}{2}$.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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