Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
(*initialisation of variables*)
n = 10;(*expected value greater than 3*)
r = 0.03;
\[Sigma] = 0.2;
sMax = 2;
sMin = -2;

(*define \[CapitalDelta]x.*)
\[CapitalDelta]x = (sMax - sMin)/(n - 1);

(*adding vector matrix... any assignment of v value should be 
included here*)
vecMatrix = Array[v, n];

(*reusable internal variables*)
p = 0.5 (\[Sigma]^2)/(\[CapitalDelta]x^2);
q = (2 r - (\[Sigma]^2))/\[CapitalDelta]x;

x = (2 p + r) - 0.75 q;
y = 5 p + q;
a = p - 0.25 q;
b = -2 p - r;
c = p + 0.25 q;

(*introducing matC for one time initialisation*)
matC = SparseArray[{Band[{1, 1}, {n, n}, n - 1] -> x, 
    Band[{1, 2}, {n, n}, {n - 1, n - 3}] -> y, 
    Band[{2, 1}, {n - 1, n}] -> a, Band[{2, 2}, {n - 1, n}] -> b, 
    Band[{2, 3}, {n - 1, n}] -> c}, {n, n}];
 MatrixForm [matC]*
share|improve this question
Can someone help to debug,,it is not running,and i am not able to put the boundary conditions. – user3005 Oct 7 '12 at 9:52
Welcome to the site! Perhaps you could explain your problem somewhat better. – Dr. belisarius Oct 7 '12 at 10:21
Probably related:… – Verbeia Oct 7 '12 at 10:57
Welcome, @user3005. Please understand that pasting a swathe of code with "it doesn't work" doesn't make it easy for people to help you. The problem is probably that you are multiplying not using Dot (.) and you wrapped the lists in MatrixForm before multiplying. See the question I linked previously. – Verbeia Oct 7 '12 at 10:59
@user3005 yes, posting the code is important, but we need to know more than just "it doesn't work". What error messages are you getting? Are you running from a freshly launched kernel? What output are you expecting? Have a look at some other questions on the site, especially those with at least 5 votes, and you will see how to write a question that shows a problem clearly. By the way, comments can be at least this long so you don't need to write in SMS speak. – Verbeia Oct 7 '12 at 19:32

I've done a lot of option pricing with MMA, but I can't relate your code to plain old Black-Scholes:

d1[spot_,strike_,ir_,div_,vol_, T_] = (Log[spot/strike] + (ir-div+vol^2/2) T) / (vol T^0.5]);  
d2[spot_,strike_,ir_,div_,vol_, T_] = (Log[spot/strike] + (ir-div-vol^2/2) T) / (vol T^0.5]);
N[z_] = (1 + Erf[z/Sqrt[2]])/2 ; (*cumulative normal density*)

bsCall[spot_,strike_,ir_,div_,vol_,T_] :=
   spot*E^(-div*T)*N[d1[spot,strike,ir,div,vol, T]] - 


ir = interest rate  
div=dividends paid  
vol=volatility of the stock as measured by the standard deviation of its price
T = time to expiry  
Log[] = the natural logarithm

(I added this simple stuff to help others understand your question.) I don't see where the Black-Scholes part comes into your code? It would help educate others if you put your question in a context, for example, black-scholes option pricing is easy if you have one option, but if you have 100,000 at varying strikes, etc. But your question seems to really be about matrix algebra and sparce arrays.

share|improve this answer
thank you very much for the reply. In fact it is more on ''code for pricing an European option under Black–Scholes model using ETD with central discretisation''. I have been introduced to the concept of boundaries in option pricing but i have no idea how to include it in the code. – user3005 Oct 7 '12 at 19:09
I wouldn't use capital N in your function definition, as it is a built-in one. Also, using n[z_]=CDF[NormalDistribution[0, 1], z] would arguably yield a slightly better readable and more generalizable version of your definition. – Sjoerd C. de Vries Oct 7 '12 at 19:26
@Sjoerd I actually used an italicized N in my code, along with other symbols that don't show well here, but didn't want to clutter my comment. I didn't know about CDF[] when I wrote this code. I just copied. I should look at it. Thanks. – George Wolfe Oct 7 '12 at 20:01

You have a syntax error in the last line. I suspect you mean:

matC.vecMatrix // MatrixForm

which yields:

0.19875 v[1]+0.55125 v[2]
0.09 v[1]-0.2325 v[2]+0.1125 v[3]
0.09 v[2]-0.2325 v[3]+0.1125 v[4]
0.09 v[3]-0.2325 v[4]+0.1125 v[5]
0.09 v[4]-0.2325 v[5]+0.1125 v[6]
0.09 v[5]-0.2325 v[6]+0.1125 v[7]
0.09 v[6]-0.2325 v[7]+0.1125 v[8]
0.09 v[7]-0.2325 v[8]+0.1125 v[9]
0.09 v[8]-0.2325 v[9]+0.1125 v[10]
0.55125 v[9]+0.19875 v[10]

As an aside, comments that just say (* define \[Delta] x *) aren't that helpful to the next person looking at the code. I would recommend using commenting to explain what the variables are for.

As a further comment on your code, all your variables are global to your session, so the comment "reusable internal variables" is misleading. If you want to scope variables as internal to a particular calculation, you need to use scoping constructs like Module, Block and With. You might find this question helpful.

share|improve this answer
"As an aside, comments that just say (* define \[Delta] x *) aren't that helpful to the next person looking at the code. I would recommend using commenting to explain what the variables are for." - "Good comments don't repeat the code or explain it. They clarify its intent. Comments should explain, at a higher level of abstraction than the code, what you're trying to do." – J. M. Oct 7 '12 at 11:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.