Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to plot x(t)=t*exp(-3*t)+0.25*exp(-3*t), and its first and second time derivatives.

I cannot even get the first plot correct. This is what I have done.

t = [0:0.01:3];
x = t*exp(-3*t)+0.25*exp(-3*t);

From what I understand, that is saying that t goes from 0 to 3 in steps of 0.01 and x is the function I defined at that t. MatLab gives me this error

"Error using * Inner matrix dimensions must agree."

Can someone please let me know what's going on? I'm a matlab noob. I use mathematica quite a bit and I have used octave for making plots of data, but never to plot functions. I always do that stuff with mathematica!

share|cite|improve this question
If you have Mathematica, why not do this there? x[t_] := t Exp[-3 t] + 0.25 Exp[-3 t]. Plot[{x[t], x'[t], x''[t]}, {t, 3, 0.1}] – Amzoti Nov 3 '13 at 2:37
Why are you using Matlab at all? Plotting and taking derivatives of functions is much easier in Mathematica or Maple. – Stefan Smith Nov 3 '13 at 3:35
@Rich Williams : Also, pure Matlab questions with no math content are discouraged here. StackOverflow might be a better place to post. – Stefan Smith Nov 4 '13 at 0:26

you have to have

x = t.*exp(-3.*t)+0.25*exp(-3.*t)

this will do element by element multiplication

.* is the element by element multiplication for a vector, * is just multiplication.

share|cite|improve this answer

Plotting and taking derivatives of functions in Matlab is perfectly easy. First you just need to use the symbolic math capabilities:

syms t;
x = t*exp(-3*t)+0.25*exp(-3*t);
xdot = diff(x,t,1)
xddot = diff(x,t,2)

Then to plot these you have several options. You can substitute in numeric values using subs, which automatically vectorizes your equations so you don't need to worry about adding .*:

t_ = 0:0.01:3;
x_ = subs(x,t,t_);
xdot_ = subs(xdot,t,t_);
xddot_ = subs(xddot,t,t_);

Or you can use the ezplot function to directly plot the symbolic function over a range:

ezplot(x,[0 3]);
ezplot(xdot,[0 3]);
ezplot(xddot,[0 3]);

The function fplot is another option if you convert your symbolic equations to numeric functions. This can be done manually or via the matlabFunction function:

fplot(matlabFunction(x),[0 3])
fplot(matlabFunction(xdot),[0 3])
fplot(matlabFunction(xddot),[0 3])
share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.