Effacer les filtres
Effacer les filtres

Trying produce input to using while loops to create fibonacci sequence but i have done using the while and it is error.

1 vue (au cours des 30 derniers jours)
n=input('Enter the number that should not exceed:');
fibonacci(1)=input('Enter the first number:');
fibonacci(2)=input('Enter the second number:');
f(1) = 1;
f(2) = 1;
for i = 3 : 30
% SUM
f(i) = f(i-1) + f(i-2);
golden_ratio = f(i)/f(i-1);
str = [num2str(f(i)) ' ' num2str(f(i-1)) ' ' ...
num2str(golden_ratio, 10)];
disp(str)
end

Réponses (1)

Jan
Jan le 30 Nov 2017
What about this:
lim = 10000;
f(1) = 1;
f(2) = 1;
for i = 3 : 30
f(i) = f(i-1) + f(i-2);
golden_ratio = f(i) / f(i-1);
fprintf('%d: %d %d %.16g\n', i, f(i), f(i-1), golden_ratio);
if f(i) > lim
break; % Stop the "for i" loop
end
end
Or maybe the limits concerns the value of golden_ratio?

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by