I'm trying to write a Matlab function that computes how many terms, m, it takes the golden fraction to get to n digits of accuracy. Here is what I have so far, but I keep getting an output of 0.
phi = (1+sqrt(5))/2;
p=1;
p=[1+1/p];
LoopCounter = 0;
while (phi-p)>10^(-n)
p=[1+1/p];
LoopCounter = LoopCounter + 1;
end
m=LoopCounter;
m
I think this is a common question for those studying Number Theory or just starting to learn Matlab. Any advice? Thanks!
The while
condition is missing an abs
. It should be
while abs(phi-p)>10^(-n)
With your code as it stands, the initial value of p
(namely 2) is greater than phi
, so phi-p
is negative and the while
loop is never entered. That's why you get m
equal to 0
.