%%
%Laplace Equation BVP example solution for analytical series given in 
%Cheng Example 4-6
%EP440 Example #1 of 2 for Numerical Project #1

%%
%Set domain parameters
%Clear variables first
clear;
%Numerical Domain Size
Nx=200;
Ny=100;
%Physical Domain Size
a=2;
b=1;
%Resulting Domain Resolution
dx=a/(Nx-1);
dy=b/(Ny-1);
%Define box
x=0:dx:a;
y=0:dy:b;

%%
%Construct series solution
%Use a meshgrid, because it is cool
[Y,X]=meshgrid(y,x);
%Max N in Series
Nterms=100;
%Initialize solution array
V=zeros(Nx,Ny);
%Define Vo at left boundary
Vo=1;
for n=1:2:Nterms+1  %Note: Odds only!
    
    %Define term n
    Vterm=(4*Vo/(pi)).*(1/n).*exp(-pi*n.*X./b).*sin(pi*n.*Y./b);
    
    %Sum term to existing solution (initial V=0 if n=1)
    V=V+Vterm;
    
end;

%%
%Plot otuput from series solution
figure(1);
imagesc(x,y,V'); 
axis xy;
axis image;
colorbar; 
colormap(gray(1024));
xlabel('X'); ylabel('Y');
title(['Solution at n=',num2str(n-2)]);