0

I am attempting to write a program that takes a user's input (n) and outputs the nth term of the Fibonacci sequence, without using any of MATLAB's inbuilt functions. I have currently written the following function, however, I wish to alter this code slightly so that n=input("Enter value of n") however I am unsure how to go about this? Do I need to declare an empty array called fib1?

function f = fib1(n)
    if n <= 1
        f = 1;
    else
        f = fib1(n-1) + fib1(n-2);
    end
end
Banghua Zhao
  • 1,518
  • 1
  • 14
  • 23
compscistudent
  • 107
  • 1
  • 2
  • 11

5 Answers5

3

The Fibonacci sequence is defined by a difference equation, which is equivalent to a recursive discrete-time filter:

>> n = 10;
>> result = [1 filter([1 1], [1 -1 -1], [1 zeros(1,n-2)])]
result =
     1     1     2     3     5     8    13    21    34    55
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
2

You can easily modify your function by first querying the actual amount of input arguments (nargin), and handling the two cases seperately:

function f = fib1(n)
if nargin<1
 n = input('input n:');
 f = fib1(n);
else
 if n <= 1
  f = 1;
 else
  f = fib1(n-1) + fib1(n-2);
 end
end
end

Try it online!

flawr
  • 10,814
  • 3
  • 41
  • 71
2

A better way is to put your function in a separate fib.m file, and call it from another file like this:

n = input("Enter value of n")  
result = fib(n)

also, you can improve your Fibonacci code performance likes the following:

function f = fib(n)
res = ones(1, n + 1);
for i = 3:(n + 1)
     res(i) = res(i - 1) + res(i - 2)
end
f = res(n + 1)
OmG
  • 18,337
  • 10
  • 57
  • 90
2

Just thought I would add this in here...

It is possible to find the nth term of the Fibonacci sequence without using recursion. (A closed form solution exists.) I'm not necessarily expecting this answer to be accepted but just wanted to show it is possible to find the nth term of Fibonacci sequence without using recursion.

Try this function. I made this a long time ago. I also added some code to round the output to the nearest integer if the input is an integer. I found this is necessary because sometimes the rounding causes the closed form solution to not produce an integer due to the computer rounding the irrational numbers.

function x = fib(n)
%FIB Fibonacci sequence.
%   X = FIB(N) returns the Nth term in the Fibonacci sequence, which is
%   defined in the following way:
%
%   FIB(N+2) = FIB(N+1) + FIB(N) , FIB(0) = 0 , FIB(1) = 1
%
%   The closed form solution to the Fibonacci sequence is:
%
%                            N                   N
%             / 1 + SQRT(5) \     / 1 - SQRT(5) \
%             | ----------- |  -  | ----------- |
%   FIB(N) =  \      2      /     \      2      /
%             ------------------------------------
%                           SQRT(5)
%
%   Although this formula is only physically meaningful for N as an
%   integer, N can be any real or complex number.

r = sqrt(5);

x = (((1+r)/2).^n-((1-r)/2).^n)/r;

for l = numel(n)
    if isequal(mod(n(l),1),0)
        x(l) = round(x(l));
    end
end

end
ImaginaryHuman072889
  • 4,953
  • 7
  • 19
  • 51
  • 1
    Satisfying to see the golden ratio come up on SO :) – jodag Nov 01 '18 at 16:59
  • @jodag Ha, yea I guess it is somewhat rare for it to come up in a programming context. Only times I can imagine you would see it is for Fibonacci sequence, or possibly making a natural "flower petal" pattern. – ImaginaryHuman072889 Nov 01 '18 at 19:24
1

You can define a function which takes n=input("Enter value of n");. Then let the calculation of nth term of the Fibonacci sequence f = fib2(n); inside that function.

function f = fib1()
    n = input('Enter value of n: '); 
    f = fib2(n);
    function f = fib2(n)
        if n <= 1
            f = 1;
        else
            f = fib2(n-1) + fib2(n-2);
        end
    end
end
Banghua Zhao
  • 1,518
  • 1
  • 14
  • 23