# Question:explicit finite difference methods for pricing an option.

## Question:explicit finite difference methods for pricing an option.

Can anybody please help me I posted this question last week and didnt get the response I was looking for. It is a question about explicit finite difference methods for pricing an option.

I understand the mathematics behind this and everything but I am not able to code it. This is where the probelem lies.

First I define three functions in terms of j.

a:= j -> [-1/2 (r-q) j Delta t + 1/2 sigma^2 j^2 Delta t] / 1 + r Delta t

b:= j ->[1 - sigma^2 j^2 Delta t] / 1 + r Delta t

c := j ->[1/2 (r-q) j Delta t + 1/2 sigma^2 j^2 Delta t] / 1 + r Delta t

with r:= 0.05, Delta t:= T/N , Delta S:=S/M, T:=0.4, q:=0, K:=1.1, S:=2, M=N:=10, sigma:=0.2

Then I defined i:=N; such that f[i,j]:=max(K - j*Delta S, 0);

And next I defined g_{k,j}:=f_{i,j} with k:=N-i because maple requires I work from 0 -> N not from N -> 0 which is what I need.

So g[k,j]:=a[j]*g[k-1,j-1] + b[j]*g[k-1,j] +c[j]*g[k-1,j+1] which all works fine.

Next was to insert an if statement which I did using p:=(k,j)->`if`(k = 0, max(k-j*S, 0), g[k, j]); which seemed to work as when I inserted p(0,1) I got 0.900 which is correct.

but when I inserted p(1,1) it gave me just g[1,1] not the answer in digits which is what I want.

I have received an answer for this question which gave this code:

` r:= 0.05; Deltat:= T/N; DeltaS:=S/M; T:=0.4; q:=0; K:=1.1; S:=2; M:= 10; N:=10;    sigma:= 0.1;   for j from 0 to M do f[N,j]:= max(K - j*DeltaS,0) end do;   for j from 0 to M do      a[j]:= (-1/2 *(r-q)*j*Deltat + 1/2*sigma^2*j^2*Deltat)/1 + r*Deltat;     b[j]:= (1 - sigma^2 * j^2 * Deltat) / 1 + r * Deltat;     c[j]:= (1/2 * (r-q) * j * Deltat + 1/2 * sigma^2 * j^2 * Deltat) / 1 + r * Deltat   end do;   for i from N-1 to 0 by -1 do     for j from 1 to M-1 do       f[i,j]:= a[j]*f[i+1,j-1] + b[j]*f[i+1,j] + c[j]*f[i+1,j+1]     end do;     f[i,0]:= b[0]*f[i+1,0] + c[0]*f[i+1,1];      f[i,M]:= a[M]*f[i+1,M-1] + b[M]*f[i+1,M]; end do:`

`And this would do the trick but I really need it in the form that I have asked above, i.e with the g function and 'if' statement. `

`Could Anybody please helpe me with the coding issue that I am having and fix it for me so the problem works.`

﻿