Fibonacci Using While Loop
Mostra commenti meno recenti
I want to produce the nth term of the Fibonacci Sequence, however I keep getting the error, "Attempted to access fibonacci(4); index out of bounds because numel(fibonacci)=3." So far I have the following code:
function f = fibonacci(n)
fibonacci(1) = 1;
fibonacci(2) = 1;
fibonacci(3) = 2;
i = 3;
n = 1000000000000;
while (i <= n)
f = fibonacci(i-1) + fibonacci(i-2);
i = i + 1;
end
end
1 Commento
LUIGI ALESSANDER HUAMAN TORRES
il 25 Ott 2020
function f = fibonacci(nmax)
f=zeros(nmax,1);
f(1) = 0;
f(2) = 1;
f(3) = 1;
i = 3;
while (i <= nmax)
f(i) =f(i-1) + f(i-2);
i = i + 1;
end
end
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!