Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Answers (1)

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

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!