# Procedural to functional style

The following code generates an iterative mapping of some trigonometric functions. I have a couple questions about it. The first question is how to optimize this iterative process using mathematica's language and the second is about the result. Do you think the flower looking structures that appear in these plots are a result of some numerical instability or a true part of the mathematics? Additionally, I was trying to get the different listplots to show up in different colors. thx

k = 100000;
m = 5;
For[l = 0, l <= m, l++,
dt = .0001 + .0005 l; t = 0; x = RandomReal[{0, 2 \[Pi]}];
y = RandomReal[{0, 2 \[Pi]}];
xs = Table[0 , {n, 1, k}]; ys = Table[0 , {n, 1, k}];
For[n = 1, n <= k,
n++, {xp = x, yp = y, x = Cos[4 yp] Cos[t], y = Sin[xp] Sin[t],
xs[[n]] = x, ys[[n]] = y, t = t + dt}]
data =
Partition[Flatten[Table[{xs[[n]], ys[[n]]}, {n, 1, k}]], 2] // N;
Subscript[p, l] =
ListPlot[data, AxesOrigin -> {0, 0},
PlotStyle -> {Hue[l], PointSize[.001]}, AspectRatio -> 1]];
Art = Table[Subscript[p, n], {n, 0, m}];
Show[Art]


If you consider the frequency of the cosine term as a parameter $x_{n+1} = \cos(ay_n)\cos(t_n)$ a bifurcation occurs at approx. $a=2.939$.

• Subscript[p, l] = ListPlot[data, AxesOrigin -> {0, 0}, PlotStyle -> {ColorData[3, "ColorList"][[l]], PointSize[.001]}, AspectRatio -> 1] shows i.stack.imgur.com/O3pjs.png Jun 2, 2014 at 17:59
• No, it's not numerical instability. Take a look at Peter de Jong attractor which is created in similar manner.
– Kuba
Jun 2, 2014 at 18:39

i = Module[{k = 100000, m = 5, data, dt, t},
data = (dt = .0001 + .0005 #; t = -dt;
NestList[{Cos[4 #[[2]]] Cos[t += dt], Sin[#[[1]]] Sin[t]} &,
RandomReal[{0, 2 π}, 2], k]) & /@ Range[0, m];
ListPlot[data, AspectRatio -> 1, PlotRange -> {{-1, 1}, .5 {-1, 1}}, Axes->None,
PlotStyle->({PointSize[.001], #}&/@ ColorData[17,"ColorList"])]
]


And then we go for some embellishments:

ColorNegate@ColorCombine[MeanShiftFilter[#, 5, .3] & /@
DeleteSmallComponents /@ ColorNegate /@ ColorSeparate[i]]


Edit

This is twice as fast:

r = Compile[{{k, _Integer}, {m, _Integer}},
Module[{dt, t}, (dt = .0001 + .0005 #; t = -dt;
NestList[{Cos[4 #[[2]]] Cos[t += dt], Sin[#[[1]]] Sin[t]} &,
RandomReal[{0, 2 π}, 2], k]) & /@ Range[0, m]]];
Module[{k = 100000, m = 5},
ListPlot[r[k, m], AspectRatio -> 1, PlotRange -> {{-1, 1}, .5 {-1, 1}},
Axes -> None,   PlotStyle -> ({PointSize[.001], #} & /@ ColorData[22, "ColorList"])]]

• @Kuba Quality takes time!
– Öskå
Jun 2, 2014 at 18:39
• @Öskå I don't think I it's the case :) belisarius, don't you feel like NestList is kind of slow here?
– Kuba
Jun 2, 2014 at 18:41
• @Kuba I didn't try to run it, I just liked the image :) But I like the images you linked in the comment above even more. GimmeTehCodez!
– Öskå
Jun 2, 2014 at 18:43
• @Öskå The same as here, it's even simpler because there is no equivalent of dt.
– Kuba
Jun 2, 2014 at 18:44
• Are you implying that this is now easy?
– Öskå
Jun 2, 2014 at 18:46