无法在sympy中绘制d(e ^-| t |)/ dt的傅立叶变换

时间:2018-07-05 09:22:29

标签: python plot sympy

我正在使用sympy来计算

的傅立叶变换
-exp(-Abs(t))*sign(t)

像这样

import sympy as sp
t = sp.Symbol('t', real=True)
o = sp.Symbol('o', real=True)
temp = sp.im(sp.fourier_transform(sp.exp(-sp.Abs(t)), t, o))
print(temp)

这将返回

4*pi*o/(4*pi**2*o**2 + 1)

现在我想通过调用来绘制

sp.plotting.plot(temp, (t,1,10))

这会导致错误

/home/somename/miniconda3/lib/python3.6/site-packages/sympy/plotting/experimental_lambdify.py:232: UserWarning: The evaluation of the expression is problematic. We are trying a failback method that may still work. Please report this as a bug.
  warnings.warn('The evaluation of the expression is'
Traceback (most recent call last):
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/plotting/experimental_lambdify.py", line 194, in __call__
    result = complex(self.lambda_func(args))
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/core/expr.py", line 244, in __complex__
    return complex(float(re), float(im))
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/core/expr.py", line 239, in __float__
    raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "sympy_analysis.py", line 153, in <module>
    main()
  File "sympy_analysis.py", line 75, in main
    sp.plotting.plot(sp.im(sp.fourier_transform(sp.diff(EXP(1,1),t), t, o)), (t,1,10))
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/plotting/plot.py", line 1295, in plot
    plots.show()
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/plotting/plot.py", line 196, in show
    self._backend.show()
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/plotting/plot.py", line 1029, in show
    self.process_series()
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/plotting/plot.py", line 908, in process_series
    collection = self.LineCollection(s.get_segments())
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/plotting/plot.py", line 514, in get_segments
    f_start = f(self.start)
  File "/home/somename/miniconda3/lib/python3.6/site-packages/sympy/plotting/experimental_lambdify.py", line 235, in __call__
    if abs(result.imag) > 1e-7 * abs(result):
AttributeError: 'Mul' object has no attribute 'imag'

为什么情节不起作用?

1 个答案:

答案 0 :(得分:3)

傅里叶变换的变量是o,而不是t。这应该是绘图范围内的变量。

此外,您的代码缺少sign(t)。并且可以简单地像sp.plot那样调用绘图命令。并且两个符号都可以一次用symbols创建。

import sympy as sp
t, o = sp.symbols('t o', real=True)
temp = sp.im(sp.fourier_transform(sp.exp(-sp.Abs(t))*sp.sign(t), t, o))
sp.plot(temp, (o, 1, 10))

plot