1

I am trying to plot a two variable function A simple function x*DiracDelta(y)+y*DiracDelta(x) code will work with simple function without DiracDelta But when I enter the delta function, it gives an error? Can you help me find the problem with this code?

from sympy import symbols, DiracDelta
from sympy.plotting import plot_parametric
from sympy import symbols, DiracDelta
from sympy.plotting import plot3d

x, y = symbols('x y')
f = x*DiracDelta(y)+y*DiracDelta(x)

plot3d(f, (x, -1, 1), (y, -1, 1))

Thanks!

try to plot a 3d function It works when I have function without Delta function but it give me error when put delta function

Stef
  • 13,242
  • 2
  • 17
  • 28

1 Answers1

1

About DiracDelta in general

The Dirac distribution is not a function in the mathematical sense.

The sympy documentation for sympy.DiracDelta states it explicitly:

DiracDelta is not an ordinary function. It can be rigorously defined either as a distribution or as a measure.

DiracDelta only makes sense in definite integrals, [...] care must be taken to not treat it as a real function. [...] if DiracDelta is treated too much like a function, it is easy to get wrong or nonsensical results.

So, you can't plot it like a function, because it isn't a function.

In simple words, the Dirac distribution behaves like a function that is 0 almost everywhere, but has an integral of 1 in any interval that includes 0.

Intuitively, if it could be plotted like a function, the Dirac distribution would have a plot that looks kinda like this:

Plot of Dirac Delta

Except the width of the peak should be 0, and the area under the peak should be exactly 1. This of course would be self-contradictory for a function, so no plot will be truly satisfactory. Again, the Dirac distribution is not a function.

About your f = x*DiracDelta(y)+y*DiracDelta(x)

Again, f is not a function in the mathematical sense. It will behave like a function that is 0 almost everywhere, but with an integral equal to (x2**2 - x1**2)/2 on regions that intersect segment [x1, x2] on the x-axis, and an integral equal to (y2**2 - y1**2)/2 on regions that intersect segment [y1, y2] on the y-axis.

Instead of plotting f, which won't work since f is not a function, I suggest approximating DiracDelta by a "peak" function like the one I drew above, and plot an approximation of f using this approximation of DiracDelta. For instance:

# approximating DiracDelta by a piecewise-affine peak function
from sympy import Piecewise

def dn(x, n):
    return Piecewise(
        (0,               x <= -1/n),
        (n + n**2 * x,    x <= 0),
        (n - n**2 * x,    x <= 1/n),
        (0,               True)
    )


from sympy import symbols, DiracDelta
x, y = symbols('x y')
f = x * DiracDelta(y) + y * DiracDelta(x)

n = 20
fn = x * dn(y, n) + y * dn(x, n)

# now plot fn instead of f
Stef
  • 13,242
  • 2
  • 17
  • 28
  • Thanks for the explanation If I want to draw the integral of this function I add the following line to the code – AliReza Mohammadi Jun 10 '23 at 19:14
  • Thanks for the explanation If I want to draw the integral of this function I add the following line to the code from sympy import symbols, DiracDelta from sympy.plotting import plot3d x, y = symbols('x y') f = x*DiracDelta(y)+y*DiracDelta(x) f1=x+y z=sp.integrate(f, x,y) z1=sp.integrate(f1, x,y) plot3d(z, (x, -1, 1), (y, -1, 1)) plot3d(z1, (x, -1, 1), (y, -1, 1)) but But the integrated function is still not plot – AliReza Mohammadi Jun 11 '23 at 12:25
  • @AliRezaMohammadi Alternatively, you can define a "peak" function `dn(x, n)`, affine by parts, with `dn(x, n) = 0` when `x <= -1/n` or `x >= 1/n`, `dn(0, n) = n`, `dn(x, n) = n - n**2 * x` when `0 <= x <= 1/n`, and `dn(x, n) = n + n**2 * x` when `-1/n <= x <= 0`. Then `dn` is an approximation of `DiracDelta`: it's 0 when `x` is not too close to 0, and its integral around 0 is equal to 1. And then, instead of plotting `f = x * DiracDelta(y) + y * DiracDelta(x)`, you choose a large value of `n` and you plot `fn = x * dn(y, n) + y * dn(x, n)`. – Stef Jun 12 '23 at 10:52
  • Great...Thank you... – AliReza Mohammadi Jun 12 '23 at 12:23
  • @AliRezaMohammadi Sorry, I meant piecewise-affine, not affine by part. – Stef Jun 12 '23 at 12:24
  • I got it Thank you – AliReza Mohammadi Jun 12 '23 at 12:33