% Exercise 2 Spring 2007
% K. A. Connor 15 April 2007

% Frequency
f=400e6;T=1/f;
w=2*pi*f;
c=3e8;

% For plotting, we also need the time
t=0;

% Medium One
eps1=4.9;
u1=c/sqrt(eps1);
beta1=w/u1;lam1=u1/f;
eta1=120*pi/sqrt(eps1);

% Medium Two
eps2=1;
u2=c/sqrt(eps2);
beta2=w/u2;lam2=u2/f;
eta2=120*pi/sqrt(eps2);

% Scales for Plotting
z1=[-1:.01:0].*3.*lam2;
z2=[0.01:.01:1].*3.*lam2;
z=[z1 z2];

% Reflection & Trans Coefficients
gam=(eta2-eta1)/(eta2+eta1);
tau=2*eta2/(eta2+eta1);

% Field in Region 1
e1=1.*exp(-j.*beta1.*z1)+gam.*exp(+j.*beta1.*z1);
e1p=exp(-j.*beta1.*z1);e1n=gam.*exp(+j.*beta1.*z1);
h1=(1.*exp(-j.*beta1.*z1)-gam.*exp(+j.*beta1.*z1))./eta1;
h1p=(1.*exp(-j.*beta1.*z1))./eta1;h1n=(-gam.*exp(+j.*beta1.*z1))./eta1;

% Field in Region 2
e2=tau.*exp(-j.*beta2.*z2);
h2=tau.*exp(-j.*beta2.*z2)./eta2;

% Fields in Both Regions
e=[e1 e2];
h=[h1 h2];

% Plotting (to see the incident and reflected waves separately, remove the
% comment symbol from the lines that begin hold on
plot(z,real(e.*exp(j.*w.*t)));grid;title('Electric Field');xlabel('z in meters')
% hold on; plot(z1, real(e1p.*exp(j.*w.*t)),'r');plot(z1, real(e1n.*exp(j.*w.*t)),'g');
figure;
plot(z,real(h*exp(j.*w.*t)));grid;title('Magnetic Field');xlabel('z in meters')
% hold on; plot(z1, real(h1p.*exp(j.*w.*t)),'r');plot(z1, real(h1n.*exp(j.*w.*t)),'g');