8

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:

ArrayPlot[Table[
  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.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 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. – flinty Jun 21 '20 at 14:05
  • 1
    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 https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users – flinty Jun 21 '20 at 14:41

1 Answers1

9

Something like

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

f[0] (* True *)

f[1] (* False *)

f[I] (* True *)

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

ArrayPlot[ 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.

ArrayPlot[
  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

Roman
  • 47,322
  • 2
  • 55
  • 121