0
$\begingroup$

I am trying to write code for 2-variable fractal interpolation functions using two iterated function systems and two starting functions (both $y(x)=x$) which creates a sequence of piecewise defined functions using iterated functions systems(IFS) that converges to an element of the single superfractal. There is randomness involved so each run generates a different attractor.

I am not familiar with most of Mathematica's built in features, so the best I could do involved too many if-statements. The code is very long due to the fact that at each stage of the iteration process (and for each function in the chosen IFS) one of the two previous stage's functions is used. The code plots the first four stages of the two sequences of functions. There must be an easier way and any help would be much appreciated.

{a1, b1} = {.25, .4}; {a2, b2} = {.28, .22}; {a3, b3} = {.3, .26}; {a4, b4} = {.32, .1}; {a5, b5} = {.7, .8}; {a6, b6} = {.73, .6}; {a7, b7} = {.78, .8}; {a8, b8} = {.8, .6}; {c1, d1} = {.20, .45}; {c2, d2} = {.25, .3}; {c3, d3} = {.28, .45}; {c4, d4} = {.32, .2}; {c5, d5} = {.6, .75}; {c6, d6} = {.7, .65}; {c7, d7} = {.8, .7}; {c8, d8} = {.83, .6}; y0[x_] := x; z0[x_] := x; If[RandomInteger[] == 0, If[RandomInteger[] == 0, f1[x_] := b1*y0[x/a1], f1[x_] := b1*z0[x/a1]]; If[RandomInteger[] == 0, g1[x_] := (b2 - b1)*y0[(x - a1)/(a2 - a1)] + b1, g1[x_] := (b2 - b1)*z0[(x - a1)/(a2 - a1)] + b1]; If[RandomInteger[] == 0, h1[x_] := (b3 - b2)*y0[(x - a2)/(a3 - a2)] + b2, h1[x_] := (b3 - b2)*z0[(x - a2)/(a3 - a2)] + b2]; If[RandomInteger[] == 0, i1[x_] := (b4 - b3)*y0[(x - a3)/(a4 - a3)] + b3, i1[x_] := (b4 - b3)*z0[(x - a3)/(a4 - a3)] + b3]; If[RandomInteger[] == 0, j1[x_] := (b5 - b4)*y0[(x - a4)/(a5 - a4)] + b4, j1[x_] := (b5 - b4)*z0[(x - a4)/(a5 - a4)] + b4]; If[RandomInteger[] == 0, k1[x_] := (b6 - b5)*y0[(x - a5)/(a6 - a5)] + b5, k1[x_] := (b6 - b5)*z0[(x - a5)/(a6 - a5)] + b5]; If[RandomInteger[] == 0, l1[x_] := (b7 - b6)*y0[(x - a6)/(a7 - a6)] + b6, l1[x_] := (b7 - b6)*z0[(x - a6)/(a7 - a6)] + b6]; If[RandomInteger[] == 0, m1[x_] := (b8 - b7)*y0[(x - a7)/(a8 - a7)] + b7, m1[x_] := (b8 - b7)*z0[(x - a7)/(a8 - a7)] + b7]; If[RandomInteger[] == 0, n1[x_] := (1 - b8)*y0[(x - a8)/(1 - a8)] + b8, n1[x_] := (1 - b8)*z0[(x - a8)/(1 - a8)] + b8]; y1[x_] := If[0 <= x < a1, f1[x], If[a1 <= x < a2, g1[x], If[a2 <= x < a3, h1[x], If[a3 <= x < a4, i1[x], If[a4 <= x < a5, j1[x], If[a5 <= x < a6, k1[x], If[a6 <= x < a7, l1[x], If[a7 <= x < a8, m1[x], n1[x]]]]]]]]], If[RandomInteger[] == 0, o1[x_] := d1*y0[1/c1*x], o1[x_] := d1*z0[1/c1*x]]; If[RandomInteger[] == 0, p1[x_] := (d2 - d1)*y0[(x - c1)/(c2 - c1)] + d1, p1[x_] := (d2 - d1)*z0[(x - c1)/(c2 - c1)] + d1]; If[RandomInteger[] == 0, q1[x_] := (d3 - d2)*y0[(x - c2)/(c3 - c2)] + d2, q1[x_] := (d3 - d2)*z0[(x - c2)/(c3 - c2)] + d2]; If[RandomInteger[] == 0, r1[x_] := (d4 - d3)*y0[(x - c3)/(c4 - c3)] + d3, r1[x_] := (d4 - d3)*z0[(x - c3)/(c4 - c3)] + d3]; If[RandomInteger[] == 0, s1[x_] := (d5 - d4)*y0[(x - c4)/(c5 - c4)] + d4, s1[x_] := (d5 - d4)*z0[(x - c4)/(c5 - c4)] + d4]; If[RandomInteger[] == 0, t1[x_] := (d6 - d5)*y0[(x - c5)/(c6 - c5)] + d5, t1[x_] := (d6 - d5)*z0[(x - c5)/(c6 - c5)] + d5]; If[RandomInteger[] == 0, u1[x_] := (d7 - d6)*y0[(x - c6)/(c7 - c6)] + d6, u1[x_] := (d7 - d6)*z0[(x - c6)/(c7 - c6)] + d6]; If[RandomInteger[] == 0, v1[x_] := (d8 - d7)*y0[(x - c7)/(c8 - c7)] + d7, v1[x_] := (d8 - d7)*z0[(x - c7)/(c8 - c7)] + d7]; If[RandomInteger[] == 0, w1[x_] := (1 - d8)*y0[(x - c8)/(1 - c8)] + d8, w1[x_] := (1 - d8)*z0[(x - c8)/(1 - c8)] + d8]; y1[x_] := If[0 <= x < c1, o1[x], If[c1 <= x < c2, p1[x], If[c2 <= x < c3, q1[x], If[c3 <= x < c4, r1[x], If[c4 <= x < c5, s1[x], If[c5 <= x < c6, t1[x], If[c6 <= x < c7, u1[x], If[c7 <= x < c8, v1[x], w1[x]]]]]]]]]] If[RandomInteger[] == 0, If[RandomInteger[] == 0, f1[x_] := b1*y0[x/a1], f1[x_] := b1*z0[x/a1]]; If[RandomInteger[] == 0, g1[x_] := (b2 - b1)*y0[(x - a1)/(a2 - a1)] + b1, g1[x_] := (b2 - b1)*z0[(x - a1)/(a2 - a1)] + b1]; If[RandomInteger[] == 0, h1[x_] := (b3 - b2)*y0[(x - a2)/(a3 - a2)] + b2, h1[x_] := (b3 - b2)*z0[(x - a2)/(a3 - a2)] + b2]; If[RandomInteger[] == 0, i1[x_] := (b4 - b3)*y0[(x - a3)/(a4 - a3)] + b3, i1[x_] := (b4 - b3)*z0[(x - a3)/(a4 - a3)] + b3]; If[RandomInteger[] == 0, j1[x_] := (b5 - b4)*y0[(x - a4)/(a5 - a4)] + b4, j1[x_] := (b5 - b4)*z0[(x - a4)/(a5 - a4)] + b4]; If[RandomInteger[] == 0, k1[x_] := (b6 - b5)*y0[(x - a5)/(a6 - a5)] + b5, k1[x_] := (b6 - b5)*z0[(x - a5)/(a6 - a5)] + b5]; If[RandomInteger[] == 0, l1[x_] := (b7 - b6)*y0[(x - a6)/(a7 - a6)] + b6, l1[x_] := (b7 - b6)*z0[(x - a6)/(a7 - a6)] + b6]; If[RandomInteger[] == 0, m1[x_] := (b8 - b7)*y0[(x - a7)/(a8 - a7)] + b7, m1[x_] := (b8 - b7)*z0[(x - a7)/(a8 - a7)] + b7]; If[RandomInteger[] == 0, n1[x_] := (1 - b8)*y0[(x - a8)/(1 - a8)] + b8, n1[x_] := (1 - b8)*z0[(x - a8)/(1 - a8)] + b8]; z1[x_] := If[0 <= x < a1, f1[x], If[a1 <= x < a2, g1[x], If[a2 <= x < a3, h1[x], If[a3 <= x < a4, i1[x], If[a4 <= x < a5, j1[x], If[a5 <= x < a6, k1[x], If[a6 <= x < a7, l1[x], If[a7 <= x < a8, m1[x], n1[x]]]]]]]]], If[RandomInteger[] == 0, o1[x_] := d1*y0[1/c1*x], o1[x_] := d1*z0[1/c1*x]]; If[RandomInteger[] == 0, p1[x_] := (d2 - d1)*y0[(x - c1)/(c2 - c1)] + d1, p1[x_] := (d2 - d1)*z0[(x - c1)/(c2 - c1)] + d1]; If[RandomInteger[] == 0, q1[x_] := (d3 - d2)*y0[(x - c2)/(c3 - c2)] + d2, q1[x_] := (d3 - d2)*z0[(x - c2)/(c3 - c2)] + d2]; If[RandomInteger[] == 0, r1[x_] := (d4 - d3)*y0[(x - c3)/(c4 - c3)] + d3, r1[x_] := (d4 - d3)*z0[(x - c3)/(c4 - c3)] + d3]; If[RandomInteger[] == 0, s1[x_] := (d5 - d4)*y0[(x - c4)/(c5 - c4)] + d4, s1[x_] := (d5 - d4)*z0[(x - c4)/(c5 - c4)] + d4]; If[RandomInteger[] == 0, t1[x_] := (d6 - d5)*y0[(x - c5)/(c6 - c5)] + d5, t1[x_] := (d6 - d5)*z0[(x - c5)/(c6 - c5)] + d5]; If[RandomInteger[] == 0, u1[x_] := (d7 - d6)*y0[(x - c6)/(c7 - c6)] + d6, u1[x_] := (d7 - d6)*z0[(x - c6)/(c7 - c6)] + d6]; If[RandomInteger[] == 0, v1[x_] := (d8 - d7)*y0[(x - c7)/(c8 - c7)] + d7, v1[x_] := (d8 - d7)*z0[(x - c7)/(c8 - c7)] + d7]; If[RandomInteger[] == 0, w1[x_] := (1 - d8)*y0[(x - c8)/(1 - c8)] + d8, w1[x_] := (1 - d8)*z0[(x - c8)/(1 - c8)] + d8]; z1[x_] := If[0 <= x < c1, o1[x], If[c1 <= x < c2, p1[x], If[c2 <= x < c3, q1[x], If[c3 <= x < c4, r1[x], If[c4 <= x < c5, s1[x], If[c5 <= x < c6, t1[x], If[c6 <= x < c7, u1[x], If[c7 <= x < c8, v1[x], w1[x]]]]]]]]]] If[RandomInteger[] == 0, If[RandomInteger[] == 0, f2[x_] := b1*y1[x/a1], f2[x_] := b1*z1[x/a1]]; If[RandomInteger[] == 0, g2[x_] := (b2 - b1)*y1[(x - a1)/(a2 - a1)] + b1, g2[x_] := (b2 - b1)*z1[(x - a1)/(a2 - a1)] + b1]; If[RandomInteger[] == 0, h2[x_] := (b3 - b2)*y1[(x - a2)/(a3 - a2)] + b2, h2[x_] := (b3 - b2)*z1[(x - a2)/(a3 - a2)] + b2]; If[RandomInteger[] == 0, i2[x_] := (b4 - b3)*y1[(x - a3)/(a4 - a3)] + b3, i2[x_] := (b4 - b3)*z1[(x - a3)/(a4 - a3)] + b3]; If[RandomInteger[] == 0, j2[x_] := (b5 - b4)*y1[(x - a4)/(a5 - a4)] + b4, j2[x_] := (b5 - b4)*z1[(x - a4)/(a5 - a4)] + b4]; If[RandomInteger[] == 0, k2[x_] := (b6 - b5)*y1[(x - a5)/(a6 - a5)] + b5, k2[x_] := (b6 - b5)*z1[(x - a5)/(a6 - a5)] + b5]; If[RandomInteger[] == 0, l2[x_] := (b7 - b6)*y1[(x - a6)/(a7 - a6)] + b6, l2[x_] := (b7 - b6)*z1[(x - a6)/(a7 - a6)] + b6]; If[RandomInteger[] == 0, m2[x_] := (b8 - b7)*y1[(x - a7)/(a8 - a7)] + b7, m2[x_] := (b8 - b7)*z1[(x - a7)/(a8 - a7)] + b7]; If[RandomInteger[] == 0, n2[x_] := (1 - b8)*y1[(x - a8)/(1 - a8)] + b8, n2[x_] := (1 - b8)*z1[(x - a8)/(1 - a8)] + b8]; y2[x_] := If[0 <= x < a1, f2[x], If[a1 <= x < a2, g2[x], If[a2 <= x < a3, h2[x], If[a3 <= x < a4, i2[x], If[a4 <= x < a5, j2[x], If[a5 <= x < a6, k2[x], If[a6 <= x < a7, l2[x], If[a7 <= x < a8, m2[x], n2[x]]]]]]]]], If[RandomInteger[] == 0, o2[x_] := d1*y1[1/c1*x], o2[x_] := d1*z1[1/c1*x]]; If[RandomInteger[] == 0, p2[x_] := (d2 - d1)*y1[(x - c1)/(c2 - c1)] + d1, p2[x_] := (d2 - d1)*z1[(x - c1)/(c2 - c1)] + d1]; If[RandomInteger[] == 0, q2[x_] := (d3 - d2)*y1[(x - c2)/(c3 - c2)] + d2, q2[x_] := (d3 - d2)*z1[(x - c2)/(c3 - c2)] + d2]; If[RandomInteger[] == 0, r2[x_] := (d4 - d3)*y1[(x - c3)/(c4 - c3)] + d3, r2[x_] := (d4 - d3)*z1[(x - c3)/(c4 - c3)] + d3]; If[RandomInteger[] == 0, s2[x_] := (d5 - d4)*y1[(x - c4)/(c5 - c4)] + d4, s2[x_] := (d5 - d4)*z1[(x - c4)/(c5 - c4)] + d4]; If[RandomInteger[] == 0, t2[x_] := (d6 - d5)*y1[(x - c5)/(c6 - c5)] + d5, t2[x_] := (d6 - d5)*z1[(x - c5)/(c6 - c5)] + d5]; If[RandomInteger[] == 0, u2[x_] := (d7 - d6)*y1[(x - c6)/(c7 - c6)] + d6, u2[x_] := (d7 - d6)*z1[(x - c6)/(c7 - c6)] + d6]; If[RandomInteger[] == 0, v2[x_] := (d8 - d7)*y1[(x - c7)/(c8 - c7)] + d7, v2[x_] := (d8 - d7)*z1[(x - c7)/(c8 - c7)] + d7]; If[RandomInteger[] == 0, w2[x_] := (1 - d8)*y1[(x - c8)/(1 - c8)] + d8, w2[x_] := (1 - d8)*z1[(x - c8)/(1 - c8)] + d8]; y2[x_] := If[0 <= x < c1, o2[x], If[c1 <= x < c2, p2[x], If[c2 <= x < c3, q2[x], If[c3 <= x < c4, r2[x], If[c4 <= x < c5, s2[x], If[c5 <= x < c6, t2[x], If[c6 <= x < c7, u2[x], If[c7 <= x < c8, v2[x], w2[x]]]]]]]]]] If[RandomInteger[] == 0, If[RandomInteger[] == 0, f2[x_] := b1*y1[x/a1], f2[x_] := b1*z1[x/a1]]; If[RandomInteger[] == 0, g2[x_] := (b2 - b1)*y1[(x - a1)/(a2 - a1)] + b1, g2[x_] := (b2 - b1)*z1[(x - a1)/(a2 - a1)] + b1]; If[RandomInteger[] == 0, h2[x_] := (b3 - b2)*y1[(x - a2)/(a3 - a2)] + b2, h2[x_] := (b3 - b2)*z1[(x - a2)/(a3 - a2)] + b2]; If[RandomInteger[] == 0, i2[x_] := (b4 - b3)*y1[(x - a3)/(a4 - a3)] + b3, i2[x_] := (b4 - b3)*z1[(x - a3)/(a4 - a3)] + b3]; If[RandomInteger[] == 0, j2[x_] := (b5 - b4)*y1[(x - a4)/(a5 - a4)] + b4, j2[x_] := (b5 - b4)*z1[(x - a4)/(a5 - a4)] + b4]; If[RandomInteger[] == 0, k2[x_] := (b6 - b5)*y1[(x - a5)/(a6 - a5)] + b5, k2[x_] := (b6 - b5)*z1[(x - a5)/(a6 - a5)] + b5]; If[RandomInteger[] == 0, l2[x_] := (b7 - b6)*y1[(x - a6)/(a7 - a6)] + b6, l2[x_] := (b7 - b6)*z1[(x - a6)/(a7 - a6)] + b6]; If[RandomInteger[] == 0, m2[x_] := (b8 - b7)*y1[(x - a7)/(a8 - a7)] + b7, m2[x_] := (b8 - b7)*z1[(x - a7)/(a8 - a7)] + b7]; If[RandomInteger[] == 0, n2[x_] := (1 - b8)*y1[(x - a8)/(1 - a8)] + b8, n2[x_] := (1 - b8)*z1[(x - a8)/(1 - a8)] + b8]; z2[x_] := If[0 <= x < a1, f2[x], If[a1 <= x < a2, g2[x], If[a2 <= x < a3, h2[x], If[a3 <= x < a4, i2[x], If[a4 <= x < a5, j2[x], If[a5 <= x < a6, k2[x], If[a6 <= x < a7, l2[x], If[a7 <= x < a8, m2[x], n2[x]]]]]]]]], If[RandomInteger[] == 0, o2[x_] := d1*y1[1/c1*x], o2[x_] := d1*z1[1/c1*x]]; If[RandomInteger[] == 0, p2[x_] := (d2 - d1)*y1[(x - c1)/(c2 - c1)] + d1, p2[x_] := (d2 - d1)*z1[(x - c1)/(c2 - c1)] + d1]; If[RandomInteger[] == 0, q2[x_] := (d3 - d2)*y1[(x - c2)/(c3 - c2)] + d2, q2[x_] := (d3 - d2)*z1[(x - c2)/(c3 - c2)] + d2]; If[RandomInteger[] == 0, r2[x_] := (d4 - d3)*y1[(x - c3)/(c4 - c3)] + d3, r2[x_] := (d4 - d3)*z1[(x - c3)/(c4 - c3)] + d3]; If[RandomInteger[] == 0, s2[x_] := (d5 - d4)*y1[(x - c4)/(c5 - c4)] + d4, s2[x_] := (d5 - d4)*z1[(x - c4)/(c5 - c4)] + d4]; If[RandomInteger[] == 0, t2[x_] := (d6 - d5)*y1[(x - c5)/(c6 - c5)] + d5, t2[x_] := (d6 - d5)*z1[(x - c5)/(c6 - c5)] + d5]; If[RandomInteger[] == 0, u2[x_] := (d7 - d6)*y1[(x - c6)/(c7 - c6)] + d6, u2[x_] := (d7 - d6)*z1[(x - c6)/(c7 - c6)] + d6]; If[RandomInteger[] == 0, v2[x_] := (d8 - d7)*y1[(x - c7)/(c8 - c7)] + d7, v2[x_] := (d8 - d7)*z1[(x - c7)/(c8 - c7)] + d7]; If[RandomInteger[] == 0, w2[x_] := (1 - d8)*y1[(x - c8)/(1 - c8)] + d8, w2[x_] := (1 - d8)*z1[(x - c8)/(1 - c8)] + d8]; z2[x_] := If[0 <= x < c1, o2[x], If[c1 <= x < c2, p2[x], If[c2 <= x < c3, q2[x], If[c3 <= x < c4, r2[x], If[c4 <= x < c5, s2[x], If[c5 <= x < c6, t2[x], If[c6 <= x < c7, u2[x], If[c7 <= x < c8, v2[x], w2[x]]]]]]]]]] If[RandomInteger[] == 0, If[RandomInteger[] == 0, f3[x_] := b1*y2[x/a1], f3[x_] := b1*z2[x/a1]]; If[RandomInteger[] == 0, g3[x_] := (b2 - b1)*y2[(x - a1)/(a2 - a1)] + b1, g3[x_] := (b2 - b1)*z2[(x - a1)/(a2 - a1)] + b1]; If[RandomInteger[] == 0, h3[x_] := (b3 - b2)*y2[(x - a2)/(a3 - a2)] + b2, h3[x_] := (b3 - b2)*z2[(x - a2)/(a3 - a2)] + b2]; If[RandomInteger[] == 0, i3[x_] := (b4 - b3)*y2[(x - a3)/(a4 - a3)] + b3, i3[x_] := (b4 - b3)*z2[(x - a3)/(a4 - a3)] + b3]; If[RandomInteger[] == 0, j3[x_] := (b5 - b4)*y2[(x - a4)/(a5 - a4)] + b4, j3[x_] := (b5 - b4)*z2[(x - a4)/(a5 - a4)] + b4]; If[RandomInteger[] == 0, k3[x_] := (b6 - b5)*y2[(x - a5)/(a6 - a5)] + b5, k3[x_] := (b6 - b5)*z2[(x - a5)/(a6 - a5)] + b5]; If[RandomInteger[] == 0, l3[x_] := (b7 - b6)*y2[(x - a6)/(a7 - a6)] + b6, l3[x_] := (b7 - b6)*z2[(x - a6)/(a7 - a6)] + b6]; If[RandomInteger[] == 0, m3[x_] := (b8 - b7)*y2[(x - a7)/(a8 - a7)] + b7, m3[x_] := (b8 - b7)*z2[(x - a7)/(a8 - a7)] + b7]; If[RandomInteger[] == 0, n3[x_] := (1 - b8)*y2[(x - a8)/(1 - a8)] + b8, n3[x_] := (1 - b8)*z2[(x - a8)/(1 - a8)] + b8]; y3[x_] := If[0 <= x < a1, f3[x], If[a1 <= x < a2, g3[x], If[a2 <= x < a3, h3[x], If[a3 <= x < a4, i3[x], If[a4 <= x < a5, j3[x], If[a5 <= x < a6, k3[x], If[a6 <= x < a7, l3[x], If[a7 <= x < a8, m3[x], n3[x]]]]]]]]], If[RandomInteger[] == 0, o3[x_] := d1*y2[1/c1*x], o3[x_] := d1*z2[1/c1*x]]; If[RandomInteger[] == 0, p3[x_] := (d2 - d1)*y2[(x - c1)/(c2 - c1)] + d1, p3[x_] := (d2 - d1)*z2[(x - c1)/(c2 - c1)] + d1]; If[RandomInteger[] == 0, q3[x_] := (d3 - d2)*y2[(x - c2)/(c3 - c2)] + d2, q3[x_] := (d3 - d2)*z2[(x - c2)/(c3 - c2)] + d2]; If[RandomInteger[] == 0, r3[x_] := (d4 - d3)*y2[(x - c3)/(c4 - c3)] + d3, r3[x_] := (d4 - d3)*z2[(x - c3)/(c4 - c3)] + d3]; If[RandomInteger[] == 0, s3[x_] := (d5 - d4)*y2[(x - c4)/(c5 - c4)] + d4, s3[x_] := (d5 - d4)*z2[(x - c4)/(c5 - c4)] + d4]; If[RandomInteger[] == 0, t3[x_] := (d6 - d5)*y2[(x - c5)/(c6 - c5)] + d5, t3[x_] := (d6 - d5)*z2[(x - c5)/(c6 - c5)] + d5]; If[RandomInteger[] == 0, u3[x_] := (d7 - d6)*y2[(x - c6)/(c7 - c6)] + d6, u3[x_] := (d7 - d6)*z2[(x - c6)/(c7 - c6)] + d6]; If[RandomInteger[] == 0, v3[x_] := (d8 - d7)*y2[(x - c7)/(c8 - c7)] + d7, v3[x_] := (d8 - d7)*z2[(x - c7)/(c8 - c7)] + d7]; If[RandomInteger[] == 0, w3[x_] := (1 - d8)*y2[(x - c8)/(1 - c8)] + d8, w3[x_] := (1 - d8)*z2[(x - c8)/(1 - c8)] + d8]; y3[x_] := If[0 <= x < c1, o3[x], If[c1 <= x < c2, p3[x], If[c2 <= x < c3, q3[x], If[c3 <= x < c4, r3[x], If[c4 <= x < c5, s3[x], If[c5 <= x < c6, t3[x], If[c6 <= x < c7, u3[x], If[c7 <= x < c8, v3[x], w3[x]]]]]]]]]] If[RandomInteger[] == 0, If[RandomInteger[] == 0, f3[x_] := b1*y2[x/a1], f3[x_] := b1*z2[x/a1]]; If[RandomInteger[] == 0, g3[x_] := (b2 - b1)*y2[(x - a1)/(a2 - a1)] + b1, g3[x_] := (b2 - b1)*z2[(x - a1)/(a2 - a1)] + b1]; If[RandomInteger[] == 0, h3[x_] := (b3 - b2)*y2[(x - a2)/(a3 - a2)] + b2, h3[x_] := (b3 - b2)*z2[(x - a2)/(a3 - a2)] + b2]; If[RandomInteger[] == 0, i3[x_] := (b4 - b3)*y2[(x - a3)/(a4 - a3)] + b3, i3[x_] := (b4 - b3)*z2[(x - a3)/(a4 - a3)] + b3]; If[RandomInteger[] == 0, j3[x_] := (b5 - b4)*y2[(x - a4)/(a5 - a4)] + b4, j3[x_] := (b5 - b4)*z2[(x - a4)/(a5 - a4)] + b4]; If[RandomInteger[] == 0, k3[x_] := (b6 - b5)*y2[(x - a5)/(a6 - a5)] + b5, k3[x_] := (b6 - b5)*z2[(x - a5)/(a6 - a5)] + b5]; If[RandomInteger[] == 0, l3[x_] := (b7 - b6)*y2[(x - a6)/(a7 - a6)] + b6, l3[x_] := (b7 - b6)*z2[(x - a6)/(a7 - a6)] + b6]; If[RandomInteger[] == 0, m3[x_] := (b8 - b7)*y2[(x - a7)/(a8 - a7)] + b7, m3[x_] := (b8 - b7)*z2[(x - a7)/(a8 - a7)] + b7]; If[RandomInteger[] == 0, n3[x_] := (1 - b8)*y2[(x - a8)/(1 - a8)] + b8, n3[x_] := (1 - b8)*z2[(x - a8)/(1 - a8)] + b8]; z3[x_] := If[0 <= x < a1, f3[x], If[a1 <= x < a2, g3[x], If[a2 <= x < a3, h3[x], If[a3 <= x < a4, i3[x], If[a4 <= x < a5, j3[x], If[a5 <= x < a6, k3[x], If[a6 <= x < a7, l3[x], If[a7 <= x < a8, m3[x], n3[x]]]]]]]]], If[RandomInteger[] == 0, o3[x_] := d1*y2[1/c1*x], o3[x_] := d1*z2[1/c1*x]]; If[RandomInteger[] == 0, p3[x_] := (d2 - d1)*y2[(x - c1)/(c2 - c1)] + d1, p3[x_] := (d2 - d1)*z2[(x - c1)/(c2 - c1)] + d1]; If[RandomInteger[] == 0, q3[x_] := (d3 - d2)*y2[(x - c2)/(c3 - c2)] + d2, q3[x_] := (d3 - d2)*z2[(x - c2)/(c3 - c2)] + d2]; If[RandomInteger[] == 0, r3[x_] := (d4 - d3)*y2[(x - c3)/(c4 - c3)] + d3, r3[x_] := (d4 - d3)*z2[(x - c3)/(c4 - c3)] + d3]; If[RandomInteger[] == 0, s3[x_] := (d5 - d4)*y2[(x - c4)/(c5 - c4)] + d4, s3[x_] := (d5 - d4)*z2[(x - c4)/(c5 - c4)] + d4]; If[RandomInteger[] == 0, t3[x_] := (d6 - d5)*y2[(x - c5)/(c6 - c5)] + d5, t3[x_] := (d6 - d5)*z2[(x - c5)/(c6 - c5)] + d5]; If[RandomInteger[] == 0, u3[x_] := (d7 - d6)*y2[(x - c6)/(c7 - c6)] + d6, u3[x_] := (d7 - d6)*z2[(x - c6)/(c7 - c6)] + d6]; If[RandomInteger[] == 0, v3[x_] := (d8 - d7)*y2[(x - c7)/(c8 - c7)] + d7, v3[x_] := (d8 - d7)*z2[(x - c7)/(c8 - c7)] + d7]; If[RandomInteger[] == 0, w3[x_] := (1 - d8)*y2[(x - c8)/(1 - c8)] + d8, w3[x_] := (1 - d8)*z2[(x - c8)/(1 - c8)] + d8]; z3[x_] := If[0 <= x < c1, o3[x], If[c1 <= x < c2, p3[x], If[c2 <= x < c3, q3[x], If[c3 <= x < c4, r3[x], If[c4 <= x < c5, s3[x], If[c5 <= x < c6, t3[x], If[c6 <= x < c7, u3[x], If[c7 <= x < c8, v3[x], w3[x]]]]]]]]]] If[RandomInteger[] == 0, If[RandomInteger[] == 0, f4[x_] := b1*y3[x/a1], f4[x_] := b1*z3[x/a1]]; If[RandomInteger[] == 0, g4[x_] := (b2 - b1)*y3[(x - a1)/(a2 - a1)] + b1, g4[x_] := (b2 - b1)*z3[(x - a1)/(a2 - a1)] + b1]; If[RandomInteger[] == 0, h4[x_] := (b3 - b2)*y3[(x - a2)/(a3 - a2)] + b2, h4[x_] := (b3 - b2)*z3[(x - a2)/(a3 - a2)] + b2]; If[RandomInteger[] == 0, i4[x_] := (b4 - b3)*y3[(x - a3)/(a4 - a3)] + b3, i4[x_] := (b4 - b3)*z3[(x - a3)/(a4 - a3)] + b3]; If[RandomInteger[] == 0, j4[x_] := (b5 - b4)*y3[(x - a4)/(a5 - a4)] + b4, j4[x_] := (b5 - b4)*z3[(x - a4)/(a5 - a4)] + b4]; If[RandomInteger[] == 0, k4[x_] := (b6 - b5)*y3[(x - a5)/(a6 - a5)] + b5, k4[x_] := (b6 - b5)*z3[(x - a5)/(a6 - a5)] + b5]; If[RandomInteger[] == 0, l4[x_] := (b7 - b6)*y3[(x - a6)/(a7 - a6)] + b6, l4[x_] := (b7 - b6)*z3[(x - a6)/(a7 - a6)] + b6]; If[RandomInteger[] == 0, m4[x_] := (b8 - b7)*y3[(x - a7)/(a8 - a7)] + b7, m4[x_] := (b8 - b7)*z3[(x - a7)/(a8 - a7)] + b7]; If[RandomInteger[] == 0, n4[x_] := (1 - b8)*y3[(x - a8)/(1 - a8)] + b8, n4[x_] := (1 - b8)*z3[(x - a8)/(1 - a8)] + b8]; y4[x_] := If[0 <= x < a1, f4[x], If[a1 <= x < a2, g4[x], If[a2 <= x < a3, h4[x], If[a3 <= x < a4, i4[x], If[a4 <= x < a5, j4[x], If[a5 <= x < a6, k4[x], If[a6 <= x < a7, l4[x], If[a7 <= x < a8, m4[x], n4[x]]]]]]]]], If[RandomInteger[] == 0, o4[x_] := d1*y3[1/c1*x], o4[x_] := d1*z3[1/c1*x]]; If[RandomInteger[] == 0, p4[x_] := (d2 - d1)*y3[(x - c1)/(c2 - c1)] + d1, p4[x_] := (d2 - d1)*z3[(x - c1)/(c2 - c1)] + d1]; If[RandomInteger[] == 0, q4[x_] := (d3 - d2)*y3[(x - c2)/(c3 - c2)] + d2, q4[x_] := (d3 - d2)*z3[(x - c2)/(c3 - c2)] + d2]; If[RandomInteger[] == 0, r4[x_] := (d4 - d3)*y3[(x - c3)/(c4 - c3)] + d3, r4[x_] := (d4 - d3)*z3[(x - c3)/(c4 - c3)] + d3]; If[RandomInteger[] == 0, s4[x_] := (d5 - d4)*y3[(x - c4)/(c5 - c4)] + d4, s4[x_] := (d5 - d4)*z3[(x - c4)/(c5 - c4)] + d4]; If[RandomInteger[] == 0, t4[x_] := (d6 - d5)*y3[(x - c5)/(c6 - c5)] + d5, t4[x_] := (d6 - d5)*z3[(x - c5)/(c6 - c5)] + d5]; If[RandomInteger[] == 0, u4[x_] := (d7 - d6)*y3[(x - c6)/(c7 - c6)] + d6, u4[x_] := (d7 - d6)*z3[(x - c6)/(c7 - c6)] + d6]; If[RandomInteger[] == 0, v4[x_] := (d8 - d7)*y3[(x - c7)/(c8 - c7)] + d7, v4[x_] := (d8 - d7)*z3[(x - c7)/(c8 - c7)] + d7]; If[RandomInteger[] == 0, w4[x_] := (1 - d8)*y3[(x - c8)/(1 - c8)] + d8, w4[x_] := (1 - d8)*z3[(x - c8)/(1 - c8)] + d8]; y4[x_] := If[0 <= x < c1, o4[x], If[c1 <= x < c2, p4[x], If[c2 <= x < c3, q4[x], If[c3 <= x < c4, r4[x], If[c4 <= x < c5, s4[x], If[c5 <= x < c6, t4[x], If[c6 <= x < c7, u4[x], If[c7 <= x < c8, v4[x], w4[x]]]]]]]]]] If[RandomInteger[] == 0, If[RandomInteger[] == 0, f4[x_] := b1*y3[x/a1], f4[x_] := b1*z3[x/a1]]; If[RandomInteger[] == 0, g4[x_] := (b2 - b1)*y3[(x - a1)/(a2 - a1)] + b1, g4[x_] := (b2 - b1)*z3[(x - a1)/(a2 - a1)] + b1]; If[RandomInteger[] == 0, h4[x_] := (b3 - b2)*y3[(x - a2)/(a3 - a2)] + b2, h4[x_] := (b3 - b2)*z3[(x - a2)/(a3 - a2)] + b2]; If[RandomInteger[] == 0, i4[x_] := (b4 - b3)*y3[(x - a3)/(a4 - a3)] + b3, i4[x_] := (b4 - b3)*z3[(x - a3)/(a4 - a3)] + b3]; If[RandomInteger[] == 0, j4[x_] := (b5 - b4)*y3[(x - a4)/(a5 - a4)] + b4, j4[x_] := (b5 - b4)*z3[(x - a4)/(a5 - a4)] + b4]; If[RandomInteger[] == 0, k4[x_] := (b6 - b5)*y3[(x - a5)/(a6 - a5)] + b5, k4[x_] := (b6 - b5)*z3[(x - a5)/(a6 - a5)] + b5]; If[RandomInteger[] == 0, l4[x_] := (b7 - b6)*y3[(x - a6)/(a7 - a6)] + b6, l4[x_] := (b7 - b6)*z3[(x - a6)/(a7 - a6)] + b6]; If[RandomInteger[] == 0, m4[x_] := (b8 - b7)*y3[(x - a7)/(a8 - a7)] + b7, m4[x_] := (b8 - b7)*z3[(x - a7)/(a8 - a7)] + b7]; If[RandomInteger[] == 0, n4[x_] := (1 - b8)*y3[(x - a8)/(1 - a8)] + b8, n4[x_] := (1 - b8)*z3[(x - a8)/(1 - a8)] + b8]; z4[x_] := If[0 <= x < a1, f4[x], If[a1 <= x < a2, g4[x], If[a2 <= x < a3, h4[x], If[a3 <= x < a4, i4[x], If[a4 <= x < a5, j4[x], If[a5 <= x < a6, k4[x], If[a6 <= x < a7, l4[x], If[a7 <= x < a8, m4[x], n4[x]]]]]]]]], If[RandomInteger[] == 0, o4[x_] := d1*y3[1/c1*x], o4[x_] := d1*z3[1/c1*x]]; If[RandomInteger[] == 0, p4[x_] := (d2 - d1)*y3[(x - c1)/(c2 - c1)] + d1, p4[x_] := (d2 - d1)*z3[(x - c1)/(c2 - c1)] + d1]; If[RandomInteger[] == 0, q4[x_] := (d3 - d2)*y3[(x - c2)/(c3 - c2)] + d2, q4[x_] := (d3 - d2)*z3[(x - c2)/(c3 - c2)] + d2]; If[RandomInteger[] == 0, r4[x_] := (d4 - d3)*y3[(x - c3)/(c4 - c3)] + d3, r4[x_] := (d4 - d3)*z3[(x - c3)/(c4 - c3)] + d3]; If[RandomInteger[] == 0, s4[x_] := (d5 - d4)*y3[(x - c4)/(c5 - c4)] + d4, s4[x_] := (d5 - d4)*z3[(x - c4)/(c5 - c4)] + d4]; If[RandomInteger[] == 0, t4[x_] := (d6 - d5)*y3[(x - c5)/(c6 - c5)] + d5, t4[x_] := (d6 - d5)*z3[(x - c5)/(c6 - c5)] + d5]; If[RandomInteger[] == 0, u4[x_] := (d7 - d6)*y3[(x - c6)/(c7 - c6)] + d6, u4[x_] := (d7 - d6)*z3[(x - c6)/(c7 - c6)] + d6]; If[RandomInteger[] == 0, v4[x_] := (d8 - d7)*y3[(x - c7)/(c8 - c7)] + d7, v4[x_] := (d8 - d7)*z3[(x - c7)/(c8 - c7)] + d7]; If[RandomInteger[] == 0, w4[x_] := (1 - d8)*y3[(x - c8)/(1 - c8)] + d8, w4[x_] := (1 - d8)*z3[(x - c8)/(1 - c8)] + d8]; z4[x_] := If[0 <= x < c1, o4[x], If[c1 <= x < c2, p4[x], If[c2 <= x < c3, q4[x], If[c3 <= x < c4, r4[x], If[c4 <= x < c5, s4[x], If[c5 <= x < c6, t4[x], If[c6 <= x < c7, u4[x], If[c7 <= x < c8, v4[x], w4[x]]]]]]]]]]; GraphicsRow[{Show[Plot[y1[x], {x, 0, 1}], PlotRange -> {{0, 1}, {0, 1}}], Show[Plot[z1[x], {x, 0, 1}], PlotRange -> {{0, 1}, {0, 1}}]}, ImageSize -> {Large}] GraphicsRow[{Show[Plot[y2[x], {x, 0, 1}], PlotRange -> {{0, 1}, {0, 1}}], Show[Plot[z2[x], {x, 0, 1}], PlotRange -> {{0, 1}, {0, 1}}]}, ImageSize -> {Large}] GraphicsRow[{Show[Plot[y3[x], {x, 0, 1}], PlotRange -> {{0, 1}, {0, 1}}], Show[Plot[z3[x], {x, 0, 1}], PlotRange -> {{0, 1}, {0, 1}}]}, ImageSize -> {Large}] GraphicsRow[{Show[Plot[y4[x], {x, 0, 1}], PlotRange -> {{0, 1}, {0, 1}}], Show[Plot[z4[x], {x, 0, 1}], PlotRange -> {{0 , 1}, {0, 1}}]}, ImageSize -> {Large}] 
$\endgroup$
1
  • 3
    $\begingroup$ Please try to post a minimal working example showing your troubles. Going through all that code is too tedious. $\endgroup$ Commented May 30, 2014 at 5:54

