Inconsistent looping issue with trapz()
Show older comments
clear
C1 = 0:1:1;
%C1 = linspace(0,500,1000);
k1 = 1;
k2 = 0;
n1 = 0;
n2 = 0;
nm = 1;
D = 0;
O = 0;
gam = 1e-04.*k1;
alph = 0;
w = linspace(-5,5,1e+06);
delw = w(2)-w(1);
for ii=1:length(C1)
TA = (w+D)./(k1./2);
TB = (w-O)./(gam/2);
xa = 1-1i*TA;
xb = 1-1i*TB;
C = (2.*1i.*sqrt(C1(ii)))./(sqrt(gam).*((xa.*xb)+C1(ii)));
D = (2.*xa)./(sqrt(gam).*((xa.*xb)+C1(ii)));
MC = (abs(C)).^2;
MD = (abs(D)).^2;
Sbb = 2.*pi.*(MC.*(n1+0.5)+MD.*(nm+0.5));
A = delw.*trapz(Sbb)./(2.*pi);
AA(ii)=A;
end
figure(2)
plot(C1,AA);
set(gca,'FontSize',13)
Running the code above would give a straight line from 9.425 to 9.65. If that is the case, then based on my C1 array:
C1 = 0:1:1
that at C1 = 0, A = 9.4247 and at C1 = 1, A = 9.6485. If I remove my for loop and set my C1 simply to be C1 = 0, I recover A = 9.4247. However, setting C1 = 1 returns A = 6.2834. This is problematic and I know that A = 6.2834 should be the correct answer. Furthermore I would like to loop this over a wide range of C1 values:
C1 = linspace(0,500,1000)
But upon running it like this I get a lot of erratic spikes which are wrong and physically absurd. Does this have anything to do with how I'm using my trapz()? Would greatly appreciate help on the matter.
Thanks in advance!
Accepted Answer
More Answers (0)
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!