Search code examples
matlabmathsignal-processingconvolutionsymbolic-math

Error plotting symbolic variable in MATLAB?


I am trying to learn fourier transform from book"signals and systems laboratory with matlab alex palamides"

On page 312, following code is given which demonstrates that convolution can be implemented by multiplying the fourier transforms of two signals and then taking the inverse fourier of product

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)

I tried MATLAB 2019 and MATLAB 2020 but i get same problem in both

Actually When i try to run above code in my MATLAB i don't get output like the one in book, instead i get following error

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)

Snapshot of book page also attached hereenter image description here


Solution

  • I found the same question on MATLAB Answers. The solution as posted by Walter Roberson is to rewrite X1*X2 in terms of exp before taking the inverse fourier transform. Quoting from MATLAB Answers:

    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])
    

    Result (on R2021b):

    result