% Schneider book pg. 41 plus snapshot graph
% FDM version with Euler forward
clear            % this is an FDM version of max1
size = 200;      % 2x # of points in max1  

ez=zeros(size,1);
hy=zeros(size,1);
ezn=ez;
hyn=hy;

maxTime =200;   % 2x # for max1


imp0 = 377; % imp0 = impedance free space, sqrt(u0/e0),  Sc = 1, dt = 1/2 dt from max1, dx = 1/2 dx from max 1.
figure(1)
plot(0,0)
hold on
for n=1:maxTime
    % save ez and hy values for times n and n-1
    eznm1=ezn;
    ezn=ez;
    hynm1=hyn;
    hyn=hy;
   
    for j=2:size-1
        hy(j) = hynm1(j) + 2*(ezn(j+1) - ezn(j-1)) / (2*imp0);
    end

    for j=2:size-1
        ez(j) = eznm1(j) + 2*(hyn(j+1) - hyn(j-1)) * imp0 / 2;
    end

      
    if mod(n-1,50)==0
       plot(ez(1:size) + n/50)
    end   
    
    % boundary conditions
    ez(size) = ez(size-1);   % PMC
    hy(size) = hy(size-1);   % PEC  both make a ABC
    
   
    ez(1) = exp(-((n-1)-60)^2/400);
    hy(1) = -ez(1)/imp0;
 
end
hold off

figure(2)
plot(ez)
hold on
plot(hy*imp0, 'Color','red')
hold off

figure(3)
z=zeros(size,1);
plot3(1:size,z, ez)
hold on
plot3(1:size, hy*imp0, z, 'Color','red')
hold off
xlabel('x')
ylabel('y')
zlabel('z')





    


