# What's wrong with this code to create a matrix?

Here is my code:

h = .2;
m = .04;
k = 4/Pi^2;
L = 4;
n = L/h;
s = k*m/h^2;
A = SparseArray[{Band[{1, 1}] -> 1.0 + 2.0*s, Band[{2, 1}] -> -s,
Band[{1, 2}] -> -s}, n + 1];


Here's the error:

SparseArray::dims: The dimensions 21. in SparseArray[{Band[{1,1}]->1.81057,Band[{2,1}]->-0.405285,Band[{1,2}]->-0.405285},21.] are not given as a list of positive machine integers. >>


I've been experimenting with this for quite a while now and can't seem to figure out what's wrong. Any ideas?

The message tells you whats wrong:

SparseArray::dims: The dimensions 11. in ...are not given as a list of positive machine integers.

You should use something like Round[n+1] as last part.

Isn't n +1 an integer though? 4/.2 + 1 = 21?

No. Mathematica makes a distinction between exact Integers and numeric values. In your formula, the .2 is a numeric value (in contrast to 2/10!) and therefore, the result is 21.0.

result=4/.2+1;
IntegerQ[result]
IntegerQ[Round[result]]
(*
Out[13]= False
Out[14]= Real
Out[15]= True
*)
`

• @David No, $n+1$ is a the real number 21.0. You might want to read up on types of numbers: reference.wolfram.com/mathematica/tutorial/TypesOfNumbers.html Commented Dec 6, 2012 at 7:26