% Set the number of iterations
num_iterations = 13;
% Call the main function to generate and plot the Barnsley Fern
barnsley_fern(num_iterations);
% Function to generate and plot the Barnsley Fern
function barnsley_fern(num_iterations)
% Initial figure: a closed equilateral triangle
S = [0, 0; 4, 0; 2, 4 * sqrt(3)/2; 0, 0]; % Triangle with vertices (0,0), (4,0), (2, height) and closing the loop
to the first point
% Define the transformation matrices and translation vectors
A1 = [0 0; 0 0.16]; t1 = [0; 0];
A2 = [0.85 0.04; -0.04 0.85]; t2 = [0; 1.6];
A3 = [0.2 -0.26; 0.23 0.22]; t3 = [0; 1.6];
A4 = [-0.15 0.28; 0.26 0.24]; t4 = [0; 0.44];
% Initialize the figure
figure;
axis equal;
hold on;
axis off;
% Iterate to generate the Barnsley Fern
for i = 1:num_iterations
% Apply transformations deterministically
S = apply_transformations(S, A1, t1, A2, t2, A3, t3, A4, t4);
end
% Plot the final iteration
plot(S(:,1), S(:,2), 'o-', 'MarkerSize', 2, 'Color', 'b');
hold off;
end
% Function to apply transformations deterministically
function new_S = apply_transformations(S, A1, t1, A2, t2, A3, t3, A4, t4)
% Initialize new set
new_S = zeros(size(S, 1) * 4, 2); % Four times the number of original points
% Apply each transformation to the entire figure
new_S(1:size(S, 1), :) = (A1 * S')' + repmat(t1', size(S, 1), 1);
new_S(size(S, 1)+1:2*size(S, 1), :) = (A2 * S')' + repmat(t2', size(S, 1), 1);
new_S(2*size(S, 1)+1:3*size(S, 1), :) = (A3 * S')' + repmat(t3', size(S, 1), 1);
new_S(3*size(S, 1)+1:end, :) = (A4 * S')' + repmat(t4', size(S, 1), 1);
end
OUTPUT WITH 13 iteration BY starting with triangle