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

Koz*_*kom 2 matlab scilab

我想在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);
Run Code Online (Sandbox Code Playgroud)

Wol*_*fie 6

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

相反,您需要使用.*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);
Run Code Online (Sandbox Code Playgroud)