Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: Plotting piecewise functions

  1. May 13, 2010 #1
    Hi guys

    Take a look at

    Code (Text):
    DensityPlot[If[x == 0 && y == 0, 1, 0], {x, -1, 1}, {y, -1, 1}]
    This just gives me a uniform plot. Why does the peak at the origin not show up?

  2. jcsd
  3. May 13, 2010 #2
    There are two main reasons why you'll have trouble with attempting to plot functions like that using DensityPlot.

    • The function is zero everywhere but at a single point. DensityPlot[] is unsuitable for functions like this, as a few minutes thought should convince you.
    • Even for other function with jump discontinuities, the number of plot points and the maximum recursion depth you use for DensityPlot[] can have a significant effect on the approximation. For instance, if you try to use

      Code (Text):
      DensityPlot[HeavisideTheta[x, y], {x, -1, 1}, {y, -1, 1}]
      you'll see that the resulting density plot exhibits approximation artifacts around the jump discontinuity in the 2D Heaviside function. This can be controlled using PlotPoints and MaxRecursion, but you should be aware that DensityPlot[] is more suited to functions that are at least [itex]C^1[/itex] everywhere in the region of interest.
  4. May 13, 2010 #3
    You are correct; I need to think a little more. Thanks; I'll try out your suggestions.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook