
% Numerical approximation to the solution of Poisson's equation 
%
%   U_xx + U_yy = pf(x,y)
%
% over the square [0,a]x[0,a] with Dirichlet boundary conditions  
%
%   U(x,y) = gf(x,y)
%
% Uses a uniform mesh with (n+2)x(n+2) total points 
% (i.e, n x n interior grid points).

a = 1;
n = 79;

pf = @(x,y) -5*pi^2*sin(pi*x) .* cos(2*pi*y);
gf = @(x,y) sin(pi*x) .* cos(2*pi*y);

tic();
[u, x, y] = myfd2poisson(pf, gf, a, n);
toc()

% Plot solution
figure;
surf(x, y, u);
h = x(1,2) - x(1,1);
title(strcat('Numerical Solution to Poisson Equation, h=', num2str(h)));
xlabel('x');
ylabel('y');
zlabel('u(x,y)');

% Plot error
figure;
surf(x, y, u - gf(x,y));
h = x(1,2) - x(1,1);
title(strcat('Numerical Solution to Poisson Equation, h=', num2str(h)));
title(strcat('Error, h=', num2str(h)));
xlabel('x');
ylabel('y');
zlabel('error');

