Problem saving vectors in a for loop

조회 수: 2 (최근 30일)
Travis
Travis 2014년 4월 24일
댓글: Travis 2014년 4월 25일
Hi all,
I am having trouble saving the vectors that I generate using a for loop. Running the code, that I have provided below, updates the 3D plot for each iteration. I would like to save each vector for each iteration and plot them all together. I believe the problem lies with the counter "xx" but I am unsure how to get around this since I want to save the vector "C" for each iteration which does not use "xx" as a counting variable. I have tried indexing "C" with "xx" to save the vector for each iteration Any help would be greatly appreciated.
for xx=0:5:350;
d2(1:71)=xx;
lambda=400:5:750;
d1=0.34;
% Define Refractive Indices for Materials:
% Air:
n_0=1;
% Graphene present & not present:
n_1=2.6-1.3i;
n_11=1;
% SiO2 (400-750nm):
n_2=[1.4701 1.4696 1.4691 1.4686 1.4681 1.4676 1.4672 1.4668 1.4663...
1.466 1.4656 1.4652 1.4648 1.4645 1.4641 1.4638 1.4635 1.4632 1.4629...
1.4626 1.4623 1.4621 1.4618 1.4615 1.4613 1.461 1.4608 1.4606 1.4603 1.4601...
1.4599 1.4597 1.4595 1.4593 1.4591 1.4589 1.4587 1.4586 1.4584 1.4582 1.458 1.4579...
1.4577 1.4576 1.4574 1.4572 1.4571 1.457 1.4568 1.4567 1.4565 1.4564 1.4563 1.4561 1.456...
1.4559 1.4558 1.4556 1.4555 1.4554 1.4553 1.4552 1.4551 1.455 1.4549 1.45475 1.4546 1.4545 1.4544 1.4543 1.4542];
% Silicon (400-750nm):
n_3=[5.554925088 5.422277418 5.302243523 5.193611448 5.095262656 5.006167192 4.925378971...
4.852031185 4.785331843 4.724559421 4.669058654 4.618236434 4.57155784 4.528542299 4.488759852...
4.451827565 4.417406046 4.3851961 4.354935495 4.326395864 4.299379722 4.273717609 4.249265359 4.225901491...
4.203524726 4.182051619 4.161414333 4.141558515 4.12244131 4.1040295 4.086297753 4.069227011 4.052802995 4.03701484...
4.021853804 4.00731224 3.9933825 3.980056106 3.967322993 3.955170878 3.943584754 3.932546514 3.922034693 3.912024332 3.902486974...
3.893390776 3.884700748 3.876379115 3.868385805 3.860679058 3.853216158 3.845954294 3.838851539 3.831867956 3.824966827 3.818116006 3.811289394...
3.804468544 3.797644378 3.790819041 3.784007871 3.777241493 3.770568042 3.7640555 3.75779417 3.751899264 3.746513614 3.741810515 3.737996688 3.735315359 3.734049478];
% Define r constants:
% For graphene on Si02:
r1=(n_0-n_1)./(n_0+n_1);
r2=(n_1-n_2)./(n_2+n_1);
r3=(n_2-n_3)./(n_2+n_3);
% Plain Si02 no graphene:
r11=(n_0-n_11)./(n_0+n_11);
r21=(n_11-n_2)./(n_2+n_11);
r31=(n_2-n_3)./(n_2+n_3);
% Define phase shifts:
psi1=(2*pi*n_1*d1)./lambda;
psi2=(2*pi*n_2.*d2)./lambda;
psi11=(2*pi*n_11*d1)./lambda;
psi21=(2*pi*n_2.*d2)./lambda;
% Intensity equation with graphene (I1) and without (I2):
I1=abs((r1.*exp(1i.*(psi1+psi2))+r2.*exp(-1i.*(psi1-psi2))+r3.*exp(-1i.*(psi1+psi2))...
+r1.*r2.*r3.*exp(1i.*(psi1-psi2))).*(exp(1i.*(psi1+psi2))+r1.*r2.*exp(-1i.*(psi1-psi2))+r1.*r3.*exp(-1i.*(psi1+psi2))+r2.*r3.*exp(1i.*(psi1-psi2))).^-1).^2;
I2=abs((r11.*exp(1i.*(psi11+psi21))+r21.*exp(-1i.*(psi11-psi21))+r31.*exp(-1i.*(psi11+psi21))...
+r11.*r21.*r31.*exp(1i.*(psi11-psi21))).*(exp(1i.*(psi11+psi21))+r11.*r21.*exp(-1i.*(psi11-psi21))...
+r11.*r31.*exp(-1i.*(psi11+psi21))+r21.*r31.*exp(1i.*(psi11-psi21))).^-1).^2;
% Contrast equation:
C=(I2-I1)./I2;
figure(1);plot3(lambda,d2,C);
figure(2);plot(lambda,C);
figure(3);plot(d2,C);
end

채택된 답변

dpb
dpb 2014년 4월 24일
One way to rearrange the loop...
Instead of
for xx=0:5:350;
d2(1:71)=xx;
...
write
xx=0:5:350;
for ix=1:length(xx)
d2(1:71)=xx(ix);
....
Then you've got an index variable that increments by one each pass. You will, of course, need to change any other references to xx to refer to the array instead of the index variable before; I didn't try to read the rest of the code. d2(1:71)=xx;
  댓글 수: 1
Travis
Travis 2014년 4월 25일
Awesome, man thanks so much! I had to index a few things in the loop to save everything but it worked out well.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by