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

Steven Lord
Steven Lord 2018년 5월 1일
This sounds like a homework assignment. If it is, show what you've written to try to solve the problem and ask a question about the specific issue that's blocking you from moving forward and we may be able to offer some guidance.
dominic blackler
dominic blackler 2018년 5월 1일
편집: James Tursa 2018년 5월 1일
this is what I have created so far. I just don't quite get the desired results for the for loop function
clear, clc
x = 'Input first term of the Fibonacci sequence: ';
input_first_term = input(x);
y = 'Input second term of the Fibonacci sequence: ';
input_second_term = input(y);
s=zeros(1,10)
s(1)=input_first_term;
s(2)=input_second_term;
for i=3:10
s(i)=s(i-2)+(i-1);
end
disp(s)
Muhammed Roshdy
Muhammed Roshdy 2019년 4월 17일
편집: Muhammed Roshdy 2019년 4월 17일
% 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))

댓글을 달려면 로그인하십시오.

 채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 1일
편집: Ameer Hamza 2018년 5월 1일

1 개 추천

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

dominic blackler
dominic blackler 2018년 5월 1일
편집: James Tursa 2018년 5월 1일
thanks I have amended the script. My next problem is 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.
I have inputted this script now but it just doesn't seem right from the graph.
clear, clc
x = 'Input first term of the Fibonacci sequence: ';
input_first_term = input(x);
y = 'Input second term of the Fibonacci sequence: ';
input_second_term = input(y);
s(1)=input_first_term;
s(2)=input_second_term;
for i=3:10
s(i)=s(i-2)+s(i-1);
end
disp(s)
d=(1:10);
polarplot(s,d)
title('Fibonacci sequence 6.6')
James Tursa
James Tursa 2018년 5월 1일
@dominic: You don't need to keep adding blank lines in your code to format it. Simply select the code and then push the "{ } Code" button.
John D'Errico
John D'Errico 2018년 5월 1일
편집: John D'Errico 2018년 5월 1일
@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. :)
dominic blackler
dominic blackler 2018년 5월 1일
thanks you

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Tasbiha Azeemi
Tasbiha Azeemi 2018년 5월 14일

0 개 추천

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
Nwajiobi Chibueze 2020년 2월 16일
편집: Nwajiobi Chibueze 2020년 2월 16일

0 개 추천

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)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 5월 1일

댓글:

2021년 1월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by