gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:12:11 31 4
gpt4 key购买 nike

我正在使用 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'

为什么剧情不行?

最佳答案

傅里叶变换的变量是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

关于python - 无法在 sympy 中绘制 d(e^-|t|)/dt 的傅立叶变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51187718/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com