1 Answer 1

2
$\begingroup$

I notice some striking similarities between this question and the following two:

Presumably, the user is the same, which conceals information about your mathematical and programming background - information that's useful to potential answers. Nonetheless, the question is interesting, so here's a go.

As I understand it, you're simultaneously generating two functions $y(x)$ and $z(x)$ on $[0,1]$. The $n^{\text{th}}$ approximations $y_n(x)$ and $z_n(x)$ are piecewise linear functions starting with $y_0(x)=z_0(x)=x$. You have two partitions of $[0,1]$, namely $$0<a_1<a_2<\cdots<a_8<1$$ and $$0<c_1<c_2<\cdots<c_8<1.$$ Each of $y_n(x)$ and $z_n(x)$ will be constructed by first randomly choosing one of those partitions (one for $y$ and one for $z$) and then placing scaled copies of either $y_{n-1}$ or $z_{n-1}$ (also chosen randomly and independently for each sub-interval) over each of the nine sub-intervals in the partition.

The main problem with your code, is that you're repeating tremendous amounts of code, rather than encapsulating repetitive stuff into functions. For example, you define y0, y1, y2, y3, and y4 using the exact same code with suitable replacements. Why not define a function and then recursively define y[n]?

Here's one approach:

(* Your as and cs *) (* One partition will be randomly chosen for each step *) partitions = { Partition[{0.0, 0.25, 0.28, 0.3, 0.32, 0.7, 0.73, 0.78, 0.8, 1.0}, 2, 1], Partition[{0.0, 0.2, 0.25, 0.28, 0.32, 0.6, 0.7, 0.8, 0.83, 1.0}, 2, 1] }; (* Your bs and ds *) compressions = { Partition[{0, 0.4, 0.22, 0.26, 0.1, 0.8, 0.6, 0.8, 0.6, 1}, 2, 1], Partition[{0, 0.45, 0.3, 0.45, 0.2, 0.75, 0.65, 0.7, 0.6, 1}, 2, 1] }; transform[f_, x_, {a1_, a2_}, {b1_, b2_}] := (b2 - b1)*(f /. x -> (x - a1)/(a2 - a1)) + b1; next[{y_, z_}, x_] := With[{ part = RandomChoice[partitions], bs = RandomChoice[compressions]}, Piecewise[Table[ {transform[RandomChoice[{y, z}], x, part[[k]], bs[[k]]], part[[k, 1]] <= x <= part[[k, 2]]}, {k, 1, Length[partitions[[1]]]}]]]; 

Note that next accepts two expressions of x, rather than functions. It then returns a single Piecewise defined expression. The recursion can be implemented as follows:

Clear[y, z]; y[0] = z[0] = x; y[n_] := y[n] = next[{y[n - 1], z[n - 1]}, x]; z[n_] := z[n] = next[{y[n - 1], z[n - 1]}, x]; 

We now graph it.

GraphicsGrid[Table[Plot[#, {x, 0, 1}] & /@ {y[n], z[n]}, {n, 1, 4}]] 

An alternative approach is to use functional iteration.

step[{y_, z_}, x_] := {next[{y, z}, x], next[{y, z}, x]}; iterated = NestList[step[#, x] &, {x, x}, 4]; GraphicsGrid[Map[Plot[#, {x, 0, 1}] &, Rest[iterated], {2}]] 

In either case, the graphs look like so:

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.