# Compute the first 10 Fibonacci numbers

16 visualizzazioni (ultimi 30 giorni)
Karl-Axel Ruuth il 13 Ott 2017
Risposto: James Tursa il 13 Ott 2017
Hi, I need to write a code in matlab that compute the first 10 Fibonacci numbers
But I am having some trouble with this. I thought of using the formula defined here:
https://www.math.hmc.edu/funfacts/ffiles/10002.4-5.shtml
And I've gotten this so far:
n = 0;
c = (((1+sqrt(5))/2)^n -((1-sqrt(5))/2)^2)/(sqrt(5));
while (n < 10)
disp(c)
n+1;
end
But as you can probably see, it is very wrong. But I'm not sure what to do else. The professor wants us to write a proper code, meaning I can't use things like fibonacci(n). Any help would be appreciated :)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

Ahmos Sansom il 13 Ott 2017
Modificato: Ahmos Sansom il 13 Ott 2017
Hey,
I wrote a simple function attached and run with the loop listed below based on your webpage link.
Thanks
Ahmos
for i=0:10
BinetFibonacci(i)
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

James Tursa il 13 Ott 2017
I'm guessing your professor expects you to implement the algorithm that he/she has given you. That is, the next number is the sum of the previous two numbers. So, here is an outline:
F = zeros(10,1); % pre-allocate the result to hold 10 numbers
F(1) = 1; % the first number (MATLAB indexing always starts at 1)
F(2) = 1; % the second number
for n=3:10 % loop to find the 3rd - 10th numbers
% You insert code here to compute F(n)
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su MATLAB 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!

Translated by