% Tony Hyun Kim
% 8.07 PS#3, Problem 4c
z = linspace(0,1,101);
Z = length(z); 
Ez = zeros (1,Z);

M = 211;
N = M;

X = 1/2;
Y = 1/2;

for i = 1:Z
    tic;
    % Compute the sums
    sum = 0;
    for m = 1:2:M
        for n = 1:2:N
            kappa = pi*sqrt(m^2+n^2);
            sum = sum + 16/(m*n*pi^2)*1/cosh(kappa/2)*...
                    sin(m*pi*X)*sin(n*pi*Y)*cosh(kappa*(z(i)-1/2));
        end
    end
    Ez(i) = sum;
    toc
end

plot(z,Ez);
xlabel('Position along the z-direction (arb. units; full length [0 1])');
ylabel('Vertical electric field, E_z (arb. units; BCs set to Ez = 1)');
title('Electric field along the line (x,y,z) = (0.5,0.5,z)');
text(0.4,0.6,'E_z(0.5,0.5,0.5) = 1/3','fontsize',14);
Ez(51)