Asked by Alvin
on 27 Jul 2017

clear

g = 1000;

k = 20;

no = 0;

nm = 40;

Del = -(10.^2);

Ohm = (10.^2);

gam = 0.0032;

C = sqrt((4.*(g.^2))./(k.*gam));

w = (-1500:1:1500);

xat = 1 - (1i.*(w + Del).*(2./k));

xbt = 1 - (1i.*(w - Ohm).*(2./gam));

c = (2.*1i.*C)./(sqrt(gam).*(xat.*xbt+(C.^2))); %power spectrum coefficient

d = (2.*xat)./(sqrt(gam).*(xat.*xbt+(C.^2))); %power spectrum coefficient

Sbb = (abs(c).^2).*(no+(1/2))+(abs(d).^2).*(nm+(1/2)); %power spectrum

A = cumtrapz(w,Sbb);

LA = A(length(A));

figure(1)

plot(w,Sbb);

Should one run the code above, one will find a plot with two sharp peaks. At g = 0, there will be one peak; With increasing g, there will be a gradual splitting of the peak into the eventual two as shown in the setting above (g = 100). Moreover for each g setting, I wish to find the area under the curve of Sbb versus w (hence my use of the cumtrapz() function). And given how cumtrapz() works, I made LA just to obtain the last entry of my A (area) array (which would give the correct area value) for some g value (g = 1000 in the above case). As it stands, LA has only one value given a particular g value, whereas A is an array since it is dependent on w and w is an array.

My question is of the following: I wish to plot a graph of LA vs C (C is dependent on g) with C varying over several values (and hence g varies as well). So, clearly I will have to loop over g and make an array to store all my C values. But since c and d depends on xat, xbt, and C, they will have to be included in the loop as well since my C is an array (which is fine). The difficulty comes in introducing w into the loop (so a double loop with g and w). In short, I ended up having to make a cell array to store all the values but became confused over the long run. I would appreciate any help in resolving this issue (for all I know I might be overthinking it).

Thank you very much in advance!

Answer by Saurabh Gupta
on 1 Aug 2017

Most of your original code is element-wise operation on vector/matrix elements, so I don't see any reason to use a loop to handle 'g' as a vector. If you set 'g' as follows, your original code should be able to evaluate all values up to the 'cumprapz' statement.

>> g = (0:1.25:999)';

Now, when you reach 'cumprapz', you will notice an error regarding dimensions mismatch because 'w' is 1x3001 and 'Sbb' is 800x3001, whereas it requires the first dimension of 'Sbb' to match the length of 'w' and it causes the error. You can loop through the first dimension of 'Sbb' and run 'cumprapz' command 800 times similar to following code, and plot corresponding results.

A = zeros(size(Sbb));

for i = 1:size(Sbb,1)

A(i,:) = cumtrapz(w, Sbb(i,:));

end

Hope this helps!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Alvin (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/350535-on-double-looping-and-confusion-with-cell-arrays-and-cumtrapz-function#comment_472429

Sign in to comment.