0
votes

The function I need to plot is y = exp(-0.3*t)*(2*cos(2*t) + 4*sin(2*t)) for the range of values of t between 0 and 2*pi.

I entered the following commands on MATLAB:

>> t=linspace(0,2*pi,101);
>> y=exp(-0.3*t)*(2*cos(2*t) + 4*sin(2*t));

And I come up with the following error:

Error using  * 
Inner matrix dimensions must agree.

I don't know why. Can someone point out why and suggest the correct command line argument?

Thanks!

2
use .* rather than * for element wise multiplication: y=exp(-0.3*t).*(2*cos(2*t) + 4*sin(2*t)); - user2999345

2 Answers

1
votes

Your issue is in this term:

exp(-0.3*t) * (2*cos(2*t) + 4*sin(2*t));

You are multiplying 2 vectors. You want to be doing element-wise operations, i.e. each element of exp(-0.3*t) times each corresponding element of (2*cos(2*t) + 4*sin(2*t)), rather than the vector product of the two.

To achieve what you want, simply add a dot . before the multiplication *, like so

y = exp(-0.3*t) .* (2*cos(2*t) + 4*sin(2*t));

See this documentation for array vs. element-wise operations: http://uk.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html

1
votes

The "*" operator is a matrix-multiplication operator, like https://en.wikipedia.org/wiki/Matrix_multiplication

You need to use an ".*" operator which is a per-element operator. You must use it to match elements from one vector or matrix to the elements from the other vector or matrix one-to-one.

So you have to do

y=exp(-0.3*t).*(2*cos(2*t) + 4*sin(2*t));

Note that ".*" is not needed when multiplying by constant, because the effect is the same for matrix and per-element operation