简体   繁体   中英

Plotting equally spaced points for a graph on MATLAB

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!

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, ie 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM