Finding the nth term of the fibonacci sequence in matlab.
9 次查看(过去 30 天)
显示 更早的评论
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
回答(1 个)
Star Strider
2018-10-28
You have the correct approach.
Try this:
n = input("Enter value of n ");
f = fib1(n)
When this is then run, the result is:
Enter value of n 10
f =
89
2 个评论
Star Strider
2018-10-28
You must save your function in a separate file called ‘fib1.m’.
Then my Answer will work.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Encryption / Cryptography 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!