# For loop in Mathematica doesn't work

1. May 12, 2012

### nikolafmf

I have problem with for loop in Mathematica given in the notebook attached.
It evaluates only first step and then shows problems and the kernel could not stop running.
What could be the problem? What is the correct syntax?

#### Attached Files:

• ###### Kepler 4.nb
File size:
61.5 KB
Views:
70
2. May 13, 2012

### Bill Simpson

The smallest change I can do to make this work is to replace
f = LinearSolve[V, u]
with
f = LinearSolve[V, Flatten]

Mathematica does not demand you create row vectors and column vectors differently. This

http://reference.wolfram.com/mathematica/tutorial/VectorsAndMatrices.html

documents that about 1/5 of the way down that page, but it could be more clear and forceful.

This

http://reference.wolfram.com/mathematica/ref/LinearSolve.html?q=LinearSolve&lang=en

says that the second argument to LinearSolve can either be a vector or a matrix. The examples show using a vector.

It appears that your trying to give an approximation of a column vector, by adding an extra {} around each element, is the source of your problem.

In your previous post I removed all those extra {} and got it to run. Now with your new post and the extra {} introduced again it fails. Flatten[] eliminates the inner {} and gets the code to run.

If you put

Print[f]; Print[g]; Print[p, i];

in place of your

Print[p, i];

and you do not use the Flatten[] described above then you can see in the first few iterations that each iteration adds another layer of {} and those additional layers result in tensors that finally blow up LinearSolve.

Last edited: May 13, 2012
3. May 13, 2012

### nikolafmf

Thank you very much again :)