Trapz error (ORDER contains an invalid permutation index)

6 ビュー (過去 30 日間)
Alvin
Alvin 2017 年 11 月 3 日
コメント済み: Alvin 2017 年 11 月 4 日
clear
C1 = linspace(0,1e+04,3000);
k1 = 1;
k2 = 0;
n1 = 0;
n2 = 0;
nm = 0;
D = 0;
O = 0;
gam = 1e-04.*k1;
alph = 0;
w = linspace(-5,5,3000);
for ii=1:length(C1)
for jj=1:length(w)
TA = (w(jj)+D)./(k1./2);
TB = (w(jj)-O)./(gam/2);
xa = 1-1i*TA;
xb = 1-1i*TB;
C = (2.*1i.*sqrt(C1(ii)))./(sqrt(gam).*((xa.*xb)+C1(ii)));
D = (2.*xa)./(sqrt(gam).*((xa.*xb)+C1(ii)));
MC = (abs(C)).^2;
MD = (abs(D)).^2;
Sbb = 2.*pi*(MC.*(n1+0.5)+MD.*(nm+0.5));
A = trapz(w,Sbb)./(2.*pi);
AA(ii)=A;Cum
figure(1)
plot(w,Sbb)
set(gca,'FontSize',13)
xlabel('\omega')
ylabel('S_{bb}')
%ylim([0,200])
drawnow
end
end
As stated in my title, the error:ORDER contains an invalid permutation index was returned upon compiling the code. I've searched this up and found that if I put my trapz to
trapz(w,Sbb,1)
or
trapz(w,Sbb,2)
I could remedy the problem. But it appears to not work. Can anyone assist me?
Much thanks!

採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 3 日
You have trapz(w,Sbb) with Sbb being a scalar.
There are two trapz() syntaxes with two arguments:
trapz(X, Y)
trapz(X, dim)
The way that MATLAB determines which one is being used is it looks to see whether the second argument is a vector or not: if it is then it assumes the first syntax, but if it is a scalar (as is the case for you) then it assumes the second syntax is being used.
Your w values form a line. You should not be trying to trapz() a scalar Y against a vector of X.
I think if you investigate, you will find that you can vectorize the Sbb calculation over all w values instead of using a loop. It does make sense to trapz with a vector of Sbb.
  1 件のコメント
Alvin
Alvin 2017 年 11 月 4 日
Thanks! I vectorized w and kept only one loop (over C1) and this seems to work. That way my Sbb is kept as a vector so trapz(w,Sbb) functions normally!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by