Question: PLEASE I WANT TO CONVERT THIS MATLAB CODE TO R OR VBA % Script to generate matricies for the discrete approximation B to the %
PLEASE I WANT TO CONVERT THIS MATLAB CODE TO R OR VBA
% Script to generate matricies for the discrete approximation B to the
% Black-Schole operator, in financial variables. The following variables
% need to be defined:
% boundary values 0<=sL % the number N of subintervals, % time increment dt % interest rate r, % volatility sigma. % The script will generate the following: % spatial increment ds, % vector of N price values s=[sL+ds, ... , sH-ds] (row), % vectors a, b, c of coefficients (columns) % the matrix B. ds=(sH-sL)/(N+1); s=linspace(sL+ds,sH-ds,N); alpha=dt/ds^2; beta=dt/ds; a=(sigma^2*alpha*s.^2-r*beta*s)'/2; b=(sigma^2*alpha*s.^2+r*dt*ones(size(s)))'; c=(sigma^2*alpha*s.^2+r*beta*s)'/2; B=spdiags([[a(2:N);0],-b,[0;c(1:N-1)]],[-1,0,1],N,N); bL=sparse(1,1,a(1),N,1); bH=sparse(N,1,c(N),N,1); % Script for Crank-Nicolson finite difference method in financial % variables using explicit boundary values. The variables sL, sH, N, M, r, % sigma, and T should be set at the command line before running the script. % (r, sigma, and K should be global). Separate m-files vT, vL, and vH give % initial and boundary values. dt=T/M; genB %Generate matricies and coefficients Mat=speye(N)-B/2; [L,U]=lu(Mat); Mat=speye(N)+B/2; v=vT(s)'; %Initialize t=T; for m=M-1:-1:0 %Time-step loop rhs=Mat*v+(vL(sL,t,T)*bL+vH(sH,t,T)*bH)/2; t=m*dt; rhs=rhs+(vL(sL,t,T)*bL+vH(sH,t,T)*bH)/2; v=U\(L hs); end s=[sL,s,sH]; %Include boundaries and v=[vL(sL,t,T),v',vH(sH,t,T)]; %Convert results to rows
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
