Mathematica Beginner question: Nested loops

Click For Summary

Discussion Overview

The discussion revolves around implementing nested loops in Wolfram Mathematica to derive sequences of values based on initial inputs. Participants explore different approaches to achieve this, including recursive functions and helper functions, while addressing the challenges of executing code in sequence.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty with loop controls in Mathematica and seeks help with executing a sequence of calculations.
  • Another participant suggests using recursive functions instead of loops to achieve the desired results.
  • A different participant provides a step-by-step breakdown of the calculations, questioning whether the outputs align with the original intent.
  • There is a proposal for a "helper function" to calculate values iteratively, which some participants find useful.
  • One participant challenges the necessity of the helper function, advocating for direct recursive implementations of the calculations.
  • Another participant notes the ambiguity in the original problem description, which complicates determining the best approach.
  • A later reply mentions that the problem is larger than initially presented and that alternative solutions have been found that are more efficient.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method to implement the calculations, with multiple competing views on the use of helper functions versus recursive functions. The discussion remains unresolved regarding the optimal approach.

Contextual Notes

Some participants note that the original problem description lacked clarity, which affected the ability to provide a straightforward solution. There are also indications that the problem may require more iterations than initially discussed.

fleuredelis
Messages
3
Reaction score
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
Looks like you should use recursive functions, not looping.
 
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}}}
 
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
 
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]
 
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.
 
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!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K