Reputation: 127
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
Upvotes: 0
Views: 1814
Reputation: 112699
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
Upvotes: 3
Reputation: 5195
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
Upvotes: 2
Reputation: 18838
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)
Upvotes: 2
Reputation: 1556
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
Upvotes: 1
Reputation: 11628
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
Upvotes: 2