% Script to generate matricies for the discrete approximation B to the 
% Black-Schole operator, in financial variables.  The following variables 
% need to be defined prior to running the script:
%     boundary values 0<=sL<sH,
%     the number N of subintervals,
%     time increment dt
%     interest rate r,
%     volatility sigma.
% The script will generate the following:
%     spatial increment ds,
%     vector of N price values s=[sL+ds, ... , sH-ds] (row),
%     vectors a, b, c of coefficients (columns)
%     the matrix B.
ds=(sH-sL)/(N+1);
s=linspace(sL+ds,sH-ds,N);
alpha=dt/ds^2;
beta=dt/ds;
a=(sigma^2*alpha*s.^2-r*beta*s)'/2;
b=(sigma^2*alpha*s.^2+r*dt*ones(size(s)))';
c=(sigma^2*alpha*s.^2+r*beta*s)'/2;
B=spdiags([[a(2:N);0],-b,[0;c(1:N-1)]],[-1,0,1],N,N);
bL=sparse(1,1,a(1),N,1);
bH=sparse(N,1,c(N),N,1);