function [u,xgrid] = explicit_fd( payoff_fn, u_m_inf, u_p_inf, r, sigma, xLeft, xRight, Nx, tau_Max, M ) % % Solves the diffusion equation using an explicit finite-difference model % % Input: % tau_Max = the maxium time to integrate the diffusion equation. % % See the Epage 156 in Wilmotts book: The Mathematics of Financial Derivatives % % Written by: % -- % John L. Weatherwax 2008-06-11 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- xgrid = linspace( xLeft, xRight, Nx+1 ); dx = (xRight-xLeft)/Nx; dt = tau_Max/M; a = dt/(dx*dx); if( 0 && a>=0.5 ) fprintf('a = %10.6f\n',a); error('a to large .... this explicit method may not be stable ... increasing M may help ...\n'); end k = r/(0.5*sigma^2); % get the initial conditions: tau = 0.0; oldu = payoff_fn( xgrid, tau, k ); % bst = tau/(0.5*sigma^2); % [C,P] = blackscholes(E,bst,E*exp(xgrid),r,sigma); %T-tau/(0.5*sigma^2) % p_tmp = (P/E).*exp( 0.5*(k-1)*xgrid )*exp( 0.25*((k+1)^2)*tau ); % %figure; plot( xgrid, oldu, '-rx' ); hold on; plot( xgrid, p_tmp, '-go' ); pause; u = zeros(M+1,Nx+1); % <- space for our results ... u(1,:) = oldu; newu = zeros(1,Nx+1); for m=1:M, tau = m*dt; % update the endpoints: newu(1) = u_m_inf( xgrid(1), tau, k ); newu(end) = u_p_inf( xgrid(end), tau, k ); % update the interior of our grid: newu( 2:(end-1) ) = oldu( 2:(end-1) ) + a * ( oldu( 1:(end-2) ) - 2*oldu( 2:(end-1) ) + oldu( 3:end ) ); % prepare for the next pass: oldu = newu; % bst = tau/(0.5*sigma^2); % [C,P] = blackscholes(E,bst,E*exp(xgrid),r,sigma); %T-tau/(0.5*sigma^2) % p_tmp = (P/E).*exp( 0.5*(k-1)*xgrid )*exp( 0.25*((k+1)^2)*tau ); % %figure; plot( xgrid, oldu, '-rx' ); hold on; plot( xgrid, p_tmp, '-go' ); %pause; % store u(m+1,:) = newu; end