I am calculating 11x6 matrix using different logics as in below code. Problem is in Do loop in below code where I have two different criteria to update matrix "fd". I am expecting logic in IF which is supposed to work for all element in row (nas1+1) as I start updating matrix from right. However logic is failing after second iteration. Hence matrix is getting incorrectly updated from (10,4)th element. (10,4)th element is computed using previous value of (11,5)th matrix fd (which is 1).I see (11,1)th value is also incorrect for same reason. Can anybody help in understanding why Do loop is not updating in correct sequence? following code is evaluated using optionprice[100, 10, 1, 0.2, 0.05]. The code is as below:
optionprice[strike1_, nas1_, expn1_, vol1_, int_] :=
Module[{ds, s, dt, dt1, t, nts, fd, delta, gamma, theta},
ds = 2*strike1/nas1;
dt = 0.9/(vol1*vol1*nas1* nas1) ;
nts = IntegerPart [expn1 /dt] + 1;
dt1 = expn1/nts;
s = Table[i*ds, {i, 0, nas1 + 1}];
t = Table [j*dt, {j, 0, nts}];
fd = ConstantArray[1, {nas1 + 1, nts + 1}];
fd = ReplacePart[fd, {i_, nts + 1} :> Max[(s[[i]] - strike1), 0]];
Table[fd[[1, j - 1]] = (1 - int*dt)*fd[[1, j]], {j, nts + 1, 2, -1}];
Do[
Do [ If[i == (nas1 + 1),
fd[[i, j]] = 2*fd[[i - 1, j]] - fd[[i - 2, j]],
delta = ( fd[[i + 1, j]] - fd[[i - 1, j]])/(2*ds);
gamma = ( fd[[i + 1, j]] - 2*fd[[i, j]] + fd[[i - 1, j]])/(ds*ds);
theta = - 0.5*vol1*vol1 *s[[i]]*s[[i]]*gamma -
int*s[[i]]*delta + int*fd[[i, j]];
fd [[i, j - 1]] = fd [[i, j]] - theta *dt],
{i, 2, nas1 + 1}]
, {j, nts + 1, 2, -1}];
Export["tests1.xls", fd]
]