Trapz error ORDER contains an invalid permutation index.

7 ビュー (過去 30 日間)
AYUSH KUMAR
AYUSH KUMAR 2019 年 3 月 29 日
コメント済み: John D'Errico 2019 年 3 月 29 日
I am trying to perform a definite integration from 0 to 1000 on a function with two variable w.r.t one variable(E) while plotting w.r.t another(V) using the trapz function. rtdoff is a pre-defined function which returns a value as double.
En = 0;
E = En*1.602*10^-19;
q = 1.6*10^-19;
T = 300;
V = 0:0.01:10;
Ef = 1.695*1.6*10^-19;
K = 5.5*10^30;
kb = 1.38*10^-23;
TE = zeros(length(V));
J = zeros(length(V));
f = zeros(length(V));
for i=1:length(V)
TE(i) = rtdoff(V(i), E , 10 , 0 );
E = 0:1:1000 ;
J(i) = K.*TE(i)*log((1+exp((Ef-E)./(kb.*T)))/(1+exp((Ef-E-(q.*V(i))./(kb*T)))));
f(i) = trapz(E,J(i)) ;
end
semilogy(V,f);
On running the function I am getting the following error
Error using permute
ORDER contains an invalid permutation index.
Error in trapz (line 43)
y = permute(y,perm);
Error in current3 (line 16)
f(i) = trapz(E,J(i)) ;
What am I doing wrong ? How can I remove this error and get the output I want ?

回答 (1 件)

John D'Errico
John D'Errico 2019 年 3 月 29 日
編集済み: John D'Errico 2019 年 3 月 29 日
If you call trapz using TWO arguments, the second of which is a scalar, then it is assumed you want to use the second argument to indicate which dimension to integrate on. If the second arguemnt is scalar, but NOT not a positive integer value, then you will generate an error.
So, let me see what you did to create that specific error.
trapz(0:10,pi)
Error using trapz (line 47)
Dimension argument must be a positive integer scalar within indexing range.
Or, this:
trapz(0:10,-pi)
Error using trapz (line 47)
Dimension argument must be a positive integer scalar within indexing range.
So, it looks like they are testing for obvious errors you may have made. But what about this?
trapz(0:10,-1)
Error using permute
ORDER contains an invalid permutation index.
Error in trapz (line 51)
y = permute(y,perm);
And that is indeed the error that you got.
Apparently, they did not test to see if the second argument of two args is a negative scalar integer. Somehow, that got through the error checks. So, inside trapz, I find this block:
elseif nargin == 2 && isscalar(y) % trapz(y,dim)
dim = y;
y = x;
x = 1;
if ~isscalar(dim) || ~isnumeric(dim) || (dim ~= floor(dim))
error(message('MATLAB:getdimarg:dimensionMustBePositiveInteger'));
end
So, the second argument was a scalar, and we have exactly 2 arguments. -1 is a scalar, it is numeric, and it is integer. So the error message never got triggered. The author of trapz never thought to test if the second argument is a negative scalar integer. But of course that later on causes permute to fail inside trapz.
You, on the other hand, should know that calling trapz in this way will not get a viable result anyway. Why you are calling it like that? Well, that is your decision to make, as I cannot debug undocumented code that does something in error, where I am given no hint as to what it does.
  4 件のコメント
Torsten
Torsten 2019 年 3 月 29 日
The line
J(i) = K.*TE(i)*log((1+exp((Ef-E)./(kb.*T)))/(1+exp((Ef-E-(q.*V(i))./(kb*T)))));
doesn't make any sense at all.
E is a vector, J(i) is a scalar.
...(kb.*T))) / (1+exp...
divides a vector by a vector ...
Think about what you want to store in J.
John D'Errico
John D'Errico 2019 年 3 月 29 日
@Ayush - it looks like you need to use cumtrapz, not trapz. But this is purely a wild guess,
Compute the vector J in the loop. FIRST. Then after the loop is done, call cumtrapz.

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

カテゴリ

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

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by