I want to nsolve the Black-Scholes-PDE combined with the uncertain volatility model with the Euler-Backwards-Method. Therefore I created a Table with boundary conditions
u = Table[a[i, j], {i, k + 1}, {j, n + 1}]
For[i = 1, i <= n + 1, i++, u[[1, i]] = Max[0, (x[i] - Strike)]]
For[i = 1, i <= k + 1, i++,
u[[i, 1]] = Max[0, 0 - Strike]*Exp[-r*(Tmax - (k - i + 1)*dt)]]
For[i = 1, i <= k + 1, i++,
u[[i, n + 1]] = smax - Strike * Exp[-r*(Tmax - (k - i + 1)*dt)]]
Then I use this code for the Euler-Backwards-Method
For[j = 1, j <= k, j++,
For[i = 1, i < n, i++,
erg = Solve[
u[[j, i +
1]] == (u[[j + 1,
i]] *(-r*
x[i + 1]/(2*
dx) + (sigma[(u[[j + 1, i + 2]] - 2*u[[j + 1, i + 1]] +
u[[j + 1, i]])/dx^2]^2*x[i + 1]^2)/(2*dx^2)) +
u[[j + 1,
i + 1]]*(1/
dt - (sigma[(u[[j + 1, i + 2]] - 2*u[[j + 1, i + 1]] +
u[[j + 1, i]])/dx^2]^2 *x[i + 1]^2)/dx^2 - r) +
u[[j + 1,
i + 2]]*(r*
x[i + 1]/(2 dx) + (sigma[(u[[j + 1, i + 2]] -
2*u[[j + 1, i + 1]] + u[[j + 1, i]])/dx^2]^2*
x[i + 1]^2)/(2 dx^2)))*dt]; u = u /. erg[[1]]]]
with the function sigma[x_]:=If[x >= 0, 0.5, 0.3]. Strike, r, dt, dx, smax, Tmax, n, k and x[i_] are entered before. But because the solve function's error is
Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.
I don't get a list with rules I could replace. This code works perfectly with the normal Black-Scholes-PDE
The initialisation looks like this
Strike = 50
r = 0.02
smin = 0
smax = 100
Tmax = 1
n = 2
k = 2
dt = Tmax/k
dx = (smax - smin)/n
l = dt/dx^2
x[i_] := smin + (i - 1)*dx
sigma[x_]:=If[x >= 0, 0.5, 0.3]
r is the interest rate. smin and smax are the borders for the status variable while 0 and Tmax the borders for the time variable are. n is the number of pricesteps and k is the number of timesteps. dt is the stepsize for time, dx the stepsize for status. x[i] gives as result the status in step i
I now found the problem. In every iteration the solve function gives a list with 2 conditional expressions. So if I replace the "wrong" rule, the next equation isn't solvable. For example with n=3=k the loop's first result is
{{a[2, 2] -> ConditionalExpression[-0.0190311 a[2, 3], a[2, 3] > 0]}, {a[2, 2] -> ConditionalExpression[-0.0102041 a[2, 3], a[2, 3]
Fornever returns any. If you want the intermediate values you could usePrint(in a pinch, but generally bad),Return, orSowandReap. If you just want the final values, evaluateuorergat the end of your code. Look also at this question pointing out some shortcomings ofFor. – MarcoB Jan 30 '17 at 17:07