Scilab, Xcos and Fibonacci

In this tutorial we are going to create a Scilab function (algorithm) which will generate the Fibonacci sequence of numbers. The same algorithm is going to be duplicated in Xcos as a block diagram model. In the end, we’ll compare the results to confirm that the algorithm’s implementation is correct.

The Fibonacci numbers (also called Fibonacci sequence) are mainly used in mathematics. The sequence of numbers are characterized by the fact that every number, after the first two, is equal with the sum of the two preceding numbers.

Fibonacci spiral

Image: Fibonacci spiral

For example, the first ten Fibonacci numbers are the following:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

The first two numbers (1, 1) are called the seed values and they are fixed.

\[ \begin{split}
\end{split} \]

Starting with the third number, the Fibonacci numbers are generated by the following recurrence relation:

\[\bbox[#FFFF9D]{F_n = F_{n-1}+F_{n-2}}\]

As stated in the definition, the third number (2) is the sum of the first two numbers (1+1), the fourth number (3) is the sum of the two preceding numbers (2+1), the fifth number (5) is the sum of the two preceding ones (2+3) and so on.

Another variant of the Fibonacci numbers is using the first seed value equal with zero. In this case:

\[ \begin{split}
\end{split} \]

and the first ten numbers in the series will be:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Let’s create a Scilab function which can generate N values of Fibonacci numbers. We’ll create a custom Scilab function called fibonacci(). The Scilab instructions of the function are the following:

function y=fibonacci(N)
    for i=3:N

Save the script file as fibonacci.sci and run it. The function should now be loaded in the Scilab environment and it’s ready to use. To display the first ten numbers of the Fibonacci series, execute in the Scilab console:

--> fibonacci(10)

ans =
0. 1. 1. 2. 3. 5. 8. 13. 21. 34.


We can also plot the Fibonacci series with the following Scilab instructions:

ylabel('Fibonacci numbers')

Executing the Scilab instructions will output the following plot:

Fibonacci numbers generated in Scilab

Image: Fibonacci numbers generated in Scilab

Next, we are going to generate the same sequence of Fibonacci numbers with an Xcos block diagram model. The construction of the model is very simple. It consists mainly in a time generator, two unit delay blocks and a summation (addition) block.

Fibonacci numbers Xcos block diagram model

Image: Fibonacci numbers Xcos block diagram model

The Clock has a Period of 1 and the Initialisation Time set to 0. This means that it will trigger the Unit Delay blocks every 1 second. The initial condition for the unit delay blocks are set as the seed values of the Fibonacci series (0 and 1).

The Scope block has the parameter Accept herited events set to 1, which means that it doesn’t need any trigger event and it will plot the discrete values as they come in.  Running the model for 10 seconds will plot the following graphical window.

Fibonacci numbers generated in Xcos

Image: Fibonacci numbers generated in Xcos

As you can see, the Xcos simulation outputs the same result as the Scilab function.

For any questions, observations and queries regarding this article, use the comment form below.

Don’t forget to Like, Share and Subscribe!

One Response

  1. Hermes

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!