%Example 1 - cosine wave
%This code corresponds to Example 1.2.
%Electric field as a function of propagation distance is plotted for
%various time-steps. Written by Shiva Kumar, McMaster University. 
clear
hold off
close ALL
format long e
i=sqrt(-1);
T0 = 1e-9;
c=3e8;
L=1;
tmax = L/c;  
tvec = 0:1e-10:tmax;
zvec = 0:0.01:L;

for t = tvec
   m = 1;
   for z = zvec
    u = t-z/c; 
    %Laser is assumed to be turned on at t=0;
    if ((t-z/c) >= 0)
        field(m) = cos(u*10e9);
    else
        field(m) = 0;
    end
    m=m+1;
   end 
   Hp1= plot(zvec, field,'r')
   set(Hp1,'LineWidth',2)
   Ha = gca;
   set(Ha,'Fontsize',16)
   Hx=xlabel('Distance(m) ');
   set(Hx,'FontWeight','bold','Fontsize',16)
   Hy=ylabel('Electric Field Intensity (V/m) ');
   set(Hy,'FontWeight','bold','Fontsize',16)
   title('Forward Propagating Plane wave ');
   pause(0.5) 
   clear field
end
pause(1)
figure(2)
zvec = 0:-0.01:-L;
%Back propagation
for t = tvec
   m = 1;
   for z = zvec
    u = t+z/c; 
    if ((t+z/c) >= 0)
        field(m) = cos(u*10e9);
    else
        field(m) = 0;
    end
    %field(m) = cos(u*10e9);
    m=m+1;
   end 
   Hp1= plot(zvec, field,'r')
   set(Hp1,'LineWidth',2)
   Ha = gca;
   set(Ha,'Fontsize',16)
   Hx=xlabel('Distance(m) ');
   set(Hx,'FontWeight','bold','Fontsize',16)
   Hy=ylabel('Electric Field Intensity (V/m) ');
   set(Hy,'FontWeight','bold','Fontsize',16)
   title('Backward Propagating Plane wave ');
   pause(0.5) 
   clear field
end