Search code examples
matlab

Plotting continuous variable in MATLAB


I am using fplot() to plot the expression exp(-t)sin(5t) over the range of (-2,2). I did fplot(exp(-t)*sin(5*t),[2,2]) but it doesn't plot the way I wanted. What should I do?


Solution

  • You're getting an error:

    Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error.

    Listen to the error - you are missing multiplication operators! We improve the code a little...

    fplot(exp(-t).*sin(5.*t),[2,2])
    

    Now you get a new error:

    Unrecognized function or variable 't'.

    Again listen to the error, you haven't defined t, and fplot expects you to pass a function (from the docs)

    fplot(f) plots the curve defined by the function y = f(x)

    We can make the expression into a function and improve the code a little more...

    fplot(@(t)exp(-t).*sin(5.*t),[2,2])
    

    Now you get a blank figure?? Ah looking at the docs again, we see

    fplot(f,xinterval) plots over the specified interval. Specify the interval as a two-element vector of the form [xmin xmax].

    Where [xmin xmax] are currently both 2 in your example. We can't plot over a range of nothing, so choose something else. Now we have working code:

    fplot(@(t)exp(-t).*sin(5.*t),[-2,2])
    

    plot