Warm tip: This article is reproduced from serverfault.com, please click

matlab-Scilab中不一致的行/列尺寸错误

(matlab - Inconsistent row/column dimensions error in Scilab)

发布于 2018-10-25 07:44:22

我想在scilab中绘制Limacon,我需要处理以下方程式:

方程式

我知道,r>0l>0

当我编译下面的代码时,在第5行出现此错误:

行/列尺寸不一致。

如果设置t为特定数字,则最终会得到干净的曲线,其中没有任何功能。

我想改变rl到其它号码,但这并不做任何事情。有人知道我在做什么错吗?

r=1;
l=1;
t=linspace(0,2,10);
x = 2 * r * (cos(t))^2 + l * cos(t);
y = 2 * r * cos(t) * sin(t) + l * sin(t);
plot (x,y);
Questioner
Kozikom
Viewed
11
Wolfie 2018-10-25 17:31:57

你(不小心)尝试使用进行矩阵乘法*

相反,你需要使用.*Scilab docsMATLAB docs)进行逐元素乘法

同样,你应该使用元素方幂.^来平方第一个方程中的余弦项。

请参阅下面的修改后的代码中的注释...

r = 1;
l = 1;
% Note that t is an array, so we might encounter matrix operations!
t = linspace(0,2,10); 
% Using * on the next line is fine, only ever multiplying scalars with the array.
% Could equivalently use element-wise multiplication (.*) everywhere to be explicit.
% However, we need the element-wise power (.^) here for the same reason!
x = 2 * r * (cos(t)).^2 + l * cos(t);      
% We MUST use element-wise multiplication for cos(t).*sin(t), because the dimensions
% don't work for matrix multiplication (and it's not what we want anyway).
% Note we can leave the scalar/array product l*sin(t) alone, 
% or again be explicit with l.*sin(t) 
y = 2 * r * cos(t) .* sin(t) + l * sin(t);
plot (x,y);