We need to impose boundary conditions for x = -L and x = L. These value of x correspond to stock prices which are very small, close to zero (the case of x = -L) and prices which are very high and approach infinity (the case of x = L). For these limiting values we will use the following approximations:
The first approach (explicit scheme) seems to be easier. We have an initial condition. Using these values, we compute the second time level. These are used to compute the next time level, etc. However, to ensure a convergence of the method, a certain condition on time and space steps is required. The condition may practically lead to a necessity of using a very small time step - because of a convergence, not because that we actually need the solution in so many close times.
The second approach (implicit scheme) leads to solving a system of linear equations on each time level.
E <- 50 r <- 0.04 D <- 0.12 sigma <- 0.4
L <- 2and parameters of the partitions:
n <- 20 h <- L/n T <- 1 m <- 12 k <- T/m
alpha <- (r-D)/(sigma^2) - 0.5 beta <- (r+D)/2 + (sigma^2)/8 + ((r-D)^2)/(2*sigma^2)
phi <- function(tau) 0 psi <- function(tau) E*exp(alpha*L + beta*tau)*(exp(L - D*tau) - exp(-r*tau))and the initial condition:
u0 <- function(x) E*exp(alpha*x)*pmax(0, exp(x)-1)
sol <- matrix(0, nrow=2*n + 1, ncol=m+1)and grid points in time and in space:
x <- seq(from=-L, by=h, to=L) tau <- seq(from=0, by=k, to=T)
a <- -0.5*(sigma^2)*k/(h^2) b <- 1 - 2*aWhen computing the first time level (the values at time k), we have the following right hand side:
rhs <- sol[c(2:(2*n)),1] rhs[1] <- rhs[1] - a*phi(k) rhs[2*n-1] <- rhs[2*n-1] - a*psi(k)
You can use the function defined as follows, which solves a special symmetric tridiagonal system with a above and below the diagonal and b on the diagonal (rhs is the right hand side of the system, v0 is the initial iteration and N is number of iterations.
gs <- function(a,b,rhs,v0,eps,N) { n.system <- length(v0) v <- v0 for (iter in 1:N) { v[1] <- (rhs[1] - a*v[2])/b for (i in 2:(n.system-1)) v[i] <- (rhs[i] - a*(v[i-1] + v[i+1]))/b v[n.system] <- (rhs[n.system] - a*v[n.system - 1])/b } gs <- v }
vOld <- v[1] vGS <- (rhs[1] - a*v[2])/b; v[1] <- omega*vGS + (1-omega)*vOldIn the same way for other ones.