gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:39:41 24 4
gpt4 key购买 nike

我想在 scilab 中绘制 limacon,我有这个方程需要处理:

equations

我知道r>0l>0

当我编译以下代码时,我在第 5 行收到此错误:

Inconsistent row/column dimensions.

如果我将 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);

最佳答案

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

相反,您需要使用 .* 进行逐元素乘法( 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);

关于matlab - Scilab 中行/列尺寸不一致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52983962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com