在 MATLAB 中绘制符号变量时出错?

Error plotting symbolic variable in MATLAB?

我正在尝试通过 matlab alex palamides 从书“信号和系统实验室”学习傅里叶变换

在第312页,给出了以下代码,演示了卷积可以通过将两个信号的傅里叶变换相乘,然后取乘积的逆傅里叶来实现

syms t w
x1=heaviside(t)-heaviside(t-2);
x2=heaviside(t)-heaviside(t-4);
X1=fourier(x1,w);
X2=fourier(x2,w);
right=ifourier(X1*X2,t)
ezplot(right)

我尝试了 MATLAB 2019 和 MATLAB 2020,但我都遇到了同样的问题

实际上,当我尝试 运行 我的 MATLAB 中的上述代码时,我没有得到像书中那样的输出,而是出现以下错误

Error using inlineeval (line 14)
Error in inline expression ==> (t.*pi.*sign(t) + fourier(cos(2.*w)./w.^2, w, -t) +
fourier(cos(4.*w)./w.^2, w, -t) - fourier(cos(6.*w)./w.^2, w, -t) - fourier(sin(2.*w)./w.^2, w,
-t).*1i - fourier(sin(4.*w)./w.^2, w, -t).*1i + fourier(sin(6.*w)./w.^2, w, -t).*1i)./(2.*pi)
 Undefined function 'fourier' for input arguments of type 'double'.

Error in inline/feval (line 33)
        INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

Error in ezplotfeval (line 53)
    z = feval(f,x(1),y(1));

Error in ezplot>ezimplicit (line 271)
    u = ezplotfeval(f, X, Y);

Error in ezplot (line 167)
                hp = ezimplicit(cax, f{1}, vars, labels, args{:});

Error in sym/ezplot (line 66)
   h = ezplot(fhandle(f)); %#ok<EZPLT>

Error in Untitled (line 7)
ezplot(right)

此处还附有书页快照

看起来 matlab 不能 transform the function X1*X2,所以它 returns 逆傅里叶变换作为对傅里叶的未评估调用。

>> right

right =
 
(pi*t*sign(t) + fourier(cos(2*w)/w^2, w, -t) + fourier(cos(4*w)/w^2, w, -t) - fourier(cos(6*w)/w^2, w, -t) - fourier(sin(2*w)/w^2, w, -t)*1i - fourier(sin(4*w)/w^2, w, -t)*1i + fourier(sin(6*w)/w^2, w, -t)*1i)/(2*pi)

ezplotfplot 无法绘制这样的表达式。

我在 MATLAB Answers 上发现了同样的问题。发布的解决方案 Walter Roberson is to rewrite X1*X2 in terms of exp before taking the inverse fourier transform。引用自 MATLAB 答案:

In your release of MATLAB, ezplot() was not compatible with plotting symbolic expressions, and fplot() had to be used instead.
However, the ifourier() is giving unusable results that neither ezplot() nor fplot() can use.
The work-around, valid from R2012a, is:

right = ifourier( rewrite(X1*X2, 'exp'), t);
fplot(right, [0 8])

结果(在 R2021b 上):