# Matrix iteration failing after second step

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]
]

• Repost your output. It is incomplete when copied back into Mma; gives syntax errors. – Edmund Apr 25 '15 at 13:13
• {{0., 0., 0., 0., 0., 0}, {0.0392863, 0.0297641, 0.020045, 0.010125, 0., 0}, {0.109358, 0.0838695, 0.0571863, 0.02925, 0., 0}, {0.203428, 0.158713, 0.110148, 0.057375, 0., 0}, {0.313219, 0.249616, 0.177169, 0.0945, 0., 0}, {1.3868, 1.60408, 1.89657, 2.28867, 2.8125, 0}, {5.19801, 7.21555, 10.1942, 14.5993, 21.125, 20}, {7.96537, 11.6816, 17.4811, 26.6124, 41.125, 40}, {8.721, 13.4449, 21.4754, 35.5557, 61.125, 60}, {7.98762, 12.9066, 22.14, 40.673, 81.125, 80}, {1, 12.3683, 22.8045, 45.7902, 101.125, 100}} – Kausik Apr 25 '15 at 13:17