%Brad Nelson 2/12/12
%Math 126 HW5 #4 
%First BVP!

m=30; %number of nodes

[z,dzdx,n,kap]=param(m); %parameterizes equation

w=zeros(1,numel(dzdx))+2*pi/m; %periodic trapezoid weights

ker=zeros(m,m); %initalizes kernel matrix

for i=1:m; %does diagonal entries
    ker(i,i)=-kap(i)/(4*pi)*dzdx(i)*w(i);
end

%function for evaluating dphi/dny
dphi=@(x,y,n) -1/(2*pi)*(n'*(y-x))/((y-x)'*(y-x));

%now to fill rest of matrix (non-diagonal)
for i=1:m
    for j=1:m
       if ~(i==j)
        ker(i,j)= dphi(z(:,i),z(:,j),n(:,j))*dzdx(j)*w(j);
       end
    end
end

% %for visualizing kernel
% figure;
% imagesc(ker)
% colorbar
% print -depsc 4b.eps;

% now to solve linear system...
A=eye(m); %initializes A matrix
A=A-2*ker; %A matrix for system

soln=@(x,y) cos(x).*exp(y); %actual solution for system

b=-2*soln(z(1,:),z(2,:))'; %solution vector

tau=A\b; %solves linear system for tau vector

x=[0.2; 0.1]; %location to evaluate solution potential at

fx=0; % holds answer at x

%nystrom method of reconstructing answer at x
for i=1:m %riemann sum
    fx=fx+dphi(x,z(:,i),n(:,i))*tau(i)*dzdx(i)*w(i);
end

error=abs(fx-soln(x(1),x(2)));
