Fibonacci Sequence and Caluclating a phi value
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuesta aceptada
bym
el 20 de Oct. de 2012
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 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!