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)].

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 value *)
]

Try it:

numSteps[f, 1.4, 20]

NestList[f,1.4,20]

numSteps[f, 1+I, 2]

NestList[f,1.3+0.4I,2]

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

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

Zoom in on (0.5,0)

DensityPlot[ numSteps[ f, x+y*I, 100], {x,0.45,0.55},{y, -0.05,0.05}, PlotPoints->{200,200}, ColorFunction->"Rainbow", AspectRatio->Automatic]

Zoom in on (0.525,0.01)

DensityPlot[ numSteps[ f, x+y*I, 100], {x,0.523,0.527},{y, 0.008,0.012}, PlotPoints->{200,200}, ColorFunction->"Rainbow", AspectRatio->Automatic]

Mandelbrot Set

Iterate the following function. Here, c is a parameter, and we start iterating at zero.

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

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