Using LabTalk to generate and plot Fibonacci sequences


When Fibonacci introduced his eponymous numbers to the Western world in the 11th century, he could hardly have predicted the influence they would bear on art and science. Initially published as a model to determine the breeding patterns of rabbits, today Fibonacci numbers are widely used, not only in industrial design, but for predicting stock prices, converting units of measurement, and even as a means of optimizing search engine queries. Origin makes it easy for users to not only generate this series, but also plot its values with precision in a number of ways. To follow along with this blog in Origin, the OPJ for it can be downloaded here.

Let’s assume we want to generate 1000 integers from this recursively-defined series. First we’ll need to populate our worksheet (“FibSeq and phi” in Fibonacci_blog.opj) with natural numbers by entering data(1,1000) in the F(x) header of our first column. Then we’ll right click on the adjacent column and open the Set Column Values dialog to define our Fibonacci series. In the col(B) = input simply enter-

This will point our finished Fibonacci script to the 1000 integers in the first column, allowing us to define the size of this script.

For all intents and purposes, please copy the code provided. If you feel typing it will help you better remember it, please be aware that any accidental changes in spaces or punctuation will adversely affect its functionality.

Next in the “Before Formula Scripts” tab, copy and paste–

So how does this script break down? Here are some of the key features–
In our first line, we define the name of our function, “fib”, and then the name our input will be given, “dataset aa”, which can be any range variable (or in the above case, col(A)). Subsequently, our third line converts the size of our integer data into a single figure, and our fourth defines the Fibonacci dataset size by using this number. Due to the recursive nature of Fibonacci series our first two entries bb[1] and bb[2] must be defined as 1.

To repeatedly add the values of preceding cells to the next cell, we need to define a loop which creates variable ii, and for values 3 to n (the size of our col(A) data) sets each cell equal to the sum of the two previous cells (-1 for one prior, -2 for two cells prior). This number is then returned as another figure in our set and the line repeats until n is reached.

This is one simple example of using LabTalk scripts to produce Fibonacci numbers. Next we’ll demonstrate how to find the “Golden Ratio” (a.k.a. phi φ) using a similar script based on this same series. The “Golden Ratio” (≈1.6180339…) is a recurring irrational number that occurs in nature as one of the most efficient means of placing objects around a single point, and is displayed in the geometry of many plants (including the sunflower, Helianthus sp.) that produce clusters of seeds and branches into arrays identical to Fibonacci values.

To generate the golden ratio from our Fibonacci sequence, we’ll need to create the following script in another column of the same worksheet-

Similarly to the first script, our col(C) f(x) box only contains a single line applying our function to the Fibonacci data. Then the first few lines in the Before Script Formula box set our data size based on the size of the Fibonacci set and set the first phi output to equal zero. The most remarkable difference comes in the loop line, which is set to divide worksheet values from 0 to n by the value found in the previous cell.


When we apply our script, we can see the ratio of these values very quickly converges within 5 decimal places of phi after a dozen iterations. As you can see from the graph below, when you plot these two series side by side, both take on their characteristic rates of change (or lack thereof in the case of phi) after only only a few iterations.0021

So now that we have an understanding of the behavior of Fibonacci sequences, let’s make a plot using phi to generate a set of integers. First let’s create a new worksheet(“Sunflower”) and generate the angle used to generate this spatial data by taking 360° and subtracting another 360° divided by phi (=360-(360÷ϕ)). By default Origin holds at least a dozen decimal places beyond what is displayed, so to keep these irrational figures as accurate as possible, it may be a good idea to run this basic calculation in Origin.

Once we’ve generated the resulting angle (≈137.50776…), we can begin to plot our sunflower graph. To plot this as a polar graph first we need to generate another 1000 integers using the data(1,1000) function. Then to place these points around the center using the “golden angle”, we need to take its value, multiply it by our set of integers after offsetting them by 1 (so the plot begins with θ=0, 137.50, 275.01, 412.52…etc). Next to plot out the distance from the origin of the graph we need to take the square route of these integers by typing in the f(x) box of column b, sqrt(col(B)). Make sure the theta column is set to x and the distances are set to y and then select  Plot>Specialized>Polar theta(X) r(Y) to plot our integers. The graph generated should appear as such–Sunflower_interm4
Let’s modify our graph symbology however to represent the distance (r) values that our data has from the origin point. To do this, double click on the graph to open Plot Details and in the Symbol tab change the size to col(D) or whichever column contained the square roots of the generated integers. Then to show the full range of these values graphically, let’s alter the scaling factor to somewhere between 0.3 and 0.4.
Then let’s make use of the color By Points feature and set the color scheme to map by Col(C), our theta values. Your sunflower graph should now appear as it does below.
For a greater choice of colors, click okay in plot dialog and, with our points selected, go to the Palettes button to select from any number of additional color schemes.

With some further formatting changes to our labels and axes, we should have a plot that looks much like this one. Note the similarities between this array and its analogue in nature-

Sunflower image courtesy of Wikipedia, CC-SA license.
A comparison of integers plotted using the Fibonacci golden ratio, and the seeds of a sunflower. Image courtesy of Nino Barbieri, Wikipedia, CC-SA 3.0 license.


Sunflowers, artichokes, cauliflower, and other common plants all use Fibonacci structuring because “[p]atterning seeds in spirals of Fibonacci numbers allows for the maximum number of seeds on a seed head, packed uniformly, with no crowding at the center and no ‘bald patches’ at the edges. In other words, the sunflower has found optimal space utilization for its seed head. The Fibonacci sequence works so well for the sunflower because of one key characteristic—growth… [t]hat is to say, as a seed head grows, seeds will always be packed uniformly, and with maximum compactness.” This is why this series appears so frequently not only in nature but in also in designs that seek to emulate this efficiency.


Addendum- Plotting on an X/Y Graph

Let’s say we’d like to convert our data to plot on a Cartesian coordinate plane. To do this, we’ll need to make two more columns in our worksheet. The first will be our x-coordinates defined as cos(radians(col(theta)))*col(r), and our y-coordinates as sin(radians(col(theta)))*col(r).

After plotting the returned values in a standard scatterplot you’ll notice that, although our axes are equidistant, our graph (on the right) is somewhat distorted.


Origin has a simple solution to correct this- to square the proportions of our data open the Plot Dialog, and with the Layer1 node selected, go to the Size/Speed tab and check off the tickbox labelled “Link Axis Length to Scale with X:Y”. leave the value as 1 and click OK.

Your graph should now appear much as it does below, and that’s how you convert display polar data on Cartesian coordinates.


Leave a Reply