Trapz error ORDER contains an invalid permutation index.
Ältere Kommentare anzeigen
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 ?
Antworten (1)
John D'Errico
am 29 Mär. 2019
Bearbeitet: John D'Errico
am 29 Mär. 2019
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 Kommentare
AYUSH KUMAR
am 29 Mär. 2019
Steven Lord
am 29 Mär. 2019
Good catch, John. I've entered this in the bug database.
Torsten
am 29 Mär. 2019
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
am 29 Mär. 2019
@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.
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!