I am trying to Integrate data (discontinuous) over certain range and get error

3 views (last 30 days)
Hi all,
I am trying to Integrate data (discontinuous) over certain range and get error. It displays as follows
"Error using permute
ORDER contains an invalid permutation index.
Error in cumtrapz (line 46)
y = permute(y,perm);
Error in Effi1 (line 72)
yy=cumtrapz(Sol_wv(1:i),Sol_wv(1:i)'.*Sol_energy(1:i));"
My goal is to have all of Ai values as in the form of a vector contained in Aii. (without replacing the old Ai value generated)
In other words, I would like to see length(Aii)= length(Sol_wv).
Here are my codes.
______________________________
A=(theta/(P_t*h*c)); % All the variables are constants
for i=1:length(Sol_wv) % length(Sol_wv) is 2002
y=((h*c./Sol_wv(i))-E_loss); %E_loss is a constant
yy=cumtrapz(Sol_wv(1:i),Sol_wv(1:i)'.*Sol_energy(1:i));
% I would like yy to return the area under the data of specified range (defined by i)
% The dimensions of Sol_wv and Sol_energy agree. I have confirmed it.
Ai(i)=A*(y).*yy;
% Return the iterative Ai(i) into Ai(i), in which gets replaced per iteration
Aii(i)=Ai;
% Store the array of Ai values into Aii. This will be used for plotting later.
length(Aii)
plot(Sol_wv,Aii)
end
% Somehow length(Aii) returns 1631, which is supposed to be 2002 if the codes are right
%I have tried while methods, yet this also fails to work.
Thanks in advance,
Ian

Answers (2)

Walter Roberson
Walter Roberson on 17 Mar 2014
Your Sol_wv(1:i) is 0, so Sol_wv(1:i)'.*Sol_energy(1:i) is 0, and your cumtrapz() call becomes cumptraz(0,0) which gives you the error.
Try adding the dimension to the call:
cumtrapz(Sol_wv(1:i), Sol_wv(1:i)'.*Sol_energy(1:i), 2)
  1 Comment
Taehyun Kim
Taehyun Kim on 17 Mar 2014
Hi Walter,
I forgot to mention that Sol_wv and Sol_energy are actually raw and colum vectors respectively. Each of length 2002. (These are predefined)
So when I try actual numbers into i e.g. Sol_wv(1,5) this actually does return the 5th value from the row vectors.
Could it be from other source of error?

Sign in to comment.


Taehyun Kim
Taehyun Kim on 18 Mar 2014
To Walter and anyone who might be looking into this post,
I have solved the problem. The error resulted from not the logic of the coding but the way matlab takes inputs.
I found the Solution from Walter's comment on another question in the forum.
"There are two trapz() syntaxes that allow passing in two parameters to trapz. The first one is obvious in the documentation and is trapz(X, Y) where X is a scalar or vector spacing increment for the data in Y. The second is not obvious in the documentation but can be found once you know what to look for. It is trapz(Y, dim) where dim is the dimension number to operate along. In the first iteration of your "for z" loop, z is initialized to 0. Then x=0:15/100:z is 0:15/100:0 which is the scalar 0. Scalar 0 in hand for x results in scalar y of 0. So the call trapz(x, y) is a call to trapz(0,0) . The error message suggests that this call trapz(0,0) is being interpreted as the second syntax, trapz(Y, dim), and that trapz() then errors out when it discovers that the dim of 0 is not a valid dimension number. You can avoid this problem by passing in three parameters to trapz(), such as trapz(x, y, 1)"
So, I fixed the problem simply by having ",1" at the end.
The following is what my code looks like. (I am not using cumtrapz anymore)
________________________________________________ %% Define a loop to plot Eficiency (method 1) vs Wavelength A=(theta/(P_t*h*c)); % set a constant to avoid complication
i=1; % Initiate the loop
while i<=length(Sol_wv) % length(Sol_wv) =202 for reference
y=((h*c./Sol_wv(i))-E_loss);
yy = trapz(Sol_wv(1,1:i),((Sol_wv(1,1:i).*10^9)'.*Sol_energy(1:i,1)),1);
Ai(i)=A*(y).*yy; % Return A*y*yy as a vector.
i=i+1;
end
____________________________________
Ian

Community Treasure Hunt

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

Start Hunting!