MATLAB: How Can I Plot y=10*exp(-5*t)*t function

MATLAB and Simulink Student Suiteplot of exponential function

I wrote this code given in the below;
t=0:0.01:1;
y=10*t*exp((-5)*t);
x=exp((-5)*t)*((-5)*t+1);
plot(t,y,'k');
hold on;
plot(t,x,'r');
and I give this error; Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise
multiplication, use '.*'.
How can i handle it.

Best Answer

It is not a big problem and I will tell you what your problem is and then you can write the code yourself then.
Let me tell you how plot works in Matlab. For example If you want to plot for Y=t*exp(3*t), you will do the following.
t=0:1:10 %Now t has values [0 1 2 3 4 5 6 7 8 9 10] which is a row vector of the size (1 * 11)
y=t.*(exp(3*t)) % .* I have used this multiplication, because t is of size(1*11) and exp(3*t) is also (1*11)
So basically * means matrix multiplication in matlab and here it does (1*11) by (1*11) which is not possible. What you actually want is the first element of t to be multiplied by first element of exp(3*t) which is (0*0) and second element by second element and so on,This is called Element-wise-Multiplication.
t= 0 1 2 3 4 5 6 7 8 9 10
exp3*t= 0 0 0 0 0 0 0.0001 0.0026 0.0532 1.0686
Now if you multiply by using (.*) command, you will end up in (1*11) vector again which will be stored in y.
So you can plot now using plot command.
plot(t,y)
Whenever you errors like this,you can use size command. Here size(t) gives you the answer 1 11 which means 1 row and 11 column, Likewise if you enter size(exp(3*t)), again it will give you 1 11 and it will give you an intuitive sense why it is not multiplying.