function HW05p1

%DRIVER_drawgrid
DRIVER_singletest

return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function DRIVER_drawgrid

n = 5;
h = 1/(n+1);
[XX1,XX2] = meshgrid(linspace(0,1,n+2));
xx = [reshape(XX1,1,numel(XX1));...
      reshape(XX2,1,numel(XX2))];

Iint = find((abs(xx(1,:) - 0.5) < (0.5 - 0.5*h)) & ...
            (abs(xx(2,:) - 0.5) < (0.5 - 0.5*h)));
han = plot([0,1,1,0,0],[0,0,1,1,0],'k',...
            xx(1,:),xx(2,:),'k.',...
            xx(1,Iint),xx(2,Iint),'k.','MarkerSize',40,'LineWidth',2);
set(han(2),'MarkerEdgeColor',0.7*[1,1,1])
axis equal
axis off
%print -dpng fig_grid.png
keyboard

h = 1/(n+3);
[XX1,XX2] = meshgrid(linspace(0,1,n+4));
xx = [reshape(XX1,1,numel(XX1));...
      reshape(XX2,1,numel(XX2))];

Iint = find((abs(xx(1,:) - 0.5) < (0.5 - 1.5*h)) & ...
            (abs(xx(2,:) - 0.5) < (0.5 - 1.5*h)));
han = plot([0,1,1,0,0],[0,0,1,1,0],'k',...
            xx(1,:),xx(2,:),'k.',...
            xx(1,Iint),xx(2,Iint),'k.','MarkerSize',40,'LineWidth',2);
set(han(2),'MarkerEdgeColor',0.7*[1,1,1])
axis equal
axis off
%print -dpng fig_gridhigh.png

return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function DRIVER_singletest

n = 20;
h = 1/(n+1);
D = (1/(h*h))*(2*eye(n+2) - diag(ones(1,n+1),1) - diag(ones(1,n+1),-1));
A = kron(eye(n+2),D) + kron(D,eye(n+2));

[XX1,XX2] = meshgrid(linspace(0,1,n+2));
xx = [reshape(XX1,1,numel(XX1));...
      reshape(XX2,1,numel(XX2))];

Iint = find((abs(xx(1,:) - 0.5) < (0.5 - 0.5*h)) & ...
            (abs(xx(2,:) - 0.5) < (0.5 - 0.5*h)));
Iext = find((abs(xx(1,:) - 0.5) > (0.5 - 0.5*h)) | ...
            (abs(xx(2,:) - 0.5) > (0.5 - 0.5*h)));

plot(xx(1,:),xx(2,:),'k.',...
     xx(1,Iint),xx(2,Iint),'bo',...
     xx(1,Iext),xx(2,Iext),'ro')
 
u       = log((xx(1,:) + 0.5).^2 + (xx(2,:) + 0.75).^2)';
v       = zeros(size(u));
v(Iext) = u(Iext);
v(Iint) = -A(Iint,Iint)\(A(Iint,Iext)*u(Iext));

fprintf(1,'Max error = %10.3e\n',max(abs(u-v)))

figure(1)
subplot(1,2,1)
hold off
mesh(XX1,XX2,reshape(v,n+2,n+2))
title('Approximate solution')

subplot(1,2,2)
hold off
mesh(XX1,XX2,reshape(u-v,n+2,n+2))
title('Errors')
 
keyboard

return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


