When I evaluate this code on Mathematica,


Picaboo[n0_, a0_] := Module[{n = n0, a = a0, i = 1, x, y, s, t},

y[0, x_] := a;

For[i = 1, i <= n, i++, y[i_, x_] := y[0, x] + Integrate[ f[x, y[i - 1, x]], x];];

Print[y[n, x]]; ]

Picaboo[1, 1]

I get:

1 - 1 x$104718+x$104718^3/3

instead of

$1 - x + x^3/3$

I encounter this issue frequently and I don't know what causes it so any help on how to solve it and avoid it is appreciated.

  • 2
    $\begingroup$ Use code blocks, not quote blocks. Then you can write $. Why this happens is explained in the Module documentation page, under "Details" (always look under "Details"!!). Module "localizes" its variables by renaming the corresponding symbol in its body to a unique name of the form name$xxx where xxx is a number unique for this session. $\endgroup$ – Szabolcs Nov 17 '17 at 19:33
  • 2
    $\begingroup$ I suggest reading mathematica.stackexchange.com/a/633/12, as well as the other answers in that thread. $\endgroup$ – Szabolcs Nov 17 '17 at 19:35
  • 3
    $\begingroup$ BTW I would write your code as picaboo[n_, a_, x_Symbol] := Nest[a + Integrate[f[x, #], x] &, a, n]. Then try e.g. picaboo[3, a, z] $\endgroup$ – Szabolcs Nov 17 '17 at 19:41
  • $\begingroup$ That was helpful, Thank you! $\endgroup$ – MereMortal47 Nov 17 '17 at 20:31
  1. You define a symbol F outside the Module, while you call f inside the Module.

    Unless you are calling some other function named f, defined elsewhere, executing the code produces

    enter image description here

    not the third degree polynomial in the answer (anything after $ above identifies the local variable x in the Module; it differs between executions).

    The reason your code executes is because you have defined F as f elsewhere or you just miss-typed F instead of f, here.

  2. In the definition of Picaboo, using a capital letter can be confusing. The convention is that built-in symbols retain the first capital letter while user-defined symbols begin with lower-case letters eg picaboo.
  3. In the definition of picaboo

    picaboo[n0_, a0_] := Module[{n = n0, a = a0, <other stuff>},
      <some code>

    instead of n=n0 and a=a0, it could have been written as

    picaboo[n0_,a0_] := With[{n=n0, a=a0},

    (see With) although it is not clear (to me) why to use n=n0 and a=a0, in the first place.

  4. Instead of using For it would be advised to use Nest as suggested in the comments (see Nest). Effectively, what Nest does is start with an expression (eg a or a0 in this case) and apply repeatedly (eg n times in this case) a supplied function (eg a + Integrate[f[x, #], x]& as suggested in the comments). The input of the function is the starting expression (eg a in this case) and in every successive iteration, the input is taken to be the output of the previous step.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.