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

1 次查看(过去 30 天)
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

回答(1 个)

Jan
Jan 2017-11-30
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?

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by