For loop in Mathematica doesn't work

In summary, the conversation discusses a problem with a for loop in Mathematica and the correct syntax for LinearSolve. It is discovered that the issue is caused by using extra {} around each element, which can be solved by using the Flatten[] function. The conversation also mentions the importance of using row and column vectors correctly and provides helpful links for further reference.
  • #1
nikolafmf
114
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

  • Kepler 4.nb
    61.5 KB · Views: 473
Physics news on Phys.org
  • #2
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:
  • #3
Thank you very much again :)
 

1. Why is my for loop in Mathematica not working?

There could be several reasons why your for loop in Mathematica is not working. Some common reasons include incorrect syntax, the loop not being properly defined, or the loop not being executed at all. It is important to carefully check your code and make sure it is written correctly.

2. How do I fix a for loop that is not working in Mathematica?

To fix a for loop that is not working in Mathematica, you can try breaking down the loop into smaller steps and testing each step individually. You can also use the built-in debugging tools in Mathematica to identify any errors in your loop. Additionally, double-checking the syntax and making sure the loop is properly defined can also help resolve the issue.

3. Can you provide an example of a working for loop in Mathematica?

Sure, here is an example of a basic for loop in Mathematica that prints the numbers from 1 to 10:

For[i = 1, i <= 10, i++, Print[i]];

This loop starts with a variable i set to 1, and it will continue to loop as long as i is less than or equal to 10. Each time the loop runs, i is incremented by 1 and the current value of i is printed.

4. What is the difference between a for loop and a while loop in Mathematica?

A for loop in Mathematica is used to repeat a set of instructions for a specified number of times, while a while loop repeats the instructions as long as a certain condition is met. Essentially, a for loop is used for a known number of iterations, whereas a while loop is used for an unknown number of iterations.

5. Are there any alternatives to using for loops in Mathematica?

Yes, there are several alternatives to using for loops in Mathematica, such as using the Table or Do loops. These can often be more efficient and easier to read than for loops. Additionally, many problems can also be solved using built-in functions in Mathematica, eliminating the need for a loop altogether.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
825
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
899
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
21
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
447
Back
Top