Finite Difference2
Finite Difference2
Simone Calogero
December 11, 2015
1 Introduction
The finite difference methods are techniques to find (numerically) approximate solutions
to ordinary differential equations (ODEs) and partial differential equations (PDEs). They
are based on the idea to replace the ordinary/partial derivatives with a finite difference
quotient, e.g., y 0 (x) ≈ (y(x + h) − y(x))/h. The various methods differ by the choice of
the finite difference used in the approximation. We shall present a number of methods by
examples. As this suffices for our applications to the Black-Scholes PDE, we consider only
linear equations.
2 ODEs
Consider the first order ODE
dy
= ay + bt, y(0) = 0, t ∈ [0, T ], (1)
dt
for some constants a, b ∈ R and T > 0. The solution is given by
b at
y(t) = y0 eat + (e − at − 1). (2)
a2
We shall apply three different finite difference methods to approximate the solution of (1).
In all cases we divide the time interval [0, T ] in a uniform partition,
T T
0 = t0 < t1 < · · · < tn = T, tj = j , ∆t = tj+1 − tj =
n n
and define
y(tj ) = yj , j = 0, . . . , n.
1
2.1 Forward Euler method
In this method we do the following approximation of dy/dt at time t:
Exercise 1. Compare the approximate solution with the exact solution for increasing values
of n. Compile a table showing the difference between the approximate solution and the exact
solution at time T for increasing value of n.
Remark 1. Note carefully that when the method is implemented with Matlab, both the com-
puted approximate solution (i.e., the solution to (4)) and the computed exact solution (2)
will contain numerical (truncation) errors!
1
The Matlab codes presented in this text are not optimized. Moreover the powerful vectorization tools
of Matlab are not employed, so as to make the codes easily adaptable to other computer softwares and
languages.
2
2.2 Backward Euler method
This method consists in approximating dy/dt at time t as
dy y(t) − y(t − ∆)
(t) = + O(∆t),
dt ∆t
hence
dy
y(t + ∆t) = y(t) + (t + ∆t)∆t + O(∆t2 ). (5)
dt
The iterative equation for (1) now is
T
yj+1 = yj + (ayj+1 + btj+1 ) , j = 0, . . . , n − 1. (6)
n
This method is called implicit, because the solution at the step j + 1 depends on the solution
at both the step j and the step j + 1 itself. Therefore implicit methods involve an extra
computation, which is to find yj+1 in terms of yj only. For the present example this is a
trivial step, as we have
aT −1 T
yj+1 = 1 − yj + btj+1 , (7)
n n
provided n 6= aT . Here is a Matlab function implementing the backward Euler method for
the ODE (1):
function [time,sol]=exampleODEimp(T,y0,n)
dt=T/n;
sol=zeros(1,n+1);
time=zeros(1,n+1);
a=1; b=1;
sol(1)=y0;
for j=2:n+1
time(j)=time(j-1)+dt;
sol(j)=1/(1-a*dt)*(sol(j-1)+b*time(j)*dt);
end
Exercise 2. Compare the approximate solution obtained with the backward Euler method
with the exact solution and the approximate one obtained via the forward Euler method.
Compile a table for increasing values of n as in Exercise 1.
dy 1 d2 y
y(t + ∆) = y(t) + (t)∆t + (t)∆t2 + O(∆t3 ), (8)
dt 2 dt2
3
and replacing ∆t with −∆t,
dy 1 d2 y
y(t − ∆) = y(t) − (t)∆t + 2
(t)∆t2 + O(∆t3 ). (9)
dt 2 dt
Subtracting the two equations we obtain the following approximation for dy/dt at time t:
Exercise 4. Compare the exact and approximate solutions at time T for increasing values
of n.
3 PDEs
In this section we present three finite difference methods to find approximate solutions to
the one-dimensional heat equation
where u0 is continuous. We refer to t as the time variable and to x as the spatial variable,
since this is what they typically represent in the applications of the heat equation. As before
we let t ∈ [0, T ] and introduce the partition
T T
0 = t0 < t1 < · · · < tn = T, tj = j , ∆t = tj+1 − tj = .
n n
As to the domain of the spatial variable x, we distinguish two cases
(i) x runs over the whole real line, i.e., x ∈ (−∞, ∞), and we are interested in finding an
approximation to the solution u ∈ C 1 (R × (0, T )) such that ∂x u ∈ C 1 (R × (0, T )).
5
(ii) x runs over a finite interval, say x ∈ (xmin , xmax ), and we want to find an approximation
of the solution u which satisfies the boundary conditions2
for some given continuous functions uL , uR . We also require uL (0) = u0 (xmin ), uR (0) =
u0 (xmax ), so that the solution is continuous on the boundary.
In fact, for numerical purposes, problem (i) is a special case of problem (ii), for the
domain (−∞, ∞) needs to be approximated by (−A, A) for A >> 1 when we solve problem
(i) in a computer. Note however that in the finite domain approximation of problem (i), the
boundary conditions at x = ±A cannot be prescribed freely! Rather they have to be given by
suitable approximations of the limit values at x = ±∞ of the solution to the heat equation
on the real line.
By what we have just said we can focus on problem (ii). To simplify the discussion we
assume that the domain of the x variable is given by x ∈ (0, X) and we assign zero boundary
conditions, i.e., uL = uR = 0. Hence we want to study the problem
X X
0 = x0 < x1 < · · · < xm = X, xi = i , ∆x = xi+1 − xi = .
m m
We let
ui,j = u(xi , tj ), i = 0, . . . , m, j = 0, . . . , n.
Hence ui,j is a m + 1 × n + 1 matrix. The j th column contains the value of the approximate
solution at each point of the spatial mesh at the fixed time tj . For instance, the zeroth
column is the initial datum: ui,0 = u0 (xi ), i = 0, . . . n. The rows of the matrix ui,j contain
the values of the approximate solution at one spatial point for different times. For instance,
the row u0,j are the values of the approximate solution at x0 = 0 for different times tj , while
um,j contains the values at xm = X. By the given boundary conditions we then have
u0,j = um,j = 0, j = 0, . . . , n.
We define
∆t T m2
d= = , (19)
∆x2 X2 n
2
These are called Dirichlet type boundary conditions. Other types of boundary conditions can be imposed,
but the Dirichlet type is sufficient for our applications to the Black-Scholes PDE.
6
3.1 Method 1: Forward in time, centered in space
In this method we use a forward difference approximation for the time derivative and a
centered difference approximation for the second spatial derivative:
To visualize the result it is convenient to employ an animation which plots the approx-
imate solution at each point on the spatial mesh for some increasing sequence of times in
7
the partition {t0 , t1 , . . . , tn }. This visualization can be achieved with the following simple
Matlab function:
function anim(r,F,v)
N=length(F(1,:));
step=round(1+N*v/10);
figure
for i=1:step:N
plot(r,F(:,i));
axis([0 1 0 1/2]);
drawnow;
pause(0.3);
end
Upon running the command anim(space,sol,v), the previous function will plot the
approximate solutions at different increasing times with speed v (the speed v must be between
0 and 1).
Let us try the following: [time,space,sol]=heatexp(1,1,50,2500). Hence we solve
the problem on the unit square (x, t) ∈ (0, 1)2 on a mesh of 50 × 2500 points. The value of
the parameter (19) is
d = 1.
If we now try to visualize the solution by running anim(space,sol,0.1), we find that the
approximate solution behaves very strangely (it produces just random oscillations). However
by increasing the number of time steps with [time,space,sol]=heatexp(1,1,50,5000),
so that
d = 0.5,
and visualize the solution, we shall find that the approximate solution converges quickly
and smoothly to u ≡ 0, which is the equilibrium of our problem (i.e., the time independent
solution of (18)). In fact, this is not a coincidence, for we have the following
Theorem 1. The forward-centered method for the heat equation is unstable if d ≥ 1 and
stable for d < 1.
The term unstable here refers to the fact that numerical errors, due for instance to the
truncation and round-off of the initial datum on the spatial grid, will increase in time. On the
other hand, stability of a finite difference method means that the error will remain small at
all times. The stability condition d < 1 for the forward-centered method applied to the heat
equation is very restrictive: it forces us to choose a very high number of points on the time
partition. To avoid such a restriction, which could be very costly in terms of computation
time, implicit methods are preferred, such as the one we present next.
8
3.2 Method 2: Backward in time, centered in space
In this method we employ the backward finite difference approximation for the time derivative
and the central difference for the second spatial derivative (same as before). This results in
the following iterative equation:
Auj+1 = uj , (22)
where uj is the m + 1-dimensional vector with components (u0,j , u1,j , . . . , um,j ) and A is the
m + 1 × m + 1 matrix with non-zero entries given by
The matrix A is invertible, hence we can invert (22) to express uj+1 in terms of uj as
This method is unconditionally stable, i.e., it is stable for all values of the parameter d.
We can test this property by using the following Matlab function, which solves the iterative
equation (23):
function [time,space,sol]=heatimp(X,T,m,n)
dt=T/n; dx=X/m;
d=dt/dx^2;
sol=zeros(m+1,n+1);
time=zeros(1,n+1);
space=zeros(1,m+1);
A=zeros(m+1,m+1);
A(1,1)=1; A(m+1,m+1)=1;
for j=2:n+1
time(j)=time(j-1)+dt;
end
for i=2:m+1
space(i)=space(i-1)+dx;
end
for i=1:m+1
sol(i,1)=exp(X^2/4)-exp((space(i)-X/2)^2);
end
sol(1,:)=0; sol(m+1,:)=0;
for i=2:m
9
A(i,i-1)=-d;
A(i,i)=1+2*d;
A(i,i+1)=-d;
end
for i=2:n+1
sol(:,i)=inv(A)*sol(:,i-1);
end
10
Note that in this new formulation the time t = T corresponds to t = 0 in the old formulation,
i.e., to the “present time”. The following project consists of two parts.
Part 1 (0.5 points)
• Write a short introduction to the Black-Scholes PDE, max 3 pages; in this part you
should discuss in particular the properties of the Black-Scholes PDE which are needed
for the numerical analysis in the second part of the project.
• Write a finite difference approximation for the PDE (25a) on the domain (x, t) ∈
(0, X) × (0, T ) with initial data g(x) at time t = 0 and boundary conditions u(0, t) =
uL (t), u(X, t) = uR (t). Use an implicit method.
• Write a Matlab function that implements the finite difference approximation derived
in part 1. The parameters r, σ, X, T , must appear as input variables of your function,
while the boundary/initial data may appear in the code itself.
• Plot the solutions at time t = T in the case of call and put options. Focus on nearly-
at-the-money options, say x/K ∈ (4/5, 6/5). Show the exact solution on the same
plot. Run several tests for different values of the parameters. Discuss possible sources
of error and how to remove them.
• Compute solutions for call and put options for different values of volatility and show in a
plot how the the solution at time t = T depends on volatility (fix the other parameters,
including the initial price of the stock close to the strike price). Do a similar analysis
to depict the depence on the time of maturity and discuss your findings.
Include your matlab codes in an appendix. The deadline to submit the project is January
7th. You can work on the project with max one other student.
11