%Electric field distribution around a current sheet
%Gaussian excitation
clear
ke=50;
ks = ke/2;
nsteps=90;
c0=3e8;
dx=0.01;
dt=dx/(2*c0);
cc = c0*dt/dx;
T = (nsteps*dt)/8;
f0 = 1/T;
xvec = -ke*dx/2+dx:dx:ke*dx/2;
xmax = dx*ke/2;
xmin = -xmax;

ex = zeros(1,ke);
hy=ex;

t0=20;
spread=8;
figure(1)
%start loop
%M=moviein(nsteps)
for tn=1:nsteps
    %Ex
    for k=2:ke-1
        ex(k) = ex(k)+cc*(hy(k-1)-hy(k));
    end
    
    %source
    ex(ks)=ex(ks)+exp(-0.5*((tn-t0)/spread)^2);
    %ex(ks)=ex(ks)+sin(2*pi*tn*dt*f0);
    
    %Hy
    for k=1:ke-1
        hy(k) = hy(k)+cc*(ex(k)-ex(k+1));
    end
    plot(xvec,ex);
    axis([xmin xmax -2 2])
    xlabel('Distance (m)')
    ylabel('Ex (V/m)')
%     plot(ex);
%     axis([1 ke -2 2])
    pause(0.5)
    %M(:,t) = getframe;
end
%movie(M,1);

    