Plotting with multiple nested arrays issue with trapz() function.

2 views (last 30 days)
clear
%g = 1;
k = 0.2;
no = 0;
nm = 0;
Del = 1;
Ohm = -1;
gam = 0.2;
w = (-10:0.01:10);
g = (0:0.01:20);
xat = 1 - (1i.*(w + Del).*(2./k));
xbt = 1 - (1i.*(w - Ohm).*(2./gam));
for u=1:length(g)
C = sqrt((4.*(g(u).^2))./(k.*gam));
Ca(u) = C; %C-array
c = (2.*1i.*Ca(u))./(sqrt(gam).*(xat(u).*xbt(u)+(Ca(u).^2))); %power spectrum coefficient
ca(u)= c; %c-array
d = (2.*xat(u))./(sqrt(gam).*(xat(u).*xbt(u)+(Ca(u).^2))); %power spectrum coefficient
da(u) = d; %d-array
Sbb = (abs(ca(u)).^2).*(no+(1/2))+(abs(da(u)).^2).*(nm+(1/2)); %power spectrum
Q = ((1./(2.*pi)).*trapz(w,Sbb))-0.5; %phonon population
Qa(u) = Q; %Q-array
end
figure(1)
plot(Ca,Qa)
Greetings!
As you can see, I am trying to plot a graph of Qa versus Ca. But upon running, I got an error saying: ORDER contains an invalid permutation index. I suspect it's the trapz() function not recognizing my Sbb. Given how I am trying to store lots of arrays, they got nested inside the loop and I think I lost track of what I really needed to loop over. This is really important to me and I would appreciate any form of help!
Thank you very much in advance!

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jul 2017
When you go to trapz, you have a vector of w and a scalar fractional Sbb.
trapz can be called with two arguments in a few different ways: the second argument Sbb can be an array the same size as w; Or w and Sbb can be "compatible" sizes (e.g., Sbb could be an array and w could be a vector with the same number of elements as the number of rows in Sbb); Or Sbb can be positive integer scalar that indicates the dimension number of w to work over. You are passing in a scalar so it thinks you are trying to pass a dimension number, but the dimension number you are providing is not a positive integer, so you get an error.
Perhaps you should be building an array of Sbb values, and then have your trapz after the loop ?
  2 Comments
Alvin
Alvin on 20 Jul 2017
Edited: Walter Roberson on 20 Jul 2017
w = (-10:0.01:10);
g = (0:0.01:20);
xat = 1 - (1i.*(w + Del).*(2./k));
xbt = 1 - (1i.*(w - Ohm).*(2./gam));
for u=1:length(g)
C = sqrt((4.*(g(u).^2))./(k.*gam)); %Cooperativity
Ca(u) = C;
c = (2.*1i.*Ca(u))./(sqrt(gam).*(xat(u).*xbt(u)+(Ca(u).^2))); %power spectrum coefficient
ca(u)= c; %c-array
d = (2.*xat(u))./(sqrt(gam).*(xat(u).*xbt(u)+(Ca(u).^2))); %power spectrum coefficient
da(u) = d; %d-array
Sbb = (abs(ca(u)).^2).*(no+(1/2))+(abs(da(u)).^2).*(nm+(1/2));
Sbba(u) = Sbb; %Sbb-array
%Qa(u) = Q;
end
Q = ((1./(2.*pi)).*trapz(w,Sbba))-0.5; %phonon population
Hello Mr Roberson!
Firstly, thanks for your reply! I think I understand what you're saying, it appears I did not create an array of Sbb before including it into my trapz() function.
I have created an array for Sbb (called Sbba(u)) as shown above in this message while including my trapz() outside the loop. Upon computing, I encounter no errors, however my Q function has only one value instead of a 1x2001 array. This shouldn't be the case since Sbba is an array so I should be having an array of Q values (Qa(u)).
Given the circumstance, I then proceed to put Q back into the loop and defining the array Qa(u) = Q. But this leads me back to the same error as before. Namely: ORDER contains an invalid permutation index
I apologize if I'm not understanding you correctly. I hope this clarifies my progress.
Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!