clear
% Tranformations to Create Chaos Name
% Each row corresponds to [a b; c d] and [e,f]
% The probabilities listed here are cumlative
Data=[ 0     0.053      -0.429    0           -7.083     5.43   0.05;
    0.143    0           0       -0.053       -5.619     8.513  0.10;
    0.143    0           0        0.083       -5.619     2.057  0.15;
    0        0.053       0.429    0           -3.952     5.43   0.20;
    0.119    0           0        0.053       -2.555     4.536  0.25;
   -0.01238 -0.0649723   0.42381  0.00189797  -1.226     5.235  0.30;
    0.08522  0.0506328   0.42044  0.0156626   -0.421     4.569  0.35;
    0.10443  0.00529117  0.05705  0.0527352    0.976     8.113  0.40;
   -0.00814 -0.0417935   0.42392  0.00415972   1.934     5.37   0.45;
    0.093    0           0        0.053        0.861     4.536  0.50;
    0        0.053      -0.429    0            2.447     5.43   0.55;
    0.119    0           0       -0.053        3.363     8.513  0.60;
    0.119    0           0        0.053        3.363     1.487  0.65;
    0        0.053       0.429    0            3.972     4.569  0.70;
    0.12399 -0.00183957  0.00069  0.0629731    6.275     7.716  0.75;
    0        0.053       0.167    0            5.215     6.483  0.80;
    0.071    0           0        0.053        6.279     5.298  0.85;
    0       -0.053      -0.238    0            6.805     3.714  0.90;
   -0.121    0           0        0.053        5.941     1.487  1.00];
% Create Transformation Matrix, Transkation and Probability Table
[nrow ncol]=size(Data);
for j=1:nrow
    A(:,:,j)=reshape(Data(j,1:4),2,2)';
    B(:,:,j)=reshape(Data(j,5:6),2,1);
    P(:,j)=Data(j,7);
end
 
z=rand(1,2)';
Z=[z'];
N=100000;

% Initialize Plot
figure(1) 
h = animatedline;
h.LineStyle = 'none';
h.Marker='.'; 
h.MarkerSize=2;
h.Color='b';

axis([-8 8 0 9]);

% Iterate the IFS
for i=1:N
    q=rand;
    j=sum(P<q)+1;   %index of the function to be applied
    z = A(:,:,j)*z+B(:,:,j);
    Z=[Z ; z'];
    addpoints(h,z(1),z(2))
    drawnow
end

%plot(Z(:,1),Z(:,2),'.','markersize',3)
