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; // exercise price r = 0.04; // interest rate D = 0.12; // dividend rate sigma = 0.4; // volatility
L = 2;and parameters of the partitions:
// space variable n = 20; h = L/n; // time variable 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);
// x = -L, i.e., price close to zero function [phi]=phi(tau) phi=0; endfunction // x = L, i.e., price close to infinity function [psi]=psi(tau) psi=E*exp(alpha*L + beta*tau).*(exp(L - D*tau) - exp(-r*tau)); endfunctionand the initial condition:
// initial condition function [u0]=u0(x) u0=E*exp(alpha*x).*max(0, exp(x)-1); endfunction
sol = zeros(2*n + 1,m+1);and grid points in time and in space:
x = -L:h:L; tau = 0:k:T;
a = -0.5*(sigma^2)*k/(h^2); // alongside the diagonal b = 1 - 2*a; // on the diagonalWhen computing the first time level (the values at time k), we have the following right hand side:
rhs = sol(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 in the file gs.sci:
// ----------------------------------------------------------- // Gauss-Seidel method // for solving a special symmetric tridiagonal system // ----------------------------------------------------------- // // gs(a,b,rhs,v0,eps) // // 3-diagonal matrix with: // a - above and below the diagonal // b - on the diagonal // // rhs - right hand side of the system - a column vector // v0 - initial interation - a column vector // // eps - criterion for ending the iterations: norm(Av-rhs)<=eps // // v - aproximation of the solution (rewritten in the interations) function [v] = gs(a,b,rhs,v0,eps) // matrix A pom = length(v0); A(1, 1) = b; for i = 2 : pom A(i, i) = b; A(i, i - 1) = a; A(i - 1, i) = a; end; // loop until the condition norm(Av-ps)<=eps is satisfied v = v0; while norm(A*v - rhs) > eps v(1) = (rhs(1) - a*v(2))/b; for i = 2:pom-1 v(i) = (rhs(i) - a*(v(i-1) + v(i+1)))/b; end v(pom) = (rhs(pom) - a*v(pom - 1))/b; end; endfunction