I am examining the mandelbrot set so I need to iterate the function

f[z_Complex, c_] := (z)^2 + c 

Besides, I want to choose the complex numbers dynamically from 2D slider.

DynamicModule[{x}, {Slider2D[Dynamic[x], {{-2, -2}, {2, 2}}], 
  Style[Nest[f[Dynamic[x[[1]] + Dynamic[[x[[2]]]], c], smthng, 10], 
    Dynamic[x[[2]]], 2], Bold, Blue]}

I tried to use


But it basically didn't work

I am a newbie at mathematica so if someone would help I will be really happy.

  • 2
    $\begingroup$ There are multiple problems with your code: the dynamic module doesn't compile (missing a closing bracket), it refers to undefined symbols c and smtng, the _Complex in your definition of f probably doesn't do what you think it does (e.g., try to evaluate f[1,2]), etc. Given all of this, I would suggest just read some tutorials about Mathematica, e.g., the elementary introduction by Stephen Wolfram, otherwise, you could be constantly puzzled by very basic things. $\endgroup$
    – Victor K.
    Commented Nov 19, 2022 at 20:34
  • 2
    $\begingroup$ I will start with the first tiniest little step. f[{z_,c_}]:={z^2+c,c}; Nest[f,{1/2+I/4,3},2] Check that result carefully. Does that correctly nest that function twice? Yes, it leaves {result,3} when what you want is just result, but does it correctly calculate result? Then try bumping the 2 up to 3 and check it again. And then up to 4 and check it again. What that is trying to do is to correctly give and get a single list argument with f. If all that is correct then you try putting [[1]] on the end of that Nest to extract just the result from that list. Study all this carefully. $\endgroup$
    – Bill
    Commented Nov 19, 2022 at 20:38
  • $\begingroup$ What @Bill said, or alternatively, you can "curry" the second argument when using Nest: ClearAll[f]; f[z_, c_] := z^2 + c; Nest[f[#, 3] &, 1/2 + I/4, 3] $\endgroup$
    – Victor K.
    Commented Nov 19, 2022 at 20:49
  • $\begingroup$ What is smthng? $\endgroup$
    – cvgmt
    Commented Nov 19, 2022 at 23:12

1 Answer 1


Let's work up gradually. We'll start with the function, then iteration, then visualization with some sort of dynamic construct.

I'll redefine your function f like this:

MStep[c_][z_] := z^2 + c

The renaming is just for clarity. I'm thinking of this function not so much as a function with two arguments, but a function of one argument that is parameterized by some other value. Defining it in this form allows me to treat an expression like MStep[.5] as a function in and of itself. For example

MStep[.5] /@ {1, 2, 3}

output: {1.5, 4.5, 9.5}


MStep[1 + 1 I] /@ {1 + 2 I, 2 - 3 I, 3}

output: {-2 + 5*I, -4 - 11*I, 10 + I}

This will be useful when we get to repeated function application. Let's try that now with NestList

NestList[MStep[1 + 1 I], 0, 5]

output: {0, 1 + I, 1 + 3*I, -7 + 7*I, 1 - 97*I, -9407 - 193*I}

Okay, now we need to work on using the slider control to pick a point. Evaluate the following and play with the slider control.

{Slider2D[Dynamic[coords]], Dynamic[coords]}

The output of a slider control is a 2-element list, i.e. it's the coordinates of a point. But we want a complex number. All we need to do is apply the head Complex:

{Slider2D[Dynamic[coords]], Dynamic[coords], Dynamic[Complex @@ coords]}

Okay, one more hurdle--we need to put this into a dynamic structure. I'm going to use Manipulate.

    {pts = 
         NestList[MStep[Complex @@ cparam], Complex @@ coords, iterationcount], 
      {{Complex @@ coords, Complex @@ cparam},
          {GrayLevel[.9], Disk[{0, 0}, 2], 
           Black, Line[ReIm[pts]]}, 
          PlotRange -> {{-2, 2}, {-2, 2}}]}}, 
      Alignment -> Top]],
    Control[{{coords, {0, 0}, "initial pt"}, {-2, -2}, {2, 2}}],
    Control[{{cparam, {0, 0}, "parameter c"}, {-2, -2}, {2, 2}}],
    {{iterationcount, 5}, 2, 20, 1},
    ControlPlacement -> Left]

A lot to unpack here. First off, most of the things I put inside the Grid are just for illustration/testing. When a control is specified to deal with a 2D variable, it automatically uses Slider2D. The Select is just removing elements with Abs greater than 2, since we know these will "blow up". You could incorporate that check into MStep if you wanted. The rest I'll let you explore and figure out on your own


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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