﻿ Euler Math Toolbox - Examples

# Random Fractals

By randomly selecting one transformation for an iteration, is is possible to generate nice fractals. If the transformations contract, the points will have limit points at a fractal K. K then is a set which is the union of all transformed copies of K. It is self similar with respect to the transformations.

Here is one example, generating the Sierpinski gasket. We first define three transformations.

```>function T1(x) := ([1,1]+x)/2; ...
function T2(x) := ([1,0]+x)/2; ...
function T3(x) := ([0,1]+x)/2
```

Now we define an iterations which takes one of the functions at random.

```>function randIter (x,f,n,p=none) ...
res=zeros(n,cols(x));
res[1]=x;
if p==none then irand=intrandom(1,n,length(f));
else irand=randpint(1,n,p);
endif;
loop 2 to n;
x=f[irand[#]](x);
res[#]=x;
end
return res;
endfunction
```

We iterate 10000 times. The result is a nx2 matrix. The points are the rows of this matrix. We discard the first 1000 iterations and define a (n-1000)x2 matrix of points.

```>P=randIter([0,0],["T1","T2","T3"],10000)[1000:10000]'; ...
plot2d(P[1],P[2],points=true,style="."):
```

Here is another example, which involves linear mappings.

```>function D(alpha) := [cos(alpha),-sin(alpha);sin(alpha),cos(alpha)]; ...
A=([0.4,0;0,1].D(100°))'; B=([0.4,0;0,1].D(-100°))'; ...
function T1(x) := x*0.6+[0,0.4]; ...
function T2(x) := (x.A)*0.8; ...
function T3(x) := (x.B)*0.8; ...
P=randIter([0,0],["T1","T2","T3"],50000)[1000:50000]'; ...
plot2d(P[1],P[2],points=true,style="."):
```

# The Fern

Here is a very nice examples. The parameters of this example were sent to me by A. Busser.

```>p = [ .85, .07, .07, 0.01]; ...
A1 = [ .85, .04; -.04, .85]; b1 = [0; 1.6]; ...
A2 = [ .20, -.26; .23, .22]; b2 = [0; 1.6]; ...
A3 = [-.15, .28; .26, .24]; b3 = [0; .44]; ...
A4 = [ 0, 0 ; 0, .16];
>function T1(x) := (A1.x'+b1)'; ...
function T2(x) := (A2.x'+b2)'; ...
function T3(x) := (A3.x'+b3)'; ...
function T4(x) := (A4.x')';
>P=randIter([0.5,0.5],["T1","T2","T3","T4"],100000,p)[1000:100000]'; ...
plot2d(P[1],P[2],a=-3,b=3,c=0,d=10,points=true,color=green,grid=0,style="."):
```

Finally, here is a three dimensional version of the Sierpinski gasket. The view is an anaglyph image. You need red/cyan glasses to appreciate it.

```>A=[1,1,1]; ...
B=[-1,1,-1]; ...
C=[-1,-1,1]; ...
D=[1,-1,-1]; ...
function T1(x) := (x+A)/2; ...
function T2(x) := (x+B)/2; ...
function T3(x) := (x+C)/2; ...
function T4(x) := (x+D)/2; ...
P=randIter([0,0,0],["T1","T2","T3","T4"],100000)[1000:100000]'; ...
plot3d(P[1],P[2],P[3],points=true,style=".",<frame,color=green,>anaglyph,zoom=3.8); ...
insimg(<antialias)
```

Examples