Mathematica: Plotting piecewise functions

Click For Summary
SUMMARY

The discussion centers on the limitations of using Mathematica's DensityPlot for visualizing piecewise functions, specifically when plotting functions like DensityPlot[If[x == 0 && y == 0, 1, 0], {x, -1, 1}, {y, -1, 1}]. The primary issues identified are that DensityPlot is unsuitable for functions that are non-zero at isolated points and that approximation artifacts can occur with functions exhibiting jump discontinuities, such as DensityPlot[HeavisideTheta[x, y], {x, -1, 1}, {y, -1, 1}]. To improve results, users should adjust PlotPoints and MaxRecursion settings, as DensityPlot is better suited for functions that are at least C^1 continuous in the plotting region.

PREREQUISITES
  • Familiarity with Mathematica version 12 or later
  • Understanding of piecewise functions and their properties
  • Knowledge of the Heaviside step function
  • Basic concepts of numerical approximation in plotting
NEXT STEPS
  • Explore the use of PlotPoints and MaxRecursion in DensityPlot for better accuracy
  • Learn about alternative plotting functions in Mathematica for discontinuous functions
  • Investigate the properties of C^1 continuous functions in the context of plotting
  • Examine the implications of approximation artifacts in numerical visualizations
USEFUL FOR

This discussion is beneficial for Mathematica users, data visualizers, and mathematicians interested in accurately plotting piecewise and discontinuous functions.

Niles
Messages
1,834
Reaction score
0
Hi guys

Take a look at

Code:
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?


Niles.
 
Physics news on Phys.org
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:
    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.
 
shoehorn said:
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.

You are correct; I need to think a little more. Thanks; I'll try out your suggestions.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K