MATLAB option pricing using the Black-Scholes transformation to Heat Equation

288 Views Asked by At

I'm currently working on a project that requires me to use MATLAB to find and plot the value of a call option against share price using explicit finite difference method. As many times as I've re-derived the heat equation from Black-Scholes as well as the required initial and boundary conditions my MATLAB code isn't giving me the correct solution. I have a feeling it may be how I'm implementing the conditions within the code but am unsure where the error is being made. Any help or insight is much appreciated, cheers!

E = 50;                 %exercise price
X = log(250/E);         %max share price
sigma = 0.3;            %volatility
T = sigma^2*1/2;        %time interval
nx = 21;                %number of steps in x direction
nt = 201;               %number of steps in t direction
r = 0.3;                %rate
k = 2*r/sigma^2;
dx = 2*X/(nx-1);
dt = T/(nt-1);

x = -X:dx:X;
t = 0:dt:T;
alpha = dt/dx^2;

%Initialising U mesh, initial and boundary conditions
u = zeros(nx,nt);
u(:,1) = max(exp(1/2*(k+1)*x)-exp(1/2*(k-1)*x),0);
u(1,:) = 0;
u(nx,:) = exp(1/2*(k+1)*X+1/4*(k+1)^2*t)-exp(1/2*(k-1)*X+1/4*(k-1)^2*t);

%Implementation of explicit method
for j=1:nt-1    %time loop
   for i=2:nx-1   %asset price loop
       u(i,j+1) = u(i,j)+alpha*(u(i+1,j)-2*u(i,j)+u(i-1,j));
   end
end

for j=1:nt
   for i=1:nx
       V(i,j)=E*exp(-1/2*(k-1)*(i-1)*dx/2-1/4*(k+1)^2*(j-1)*dt)*u(i,j);
   end
end

   plot(E*exp(x), V(1:end,1:end));
   ylabel('Value');
   xlabel('Asset price');
   grid;