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

时间:2018-10-25 07:44:22

标签: matlab scilab

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

equations

我知道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);

1 个答案:

答案 0 :(得分: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);