| New Reply |
Mathematica Beginner question: Nested loops |
Share Thread | Thread Tools |
| Mar17-11, 05:16 AM | #1 |
|
|
Mathematica Beginner question: Nested loops
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 |
| PhysOrg.com |
science news on PhysOrg.com >> Hong Kong launches first electric taxis >> Morocco to harness the wind in energy hunt >> Galaxy's Ring of Fire |
| Mar17-11, 06:37 AM | #2 |
|
Mentor
|
Looks like you should use recursive functions, not looping.
|
| Mar17-11, 03:55 PM | #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}}} |
| Mar17-11, 04:03 PM | #4 |
|
|
Mathematica Beginner question: Nested loops
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 |
| Mar17-11, 07:36 PM | #5 |
|
Mentor
|
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] |
| Mar17-11, 08:51 PM | #6 |
|
|
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. |
| Mar18-11, 11:15 AM | #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! |
| New Reply |
| Thread Tools | |
Similar Threads for: Mathematica Beginner question: Nested loops
|
||||
| Thread | Forum | Replies | ||
| Connecting plot points from nested loops | Math & Science Software | 2 | ||
| Complexity of nested for-loops | Engineering, Comp Sci, & Technology Homework | 0 | ||
| Beginner to Mathematica's question! using Module! | Math & Science Software | 1 | ||
| Innanely stupid beginner Mathematica question | Math & Science Software | 0 | ||
| Mathematica question - Beginner. | Math & Science Software | 4 | ||