Writing a function to obtain any element of Fibonacci sequence
What Will I Learn?
Greetings, in this tutorial we will focus on generating Fibonacci sequence on Octave.
- You will learn function writing in Octave,
- You will learn Fibonacci sequence and its properties,
- You will learn execution and debugging of the function via command window.
Requirements
- Octave 4.2
- Basic knowledge on coding
- Basic knowledge on sequences
- Information about Fibonacci sequence
Difficulty
- Intermediate
Tutorial Contents
To begin with Fibonacci sequence is a set of natural numbers having property of each element is equal to the sum of previous to elements. So for example the fourth element will be the sum of second and third one. In this task we will use a software capable of generating this sequence with a user defined input of desired last term. As a definiton the zeroth of the sequence is zero and the first element is one then the second element is actually the sum first and zeroth. Thus the sequence continues like the sum of k-2 and k-1. (where k is the element which we want to find)
Initially to form the sequence on Octave we should use function. Simply from editor menu click file and then new function. It will ask you the name of the function. For this tutorial the name was picked as 'fibonacci' and below code was written,
function [retval] = fibonacci (x)
The above code is needed while generating any functions. It is declearing the name of the function, the type (in this case it is retval which will return a value) and the variable x that can be used in calculations.
Then to define the variables and their behaviours we need to ask the user how many Fibonacci number he wants. By implementing the below code this was done,
N=input("Till which element you want the series go?\n");
Now the sequence needed to be defined so that Octave can calculate the elements. To do that first two elements of the series were defined,
fib=zeros(1,N);
fib(1)=1;
fib(2)=1;
k=3;
The above code is used to identify how many elements of Fibonnaci sequence does the uesr wants and the very first 2 elements of the sequence was defined. Later according to the N we must calculate other elements of the sequence. To do that below code was implemented to the function,
while k <= N
fib(k)=fib(k-2)+fib(k-1);
k=k+1;
endwhile
This will generate the desired Fibonnaci sequence elements by summing the two previous elements. fib(k-1) shows the previous element fib(k-2) shows the two elements before the desired, calculated element. So for instance if the user wants to list 5 terms of the sequence, the function must calculate all five elements. Since the first element of sequence is usually assumed zero and second is one third will the sum of the past two ones or in other words third element will be one too. Fourth will be the sum of third and second element resulting 2 and the fifth element will be 3 respectively. The above code actually calculates the overall sequence but for some execptions we must manually define the outcome if the user wants to see the zero, one or two terms. To do that below code was written to display only one two or three sequence elements if the user enters 0,1 or 2.
if (N>1)
fprintf("The Fibonacci sequence to %d term is\n",N);
fprintf('0 ');
fprintf('%g ',fib);
fprintf('\n');
elseif (N>0)
fprintf("The Fibonacci sequence to %d term is\n",N);
fprintf("0 1\n");
elseif (N>-1)
fprintf("The Fibonacci sequence to %d term is\n",N);
fprintf("0\n");
endif
The above code will take placce for the special cases where user enters 0,1 or 2. If he wants to see 0 terms the output will be 0, for 1 term it will be 1 1 and for 2 terms it will be 0 1 1.
Below is the full code that has been used to obtain Fibonnaci sequence,
function [retval] = fibonacci (x)
N=input("Till which element you want the series go?\n");
fib=zeros(1,N);
fib(1)=1;
fib(2)=1;
k=3;
while k <= N
fib(k)=fib(k-2)+fib(k-1);
k=k+1;
endwhile
if (N>1)
fprintf("The Fibonacci sequence to %d term is\n",N);
fprintf('0 ');
fprintf('%g ',fib);
fprintf('\n');
elseif (N>0)
fprintf("The Fibonacci sequence to %d term is\n",N);
fprintf("0 1\n");
elseif (N>-1)
fprintf("The Fibonacci sequence to %d term is\n",N);
fprintf("0\n");
endif
endfunction
Then to test the function simply run it by using save file and run button.
The Fibonacci sequence to 0 term is
The Fibonacci sequence to 10 term is
The Fibonacci sequence to 100 term is
Note that for long sequences to gain from space Octave uses the letter e denoting ten to the power. For example e+006 means 10^(6).
Curriculum
- Advanced drawing with Octave
- Two and three dimensional plotting with 'Octave'
- Solve and plot your equations with Octave!
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you @wodsuz for the contribution has been approved.
Note
You can contact us on Discord.
[utopian-moderator]
Hey @wodsuz I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x