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/(c*dt+dz)).*(ex(Nx,Nz)-ex3(Nx));
ez(1,1:Nz)=0;
ez(Nx+1,1:Nz)=0;
% ex(1:Nx,1)=ex2+((c*dt-dz)/(c*dt+dz)).*(ex(1:Nx,2)-ex1);
% ex(1:Nx,Nz+1)=ex4+((c*dt-dz)/(c*dt+dz)).*(ex(1:Nx,Nz)-ex3);
% ez(1,1:Nz)=0;
% ez(Nx+1,1:Nz)=0;
etotal(1:Nx,n)=ex(1:Nx,10);
etr(1:Nx,n)=ex(1:Nx,390);
ein(1:Nx,n)=ex_air(1:Nx,10);
if mod(n,10)==0
rtime=num2str(round(n*dt/1.0e-9));
figure(1);
subplot(2,1,1);
mesh(ex);
axis([0 400 0 12 -1 1]);
caxis([-1 1]);
title(['time = ',rtime,' ns']);
subplot(2,1,2)
imagesc(ex);
caxis([-1 1]);
pause(0.01)
end
end
ere=etotal-ein;
vin=sum(dx.*ein);
vre=sum(dx.*ere);
vtr=sum(dx.*etr);
figure(2);plot(t,vin);axis([0 nmax*dt -0.2 0.2]);
title('入射电压');xlabel('t (s)');ylabel('V');
figure(3);plot(t,vre);axis([0 nmax*dt -0.2 0.2]);
title('反射电压');xlabel('t (s)');ylabel('V');
figure(4);plot(t,vtr);axis([0 nmax*dt -0.2 0.2]);
title('透射电压');xlabel('t (s)');ylabel('V');
M=0:nmax-1;
f=M/(nmax*dt);
Vin=fft(vin);
Vre=fft(vre);
Vtr=fft(vtr);
figure(5);plot(f,abs(Vin));axis([0 1.0e9 0 10]);
title('入射电压');xlabel('f (Hz)');ylabel('|Vre|');
figure(6);plot(f,abs(Vre));axis([0 1.0e9 0 10]);
title('反射电压');xlabel('f (Hz)');ylabel('|Vtr|');
figure(7);plot(f,abs(Vtr));axis([0 1.0e9 0 10]);
title('透射电压');xlabel('f (Hz)');ylabel('|Vin|');
s11=Vre./Vin;
s21=Vtr./Vin;
figure(8);
subplot(3,1,1),plot(f,abs(s11));axis([0 1.0e9 0 2]);xlabel('f (Hz)');ylabel('S11');
subplot(3,1,2),plot(f,abs(s21));axis([0 1.0e9 0 2]);xlabel('f (Hz)');ylabel('S21');
figure(9);
plot(f,abs(s11).^2+abs(s21).^2);axis([0 1.0e9 0 2]);xlabel('f (Hz)');