#Barnsley Fern Fractal Using pg. 309 Linear Functions and Probabilities
@interact
def f(x=(1), y=(1), n=(1..10000)):
#f(x(1),y(1)) is the multivariate function that begins with the initial value (1,1). n=(10..10000) is the range of iterations, starting with 1 and ending at 10000 where the fern shape is distinct.
l=[]
for i in range(n):
#This tells the operator to begin a list with elements i in the range of 1 to 10000. This will become the list of iterations that will be plotted.
r = random()
#Based on the formula on pg. 309, we know probabilities will be involved, and the random number generator with if...elif...else statements is an elegant way to find the outcome of stochastic processes.
if 0<= r <= 0.77:
temp=[0.85*x+0.04*y+0.075,-0.04*x+0.85*y+0.18]
[x,y]=temp
elif 0.77< r <= 0.89:
temp=[0.2*x-0.26*y+0.4,0.23*x+0.22*y+0.045]
[x,y]=temp
elif 0.89< r <= 0.99:
temp=[-0.15*x+0.28*y+0.575,0.26*x+0.24*y-0.086]
[x,y]=temp
else:
temp=[0.5,0.16*y]
[x,y]=temp
l=l+[temp]
#This is the basic iteration formula.
show(point(l, pointsize=2))
#This plots the list of iterations.
|
|
Click to the left again to hide and once more to show the dynamic interactive window
|