Mathematica For loop in Mathematica doesn't work

Click For Summary
The discussion centers on troubleshooting a for loop issue in Mathematica related to the use of the LinearSolve function. The primary problem arises from the incorrect formatting of the input vector, specifically the introduction of unnecessary curly braces, which leads to the kernel failing to execute properly. The solution involves using the Flatten function to eliminate these extra braces, allowing LinearSolve to process the input correctly. Documentation references clarify that while Mathematica does not require distinct formats for row and column vectors, the presence of additional braces can cause the function to misinterpret the input. By adjusting the code to remove these extra braces or by using Flatten, the for loop can run successfully without generating errors.
nikolafmf
Messages
112
Reaction score
0
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?
 

Attachments

Physics news on Phys.org
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:
Thank you very much again :)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K