I'm new to Mathematica and will need some help in coding the Mandelbrot set.

I don't want to use MandelbrotSetPlot since I want to understand the interior mechanisms of the set. I'm also not interested in speed but just want to keep it simple.

Where do I start? z=z^2+c is relatively simple but:

  • How do I make clear that the function should be iterated? I tried NestList, NestGraph and NestWhile but nothing seems to work. Is there a way to include n and n+1 instead?
  • How do I make clear that c is a complex number? Can I define c=r+i, for example?

I found this Julia set here(Why is this Mandelbrot set's implementation infeasible: takes a massive amount of time to do?) which makes sense to me:

  NestWhile[#^2 - (0. - 1 I) & , r + i I, Abs[#] < 2.0 &, 1, 10],
  {r, -2, 2, 0.005},
  {i, -2, 2, 0.005}]]

But a Mandelbrot should have a variable c instead, that's right?

Thanks in advance and sorry for the stupid questions.

  • $\begingroup$ Just change 0. - 1 I to (r + i I). Mandelbrot is $z_{n+1}\leftarrow z_n^2 + c$ where $c$ is the intial $z_0$ - also switch your {r,...} and {i,...} ranges in the table over to rotate it by 90*. And consider using ParallelTable for better performance. $\endgroup$
    – flinty
    Jun 21, 2020 at 14:05
  • 1
    $\begingroup$ How do I make clear that the function should be iterated? ... Is there a way to include n and n+1 instead? You're on the right track using NestWhile and this is the most efficient and expressive approach to this problem. There is no need to refer to n and n+1 and you should continue to experiment with constructs like Nest, FixedPoint, Map, as you learn Mathematica. Also if you have any syntax trouble, these answers are a good place to start mathematica.stackexchange.com/questions/18393/… $\endgroup$
    – flinty
    Jun 21, 2020 at 14:41

1 Answer 1


Something like

f[c_, maxiter_Integer : 10^4] := 
  Abs[NestWhile[#^2 + c &, 0, Abs[#] < 2 &, 1, maxiter]] < 2

(*    True    *)

(*    False    *)

(*    True    *)

f[I, 10^6]
(*    True    *)

  Table[Boole[f[N[x + I y], 10^3]], {y, -2, 2, 1/50}, {x, -2, 2, 1/50}]]

enter image description here

By playing with the working precision of N you can achieve high-precision calculations.

  Table[Boole[f[N[x + I y], 200]],
        {y, 0.14, 0.15, 10^-5},
        {x, -0.75, -0.74, 10^-5}]]

enter image description here


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.