I have a Module that can act on other Modules, which is given as


and I am trying to iterate it, such that


for n many times. The problem is that the previous iteration needs to be a function of p and the values of z change during each iteration or shift by one, so I cannot simply use the Nest function.

This works manually if I define functions, for example, for the 2nd iteration:

KK[1][p_]:=KKKK[EE,z3,z4,p] (This is the initial function)



But, I want this in a Do loop. So, my current code is:

vz = {EE/2, 12, 11, 5};

vz = Reverse[vz];
lz = Length[vz];

kk[1][p_] := KKKK[EE, p, vz[[2]], vz[[1]]]; (Define the initial function)

   ki = i - 1;
   v1 = i - 1;
   v2 = i;
   v3 = i + 1;
     kk[i][p_] := Kk[EE, p, v3, v2, v1, kk[ki]];
 ,{i, 2, 3, 1}];

But this doesn't seem to work. Any suggestions? Apologies if I'm a bit ignorant, this is my first time using Stack Exchange!

  • $\begingroup$ I don't see why Nest wouldn't work: Nest[Kk[E,p,#]&,KKKK[E,p],n] $\endgroup$ – Lukas Lang May 10 '18 at 14:42
  • $\begingroup$ I should have also added that Kk is a function of 3 other values, which can be called z1,z2,z3. These values change with each iteration, so I don't think Nest can be used $\endgroup$ – Jordan May 10 '18 at 14:47
  • 1
    $\begingroup$ Then please add this information to the question. Also, having global variables in mathematica (or anywhere really) is generally a bad idea $\endgroup$ – Lukas Lang May 10 '18 at 14:48
  • $\begingroup$ Okay, thank you for you advice! $\endgroup$ – Jordan May 10 '18 at 15:07
  • 1
    $\begingroup$ Have you looked at Fold? Together with Partition, it should be able to give you what you want $\endgroup$ – Lukas Lang May 10 '18 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.