% Script for Crank-Nicolson finite difference method in financial
% variables using explicit boundary values.  The variables sL, sH, N, M, r,
% sigma, and T should be set at the command line before running the script.
% (r, sigma, and K should be global).  Separate m-files vT, vL, and vH give
% initial and boundary values.
dt=T/M;
genB                            %Generate matricies and coefficients
Mat=speye(N)-B/2;
[L,U]=lu(Mat);
Mat=speye(N)+B/2;
v=vT(s)';                       %Initialize
t=T;
for m=M-1:-1:0                  %Time-step loop
	rhs=Mat*v+(vL(sL,t,T)*bL+vH(sH,t,T)*bH)/2;
	t=m*dt;
	rhs=rhs+(vL(sL,t,T)*bL+vH(sH,t,T)*bH)/2;
	v=U\(L\rhs);
end
s=[sL,s,sH];                    %Include boundaries and
v=[vL(sL,t,T),v',vH(sH,t,T)];   %Convert results to rows