Fibonacci Spiral in Python

I always like to play around with my models and see what happens next. Don’t you? Problem is that most of the time we don’t know how to do it and with what resources… Let me show you how to model a Fibonacci spiral easy.

First we will use Python. Don’t worry. Install Python or just install Anaconda and open Spider.

Once in spider paste the following code to the editor and hit play.

The result should be something like this:

Fibonacci Python Generated Spiral
Generating a Fibonacci spiral.

There is a nice page that explains how the python script works and gives a deeper insight into the Fibonacci series:

Week 1: Fibonacci Sequence and the Golden Ratio

# chromoSpirals.py
# ----------------
# Code written by Peter Derlien, University of Sheffield, March 2013
# Draws spiralling patterns of circles using the Golden Angle.
# ----------------
# Import from the numpy and matplotlib packages.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.collections import PatchCollection
import matplotlib.patches as mpatches
ox=0.5; oy=0.4 # centre of plot
ndiscs=300
ndiscs=input('No. of discs (e.g. 300)? ')
ndiscs=int(ndiscs)
ncols=input('no. of colours (1 to 34)? ')
ncols=int(ncols)
offset=0.0
offset=input('offset (in radians) from golden angle? ')
offset = float(offset)
tau=(1+5**0.5)/2.0 # golden ratio approx = 1.618033989
#(2-tau)*2*np.pi is golden angle = c. 2.39996323 radians, or c. 137.5 degrees
inc = (2-tau)*2*np.pi + offset
theta=0
k=0.1 # scale factor
drad=k*(1+5**0.5)/4.0 # radius of each disc
minv=maxv=0 # minv and maxv will be used later to display inputs chosen
# now collect in list 'patches' the locations of all the discs
patches = []
for j in range(1,ndiscs+1):
r = k*j**0.5
theta += inc
x = ox + r*np.cos(theta)
y = oy + r*np.sin(theta)
if y > maxv:
maxv=y
elif y < minv:
minv=y
disc = mpatches.Circle((x,y),drad)
patches.append(disc)
# start building the plot
fig = plt.figure()
ax = plt.axes([0,0,1,1])
# create text to show which inputs the user has chosen
font = "sans-serif"
maxv=maxv*0.95
nd = 'ndiscs: '+ str(ndiscs)
plt.text(minv, maxv, nd, ha="center",family=font, size=14)
setting = 'angle offset: '+ str(offset)
plt.text(minv, minv, setting, ha="center",family=font, size=14)
nc = 'ncols: '+ str(ncols)
plt.text(maxv, maxv, nc, ha="left",family=font, size=14)
# build colour cycle, using a number between 0 and 100 for each colour
colcycle=[]
s=100/ncols
for j in range(ndiscs):
colcycle.append((j%ncols)*s)
# bring together the information for locations and colours of discs
collection = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=1.0)
collection.set_array(np.array(colcycle))
ax.add_collection(collection)
ax.set_xticks([]); ax.set_yticks([]) # suppress display of axes
plt.axis('equal')
plt.show() # display the plot we have built 

Natures numbers – Fibonacci

It is really beautiful when we can see math in the world around us! How can a flower find the most optimised structure to balance growth, sunlight and moisture in each seasonal energy burst? I guess millions and millions of year and we get the golden ratio in the fibonacci series.

Fibonacci Spiralled Flower
A beautiful image portraying Fibonacci Structures

 

Mathematical Spirals

Today we use super computers and special algorithms for complex problems, however in the past mathematicians could model problems and solve them with very little resources. It is always good to get to the basics and see art in the form of math.

Leonardo of Pisa nicknamed “Fibonacci”, brought state of the art Arab mathematics to medieval Europe. One of his contribution was his book Liber Abacci where it described a series that we commonly see in nature. It’s all about spirals but they follow a rule…

The Fibonacci series can be seen as a numeric sequence starting with two ones where each subsequent number is equal to the sum of the preceding two numbers: 1, 1, 2, 3, 5, 8, 13, and so on.

FibonacciSpiral

Although this number series was known in India previously, Leonardo helped spread this knowledge worldwide.

The most interesting thing is that  he stumbled upon it trying to solve a rabbit problem! I think the book proposed the following:

 ‘A man put a pair of rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits are produced from that pair in a year, if it is supposed that every month each pair produces a new pair, which from the second month onwards becomes productive?’