The code is here: https://dl.dropboxusercontent.com/u/43553083/oldfern.m We will
ID: 3541115 • Letter: T
Question
The code is here: https://dl.dropboxusercontent.com/u/43553083/oldfern.m
We will be growing ferns by random repetition of a few simple matrix/vector transformations. We have seen above how to multiply a 2-by-2 matrix and a 2-by-1 vector and we have interpreted the "action" via stretching and rotation. A third natural geometric transformation is to shift or translate. This is done algebraically by simple vector addition. In particular, if x=[x(l);x(2)J and y=[y(l);y(2)] are two 2-by-1 vectors then their sum z=x+y is also 2-by-1 and its elements are z(l)=x(l)+y(l) and z(2)=x(2)+y(2). The old-school fern below was produced by this code You see that at each step it generates a random number and applies one of two possible affine transformations, the first with probability 0.3 and the second with probability 0.7. Your task is to dissect and document this existing code and then to write and run code (called fern. m) that produces a significantly more interesting fern. In particular, your new program should successively generate a random number and should apply z = [0 0;0 0.16]*z with p = 0.01 apply z =[0.85 0.04; -0.04 0.85]*z + [0; 1.6] with p = 0.85 apply z = [0.2 -0.26; 0.23 0.22]*z + [0; 1.6] with p = 0.07 apply z = [-0.15 0.28; 0.26 0.24]*z + [0; 0.44] with p = 0.07 You should accomplish this with a single if clause, although, given that your logic now forks four ways, you will want, to make use of some subordinate else if statements.Explanation / Answer
z = [0; 0];
for j = 1:4000,
r = rand;
if r < 0.01
z = [0 0;0 0.16]*z;
else if r>0.01 && r<0.07
z = [0.85 0.04;-0.04 0.85]*z+[0;1.6];
else
z=[0.2 -0.26;0.23 0.22]*z+[0;1.6];
end
end
plot(z(1),z(2),'.','markersize',4)
hold on
end
hold off
axis off