Mathematica Beginner question: Nested loops

In summary, the conversation discusses the problem of executing code in sequence using commands like Table or Do in Wolfram Mathematica. One user provides an example of using recursive functions and another suggests using a "helper function" in combination with NestList to solve the problem. After some discussion and testing, the user discovers a faster solution to the problem.
  • #1
fleuredelis
3
0
Hello,

I'm quite new to Wolfram Mathematica and do like the elegance of some parts of the language. However, I think that loop controls are quite different and not very intuitive.

Here is a simplified version of what I'm trying to do (now of course done manually):

k[1] = {200, 300, 400, 500}
u[1] = k[1]*1/k[1][[4]]
k[2] = k[1]*u[1]
u[2] = k[2]*1/k[2][[4]]
k[3] = k[2]*u[2]

Basically, there is are some starting value and then a control is derived from that, which, in the next steps creates a new set of values. Problem is that I haven't figured out how to execute the code in sequence with commands like Table or Do.

Any help is highly appreciated!

Thanks a lot already,

Daniele
 
Physics news on Phys.org
  • #2
Looks like you should use recursive functions, not looping.
 
  • #3
Very good that you included an example. I hope I understand what you want. If you had included the numeric values for u[2] and k[3] I would have been more certain I understood.

First try this step by step so you can verify this is what you really meant

In[1]:= k1={200,300,400,500};u1=k1*1/k1[[4]]
Out[1]= {2/5, 3/5, 4/5, 1}

In[2]:= k2=k1*u1
Out[2]= {80,180,320,500}

In[3]:= u2=k2*1/k2[[4]]
Out[3]= {4/25, 9/25, 16/25, 1}

In[4]:= k3=k2*u2
Out[4]= {64/5, 324/5, 1024/5, 500}

Now, since you didn't show the actual output, are these what you really want?

If so then let's create a small "helper function" and see if it can find the next u and k

In[5]:= f[{k_,u_}]:={k*k/k[[4]],k/k[[4]]}

In[6]:= f[{{200,300,400,500},{}}]
Out[6]= {{80, 180, 320, 500}, {2/5, 3/5, 4/5, 1}}

In[7]:= f[%]
Out[7]= {{64/5, 324/5, 1024/5, 500}, {4/25, 9/25, 16/25, 1}}

Is the helper function correctly calculating both u and k correctly for each step?
If so then we can use NestList to find these all at once.

In[9]:= result = NestList[f,{{200,300,400,500},{}},3]

Out[9]= {{{200, 300, 400, 500}, {}}, {{80, 180, 320, 500}, {2/5, 3/5, 4/5, 1}}, {{64/5, 324/5, 1024/5, 500}, {4/25, 9/25, 16/25, 1}}, {{1024/3125, 26244/3125, 262144/3125, 500}, {16/625, 81/625, 256/625, 1}}}

You can then use, for example, result[[3,1]] and result[[3,2]] to extract a k or u.
Or perhaps you would like to group the k's together and the u's together.

In[10]:= Transpose[result]
Out[10]= {{{200, 300, 400, 500}, {80, 180, 320, 500}, {64/5, 324/5, 1024/5, 500}, {1024/3125, 26244/3125, 262144/3125, 500}},
{{}, {2/5, 3/5, 4/5, 1}, {4/25, 9/25, 16/25, 1}, {16/625, 81/625, 256/625, 1}}}
 
  • #4
Bill, a thousand thanks for your detailed and very kind answer! I'll look into the exact application to my problem tomorrow morning, but it looks like the "helper function" in combination with NestList is really what I was missing.

Many thanks again and sorry for bothering you!

Cheers, Daniele
 
  • #5
I don't understand the advantage of the helper function. I would just implement k and u directly as recursive functions:

k[1] := {200, 300, 400, 500}
u[n_] := k[n]*1/k[n][[4]]
k[n_] := k[n - 1]*u[n - 1]
 
  • #6
DaleSpam said:
I don't understand the advantage of the helper function.

My understanding from the original description of the problem was that what was desired was table, of unknown size, of these values. So I tried to construct the easiest to understand f that could be handed to NestList to return the desired table and by changing a single number you could ask NestList to return 3 or 1000 of these. The original description was not explicitly clear how the results needed to be organized and that makes it a little difficult to decide how best to try to answer the question. If what was desired was just a single value then what I did was certainly not the simplest way to get there.

As always, particularly in Mathematica, there are at least a dozen ways of doing anything, half of them most people would never think of and some of those nearly incomprehensible.
 
  • #7
Indeed, the problem is a lot larger than just three iterations. NestList didn't work directly for the problem at hand, but I found another solution that is a lot faster than my previous work around. As has been said there are dozens of ways to solve a problem ;-)

Many thanks again to all of you!
 

What is a nested loop in Mathematica?

A nested loop in Mathematica is a control structure that allows you to run a loop within another loop. This means that the inner loop will be executed multiple times for each iteration of the outer loop. Nested loops are useful for performing repetitive tasks and iterating through multi-dimensional data structures.

How do I create a nested loop in Mathematica?

To create a nested loop in Mathematica, you can use the "Do" or "Table" functions. These functions allow you to specify the number of iterations for each loop and the code to be executed within the loop. You can also use the "For" loop and manually specify the loop indices for each nested loop.

Can I have multiple levels of nested loops in Mathematica?

Yes, you can have multiple levels of nested loops in Mathematica. You can have as many nested loops as you need to perform your desired task. However, it is important to keep in mind that the execution time for nested loops increases exponentially as the number of levels increases.

How can I avoid infinite loops in nested loops?

Infinite loops can occur in nested loops if the loop conditions are not properly set up. To avoid this, make sure to carefully define the loop conditions and update the loop indices appropriately within the loop. You can also use conditional statements, such as "If" or "While" statements, to control the execution of the nested loops.

Can I break out of a nested loop in Mathematica?

Yes, you can break out of a nested loop in Mathematica by using the "Break" statement. This statement allows you to terminate the current loop and continue with the next statement outside of the loop. However, it is important to use this statement carefully to avoid unexpected results and ensure that all necessary iterations are completed.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
12
Views
187
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Atomic and Condensed Matter
Replies
3
Views
844
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
16
Views
1K
Back
Top