% Script to solve the American BSPDE
dt=T/M;
genB				%Generate matricies and coefficients
ML=speye(N)-B/2;
MR=speye(N)+B/2;
PSOR(ML,tol);			    %Initialize PSOR
exbd=zeros(1,M);		 %For tracking the exercise boundary
t=T;
v=exercise(s,t)';		%v^M=phi(s,T)
z=zeros(N,1);
for m=M-1:-1:0			%Time-step loop
  q=-(MR*v+(vL(sL,t,T)*bL+vH(sH,t,T)*bH)/2);	   %using previous values
  t=m*dt; phi=exercise(s,t)';			                  %update t and phi
  q=q+ML*phi-(vL(sL,t,T)*bL+vH(sH,t,T)*bH)/2;   %Finish constructing q
  z=PSOR(q,z);                                  %Solve LCP; z0=old z
  v=z+phi;
  exbd(m+1)=s(min(find(v>phi)));                %locate exercise bndry
end
exbd=[exbd,K];
s=[sL,s,sH];                 	%Include boundaries and
v=[vL(sL,t,T),v',vH(sH,t,T)]; %Convert results to rows