# Fractal plotting for the Collatz fractal

I have the following equation:

f[z_] := 1/4 (2 + 7 z - (2 + 5 z) Cos[Pi*z])


I want to map this on the imaginary plane such that if it converges under iterations of the function then it is plotted. I would like to then color this in a Mandelbrot type way. How would I go about plotting this?

The goal is to explore the fractal mentioned here.

This is the type of image i'm trying to produce with the ability to zoom in to particular points.

• Have you seen Speeding up this fractal-generating code – user9660 May 20 '16 at 7:03
• Have you seen MandelbrotSetPlot – user9660 May 20 '16 at 7:11
• Have you seen Mandelbrot Set – user9660 May 20 '16 at 7:14
• Tweak as needed: DensityPlot[-Length[FixedPointList[Function[z, (2 + 7 z - (2 + 5 z) Cos[Pi z])/4], x + I y, 100, SameTest -> (Abs[#2] > 25. &)]], {x, -5, 5}, {y, -1, 1}, AspectRatio -> Automatic, ColorFunction -> (RGBColor[0, 0, #] &), PlotRange -> All] – J. M. is in limbo May 20 '16 at 8:04
• I'm always at a loss with these pictures, about what is being plotted. Nathaniel Johnston writes that the black portions of the plot are areas where the orbit is bounded (Check out the Orbit function here). But what is the color in the rest of the plot based on? – Jason B. May 20 '16 at 9:59

One way of coding the iterations may be defined as follows. The function CollatzFractal01[z0] returns a list {Abs[z],iter}, where Abs[z] is the final value of z, and iter is the number of iterations required to escape the bound of Abs[z]>200. Use compilation and parallelisation for speed.

CollatzFractal01 = Compile[{{z0, _Complex, 0}},
Module[{iter = 0, max = 3000, z = z0},
While[iter++ < max,
If[Abs[z = (2 + 7 z - (2 + 5 z) Cos[Pi z])/4] > 200., Break[]]];
{Abs[z], iter}],
CompilationTarget -> "C", Parallelization -> True,
RuntimeAttributes -> {Listable}];


Generate a table of locations in the complex plane with ParallelTable.

d = ParallelTable[i + I*j, {j, -0.5, 0.5, 0.001}, {i, 0.3, 2.5, 0.001}];


Graphics[Raster[...]] can be faster than ArrayPlot[...]. This plot shows the final Abs[z].

Graphics[Raster[
Rescale[Log[Log[CollatzFractal01[d][[All, All, 1]] + 1.] + 1.]],
ColorFunction -> (ColorData["FallColors", 1 - #] &)],
ImageSize -> 600, Background -> Black]


This plot shows the number of iterations.

Graphics[Raster[
Rescale[Log[Log[CollatzFractal01[d][[All, All, 2]] + 1.] + 1.]],
ColorFunction -> (ColorData["Rainbow", #^0.5] &)],
ImageSize -> 600, Background -> Black]


Slightly redefining your original equation gives more "interesting" plots, with far more structure. However, the calculation time increases substantially.

CollatzFractal02 = Compile[{{z0, _Complex, 0}},
Module[{iter = 0, max = 3000, z = z0},
While[iter++ < max,
If[Abs[z = (1 + 4 z - (1 + 2 z) Cos[Pi z])/4] > 200., Break[]]];
{Abs[z], iter}],
CompilationTarget -> "C", Parallelization -> True,
RuntimeAttributes -> {Listable}];


The iteration count for example:

• Only slightly slower: DensityPlot[Log[1 + Log[Length[FixedPointList[Function[z, (2 + 7 z - (2 + 5 z) Cos[Pi z])/4], x + I y, 3*^3, SameTest -> (Abs[#2] > 200. &)]]]], {x, 1/3, 5/2}, {y, -1/2, 1/2}, AspectRatio -> Automatic, ColorFunction -> (ColorData["Rainbow", Sqrt[#]] &), PlotPoints -> 145, PlotRange -> All] – J. M. is in limbo May 21 '16 at 17:24