DiracDelta not giving correct result

大兔子大兔子 提交于 2020-12-30 02:22:05

问题


I have to use dirac delta in a complicated integral and was hoping to see how it works with a simple case but it returns the wrong answer. Any clue what I did wrong in the following?

from sympy import DiracDelta
from scipy import integrate

def f(x):
     return x*DiracDelta(x-1)

b, err = integrate.quad(f, 0, 5)    
print b

This returns 0.0 while it shouldn't.


回答1:


It seems sympy functions are not compatible with scipy integrate. One needs to use sympy integrate. The following gives the correct answer

from sympy import *
x = Symbol('x')
print integrate(x*DiracDelta(x-1), (x, 0, 5.0))    

Although, I am not sure if sympy.integrate is as powerful and versatile as scipy.integrate.




回答2:


HuShu's answer is correct. I'll add that the Dirac δ function is a symbolic method of representing function evaluation as an integral. It's useful as a symbolic abstraction, but if you only care about numeric evaluation, just do the function evaluation. That is instead of

b
⌠
⎮ f(x)⋅DiracDelta(x - 1) dx
⌡
a

just use

f(1) if a <= 1 <= b else 0


来源:https://stackoverflow.com/questions/36755487/diracdelta-not-giving-correct-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!