Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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

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?

share|improve this question
up vote 8 down vote accepted

The message tells you what`s 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.

Your comment

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.

Out[13]= False
Out[14]= Real
Out[15]= True

For further reading please see here or search the forum.

share|improve this answer
Isn't n +1 an integer though? 4/.2 + 1 = 21? – David Dec 6 '12 at 7:16
@David No, $n+1$ is a the real number 21.0. You might want to read up on types of numbers: – Mark McClure Dec 6 '12 at 7:26
ok. That kind of makes sense. I just figured the language would be sufficiently robust to allow for real numbers equal to an integer to be used as an integer. Is there a reason why it doesn't support this? – David Dec 6 '12 at 7:31
@David Yes, there are very good reasons to distinguish between real numbers and integers - this is common across a most programming languages. Internally, the arithmetic is very different. Exact arithmetic, between integers for example, is more costly computationally - so, why do it if the programmer doesn't need it? – Mark McClure Dec 6 '12 at 7:37

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.