Fibonacci Sequence and Caluclating a phi value

조회 수: 1 (최근 30일)
Mark Grano
Mark Grano 2012년 10월 19일
I am having trouble with this program running properly. It generates the fibonacci sequence no problem but when i need to calulate the phi value it does nothing. or at least diplays nothing. any ideas why it is not displaying any value for phi or displaying the number of iterations required?
here is my code:
clear;clc;
num1=input('Enter an integer,greater than three, for the number of terms in the sequence: ');
if (num1<=3 || rem(num1,1)~=0)
num1=input('Enter an integer greater than three');
end
num2=input('Enter a integer from 2 to 15 for the precision: ');
if (num2<2 || rem(num2,1)~=0)
num2=input('Please enter a valid integer between 2 and 15');
end
Npts=num1; m=1; d0=0; d1=1;
while(m<=Npts)
db(m)=d0;
d2=d1+d0;
d0=d1;
d1=d2;
m=m+1;
end
disp(db);
iter=0;
Npt=1000; f=1; b0=0; b1=1;
while(f<=Npt)
bd(f)=b0;
b2=b1+b0;
b0=b1;
b1=b2;
f=f+1;
end
phi= abs((bd(2)/bd(1))-((bd(3)/bd(2))));
if(phi< (10^-num2))
disp(iter);
disp(phi);
else
t=4;
bd(1)=bd(2);
bd(2)=bd(3);
bd(3)=bd(t);
t=t+1;
end
iter=iter+1;
any input would be appreciated! Thanks in advance.

채택된 답변

bym
bym 2012년 10월 20일
close, I have tried to keep intact most of what you have done
clear;clc;
num1=input('Enter an integer,greater than three, for the number of terms in the sequence: ');
if (num1<=3 || rem(num1,1)~=0)
num1=input('Enter an integer greater than three');
end
num2=input('Enter a integer from 2 to 15 for the precision: ');
if (num2<2 || rem(num2,1)~=0)
num2=input('Please enter a valid integer between 2 and 15');
end
Npts=num1; m=1; d0=0; d1=1;
while(m<=Npts)
db(m)=d0;
d2=d1+d0;
d0=d1;
d1=d2;
m=m+1;
end
disp(db);
iter=0;
tol = 1;
%Npt=1000; f=1; b0=0; b1=1;
bd = db(end-3:end); %create bd
while tol > 10^-num2
% bd(f)=b0;
% b2=b1+b0;
% b0=b1;
% b1=b2;
% f=f+1; you have already created the sequence
tol = abs((bd(2)/bd(1))-((bd(3)/bd(2))));
% if(phi< (10^-num2))
% disp(iter);
% disp(phi);
% else
% t=4;
bd(1)=bd(2);
bd(2)=bd(3);
bd(3)=bd(1)+ bd(2);
% t=t+1;
% end
iter=iter+1;
end
fprintf('phi = %1.15f\n',bd(3)./bd(2))
fprintf('iterations = %d\n',iter)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Pole and Zero Locations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by