
% This is a function to compute harmonic measure
% a  a X 1 rectangle using sparse linear algebra. We
% assume a is a positive integer and the grid size
% is 1/m where m is also a positive integer.

function out = hm_linalg(a,m)
   tic;
   L1=(2*m +1)
   L2=(2*a*m+1)
   L=L1*L2
   M=eye(L);
   Y=zeros(L,1);
   % left side
   for k=1:L1
     Y(k)=1;
   end
   % interior
   for k=2:L1-1
     for j=1:L2-2
        M(L1*j+k,L1*(j+1)+(k))=-.25;
        M(L1*j+k,L1*(j-1)+(k))=-.25;
        M(L1*j+k,L1*(j)+(k+1))=-.25;
        M(L1*j+k,L1*(j)+(k-1))=-.25;
     end
   end
   figure
   spy(M)
   X = M\Y;
   prob=X((L+1)/2)
   Z=reshape(X,[L1,L2]);
   depth=100;
   figure;
   hold on;
   title('Percent chance of hitting left side');
   image(depth*Z);
   axis equal;
       %colormap(flipud(jet(depth)));
       colormap(jet(depth));
       %colormap(hsv(depth));
       %colormap(gray(depth));
       %colormap(flipud(winter(depth)));
       % add a legend saying how colors give iterates
       colorbar;
   toc;

return


