Mathematica: Plotting piecewise functions

In summary, The speaker explains that using DensityPlot for functions with a single point of non-zero value is unsuitable and can result in approximation artifacts. They suggest using PlotPoints and MaxRecursion to control these artifacts, but note that DensityPlot is better suited for functions that are at least C^1 everywhere in the region of interest. The listener acknowledges this and thanks the speaker for their suggestions.
  • #1
Niles
1,866
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
  • #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:
    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.
 
  • #3
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.
 

1. What is a piecewise function?

A piecewise function is a mathematical function that is defined by different expressions for different parts of its domain. It is typically used to represent a function that has different rules or behaviors for different input values.

2. How do I plot a piecewise function in Mathematica?

To plot a piecewise function in Mathematica, you can use the Piecewise function and specify the different expressions and conditions for each part of the function. Then, use the Plot function to graph the function over a desired range of inputs.

3. Can I plot multiple piecewise functions on the same graph?

Yes, you can plot multiple piecewise functions on the same graph by using the Plot function and including all of the functions within a single argument. You can also use different colors or styles to differentiate between the different functions.

4. How do I add labels and titles to my piecewise function plot?

To add labels and titles to your plot, you can use the PlotLabels and PlotLegends options within the Plot function. You can also use the PlotLabel option to add a title to your graph. Additionally, you can use the AxesLabel option to add labels to the x and y axes.

5. Can I change the appearance of my piecewise function plot?

Yes, you can change the appearance of your plot by using various options within the Plot function, such as PlotStyle, PlotMarkers, and PlotTheme. These options allow you to customize the color, shape, and overall style of your plot to your preferences.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
200
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
720
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
864
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top