Maple Two Coursework - Fractal Fern

The following code is to define the fern() procedure, it takes as it's input; an integer. This will be how many times the code is to repeat itself. The initial step for this code after importing 'linalg,' and declaring the local variables is to set up the array fernPoints with the initial staring point of (0,0).

The code then loops from 1 until the specified stop point, setting the previousPoint matrix to the most recent element in the array fernPoints. A random number (randNum) is then chosen and the size of that number, determines which of the succeeding logic statements are executed.

Each transition, determined by probabilities, consists of an affine transformation on the previous point. This is achieved through matrix operations of multiplying the coefficient transformation (coffTrans) by the previous point; then the addition of additionTrans.

The next point (determined by the previous affine transformation) is then extracted from the matrix and added onto the end of the array fernPoints. Outside of the loop, fernPoints is then plotted and the fern reveals itself.

fern() code:-

fern:= proc(repeat::integer)
     local fernPoints, i, nextPoint, previousPoint, randNum, coffTrans, additionTrans;   
     fernPoints := [[0, 0]];
     with(linalg);
     for i from 1 to repeat do
          previousPoint:= matrix([[fernPoints[nops(fernPoints)][1]],
            [fernPoints[nops(fernPoints)][2]]]);
          randNum:= rand(1..100)();
             # Transition One
             if randNum <= 82 then
               coffTrans:= matrix([[0.86, 0.03],[-0.03, 0.86]]);
               additionTrans:= matrix([[0],[1.5]]);
             # Transition Two
             elif randNum <= 90 then
               coffTrans:= matrix([[0.2, -0.25],[0.21, 0.23]]);
               additionTrans:= matrix([[0],[1.5]]);
             # Transition Three
             elif randNum <= 98 then
               coffTrans:= matrix([[-0.15, 0.27],[0.25, 0.26]]);
               additionTrans:= matrix([[0],[0.45]]);
             # Transition Four
             else
               coffTrans:= matrix([[0, 0],[0, 0.17]]);
               additionTrans:= matrix([[0],[0]]);
          fi:
         nextPoint:= evalm(evalm((coffTrans &* previousPoint)) + evalm(additionTrans));          
         fernPoints:= [op(fernPoints), [(nextPoint[1, 1]),(nextPoint[2, 1])]];
         od:
         plot(fernPoints, color=[green], style=[point]);
end:

Testing:-

The code was tested at three stages of the fern's growth; at 500, 2500 and 5000. In addition to the three stages of fern growth stated in the brief, I decided to plot an extra stage at 10,000 repeats.

fern(500);

After the fern procedure is repeated 500 times, the fern is not fully developed but you can see the initial stage.

fern(2500);

After the fern procedure is repeated 2500 times, the fern is not fully developed but you can see what it will look like.

fern(5000);

After the fern procedure is repeated 5000 times, The fern is nearly fully developed

fern(10000);

After the fern procedure is repeated 10000 times, the fern is now fully developed