[英]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. 对于0到2 * pi之间的t值范围,我需要绘制的函数是y = exp(-0.3 * t)*(2 * cos(2 * t)+ 4 * sin(2 * t))。
I entered the following commands on MATLAB: 我在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. 您正在将2个向量相乘。 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. 您想要做逐元素的操作,即, 每个元件
exp(-0.3*t)
时间的每一个对应元件 (2*cos(2*t) + 4*sin(2*t))
,而不是所述载体两者的乘积。
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 请参阅此文档以了解数组与元素方式的操作: 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 “ *”运算符是矩阵乘法运算符,例如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 请注意,乘以常数时不需要“。*”,因为对于矩阵运算和每个元素运算,其效果相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.