0

我的“新爱”有问题,matlab:我写了一个函数来使用 trapz 方法计算积分:`

function [L]=bogenlaenge_innen(schwingungen)
R = 1500;            %Ablegeradius
OA = 1;              %Amplitude
S = schwingungen;    %Schwingungszahl
B = 3.175;           %Tapebreite

phi = 0:2.*pi./10000:2.*pi;

BL = sqrt((R-B).^2+2.*(R-B).*OA.*sin(S.*phi)+OA.^2.*(sin(S.*phi)).^2+OA.^2.*S.^2.*(cos(S.*phi)).^2);   

L = trapz(phi,BL)`

当我使用命令窗口中的一个特定数字启动它时,这很好用。现在我想为几个 S 绘制“L”的值。

我在一个新的 *.m 文件中执行了以下操作: W = (0:1:1500); T = bogenlaenge_innen(W); plot(W,T)

它就是:

错误使用 .* 矩阵尺寸必须一致。

怎么了?它只是某个地方的一个点吗?我现在第二天使用matlab,所以请耐心等待.... ;) 非常感谢您!

PS:忽略代码的德语部分,这并不重要:)

4

3 回答 3

0

The error is most likely because you have made it so that the number of elements in schwingungen, i.e. W in your code, must be equal to the number of elements in phi. Since size(W) gives you a different result from size(0:2.*pi./10000:2.*pi), you get the error.

The way .* works is that is multiplies each corresponding elements of two matrices provided that they either have the same dimensions or that one of them is a scalar. So your code will work when schwingungen is a scalar, but not when it's a vector as chances are it has a different number of elements from the way you hard coded phi.

The simplest course of action (not necessarily the most Matlabesque though) for you is to loop through the different values of S:

W = (0:1:1500);
T = zeros(size(W); %Preallocate for speed)

for ii = 1:length(W)
    T(ii) = bogenlaenge_innen(W(ii));
end

plot(W,T)
于 2013-06-06T13:57:54.287 回答
0

在您的函数中,您将 phi 定义为 10001 个元素的向量。在同一个函数中,您执行 S.*phi,因此如果 S 与 phi 的长度不同,您将收到“尺寸必须一致”错误。在您调用该函数时,您正在使用长度为 1501 的向量执行此操作,因此您的错误是。

问候

于 2013-06-06T14:03:01.530 回答
0

在您的代码中,数组Sphi表达式中的sin(S.*phi)大小应该相同,或者其中一个应该是常量,这样代码才能正常工作

于 2013-06-06T14:00:29.323 回答