Fibonacci Series | MATLAB Tutorial
by SAURAB S
Fibonacci sequence in MATLAB
The Fibonacci sequence is a set of numbers that start with a one or a zero, followed by a one, and proceeds based on the rule that each number is equal to the sum of the preceding two numbers.
Example :- 0 , 1 , 1 , 2 , 3 , 5 , 8 ,13 , 21 …..
We can generate a series of Fibonacci numbers using MATLAB.
The algorithm for it is very simple.
- We initialize two variables as 0 and 1. This is because a series always starts with 0 and a 1.
- After this, we take an input from user where the user has to enter the no. of terms he wants in the series.
- Accordingly, a for loop is executed where it prints all the terms of the sequence that the user wants and exits the loop.
Following is the Matlab code for the Fibonacci sequence.
a=0; b=1; n=input('Enter no. of terms : '); for i=1:n fprintf('%d',a) ; fprintf('\t'); c=a+b; a=b; b=c; end fprintf('\n')
When I enter number of terms:- 2
When I enter number of terms:- 3
When I enter number of terms:- 6