Fractals

Extending the Collatz function

Hear is an extension of the Collatz function to real numbers: f(x)=[1+cos(πx)](x/4)+[1-cos(πx)]((3x+1)/2).

m=7;
f[x_]:=(1+Cos[Pi*x])*x/4+(1-Cos[Pi*x])*(3x+1)/2;
dots=ListPlot[f/@Range[m],PlotStyle->{PointSize[Large], Purple}];
curves=Plot[{f[x],x/2,3x+1},{x,0,m+0.5}];
Show[curves,dots]

We can also use complex numbers: f(z)=[1+cos(πz)](z/4)+[1-cos(πz)]((3z+1)/2). 
Simplify the function to obtain f(z)=(1/4)[2+7z-(2+5z)cos(πz)].

ClearAll[f]

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

Drawing a Fractal

We want to iterate the function on complex numbers until the value exceeds some bound. We will use a module to do this; the module returns the number of steps until the bound is exceeded.

numSteps[f_, start_, bound_]:=Module[{steps=0,z=start},
While[ Abs[z]< bound && steps<100,
z=f[z]; (* iterate the function *)
steps++
];
steps (* return the number of steps *)
]

A little test :

numSteps[f,2.3,50]

NestList[f,2.3,10]

Compute numSteps for lots of complex numbers in a grid. Use DensityPlot, and color points according to their number of steps.

DensityPlot[ numSteps[f, x+y*I,50], {x,-4,4},{y,-1.5,1.5},PlotPoints->{400,150}, ColorFunction->"Rainbow", AspectRatio->Automatic]

Zoom in:

DensityPlot[ numSteps[f, x+y*I,50], {x,1.197,1.202},{y,-0.001,.004},PlotPoints->{300,300}, ColorFunction->"Rainbow", AspectRatio->Automatic]

Mandlebrot Set

Iterate a function (Subscript[g, c])(z) for some parameter c, starting at z=0.

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

DensityPlot[ numSteps[ g[x+y*I],0, 50], {x,-2,1},{y,-1,1},PlotPoints->{300,100},ColorFunction->"Rainbow",AspectRatio->Automatic]