%CH17      Program for Chapter 17
%
% Draws Black-Scholes surface for cash-or-nothing call

clf

%%%%%%%%%%%%%%%%%%%% Parameters %%%%%%%%%%%%%%%%%%% 
E = 1; A = 2; r = 0.05; sigma = 0.2; T = 1; L = 50;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Svals = linspace(0,2,L);
tvals = linspace(0,T,L);
C = zeros(L,L);

for i = 1:L
    S = Svals(i);
    for j = 1:L-1
        t = tvals(j);
        tau = T-tvals(j);
        d2 = (log(S/E) + (r - 0.5*sigma^2)*(tau))/(sigma*sqrt(tau));
        N2 = 0.5*(1+erf(d2/sqrt(2)));
        C(i,j) = A*exp(-r*tau)*N2;
    end
        % value at expiry 
        C(i,L) = 0.5*A*(1+sign(S-E));
end

[Smat,tmat] = meshgrid(Svals,tvals);
mesh(Smat,tmat,C)
xlabel('t'), ylabel('S'), zlabel('C(S,t)')
