Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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

Antworten (1)

Jan
Jan am 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?

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by