Scilab 中不一致 row/column 尺寸错误
Inconsistent row/column dimensions error in Scilab
我想在 scilab 中绘制 limacon,我有这个方程式要处理:
我知道 r>0
和 l>0
当我编译下面的代码时,我在第 5 行得到这个错误:
Inconsistent row/column dimensions.
如果我将 t
设置为特定数字,我最终会得到干净的图,其中没有任何功能。
我试图将 r
和 l
更改为其他数字,但这没有任何作用。有人知道我做错了什么吗?
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);
您(不小心)尝试用 *
进行矩阵乘法。
相反,您需要与 .*
(Scilab docs, MATLAB 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);
我想在 scilab 中绘制 limacon,我有这个方程式要处理:
我知道 r>0
和 l>0
当我编译下面的代码时,我在第 5 行得到这个错误:
Inconsistent row/column dimensions.
如果我将 t
设置为特定数字,我最终会得到干净的图,其中没有任何功能。
我试图将 r
和 l
更改为其他数字,但这没有任何作用。有人知道我做错了什么吗?
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);
您(不小心)尝试用 *
进行矩阵乘法。
相反,您需要与 .*
(Scilab docs, MATLAB 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);