For loop fibonacci sequence
8 views (last 30 days)
Show older comments
dominic blackler
on 1 May 2018
Commented: Milan Parikh
on 19 Jan 2021
I need to create a Fibonacci sequence using the for loop function. the first two number of the Fibonacci sequence must be defined from a user input.
I then need to plot this on a polar graph with the element number as the angle and value of the element in the sequence for the radius
3 Comments
Muhammed Roshdy
on 17 Apr 2019
Edited: Muhammed Roshdy
on 17 Apr 2019
% Fibonacci sequence
clear;clc;
sequence_end=25; %Insert the Sequence end here
F_curr=zeros(1,sequence_end);
golden_ratio=zeros(1,sequence_end-1);
F_curr(1) = 0; %Insert First Value of the sequence here
F_curr(2) = 1; %Insert second Value of the sequence here
i=3;
golden_ratio(1)=F_curr(2)/F_curr(1);
for n=3:sequence_end
F_curr(i)=F_curr(i-1)+ F_curr(i-2);
golden_ratio(i-1)=F_curr(i)./F_curr(i-1);
i=i+1;
end
disp(F_curr)
disp(golden_ratio(end))
Accepted Answer
Ameer Hamza
on 1 May 2018
Edited: Ameer Hamza
on 1 May 2018
In the code you shared, you made a mistake in the line inside for loop. Despite that, your logic is correct. Correct it as follow,
s(i)=s(i-2)+s(i-1); % you missed second s
4 Comments
John D'Errico
on 1 May 2018
Edited: John D'Errico
on 1 May 2018
@dominic: READ THE HELP FOR POLAR PLOT! Before you use any function, read the help. Make sure you understand what the arguments are intended to mean.
polarplot(TH,R) plots vector TH vs R. The values in TH
are in radians.
Really, you were so close.
You were asked to plot: "with the element number as the angle and value of the element in the sequence for the radius"
What did you do?
polarplot(s,d)
You passed in the arguments in the wrong order.
You were told to use the element number as the angle (in radians). So do this:
polarplot(d,s)
So close, yet so far. :)
More Answers (2)
Tasbiha Azeemi
on 14 May 2018
Try this one!!!
function a=FabonacciSequence(n)
a(1)=0;
a(2)=1;
for i=1:n;
a(i+2)=a(i+1)+a(i)
end
end
Nwajiobi Chibueze
on 16 Feb 2020
Edited: Nwajiobi Chibueze
on 16 Feb 2020
x = input('enter the first and second number of the sequence:')
y = input('enter the number of elements in the sequence:')
for k = 3:y
n(1) = x;
n(2) = x;
n(k) = n(k-1)+n(k-2);
end
n
polar(n)
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!