Finding the nth term of the fibonacci sequence in matlab.
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
Risposte (1)
Star Strider
il 28 Ott 2018
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 Commenti
Star Strider
il 28 Ott 2018
You must save your function in a separate file called ‘fib1.m’.
Then my Answer will work.
Vedere anche
Categorie
Scopri di più su Encryption / Cryptography in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!