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
whilecondition is missing anabs. It should beWith your code as it stands, the initial value of
p(namely 2) is greater thanphi, sophi-pis negative and thewhileloop is never entered. That's why you getmequal to0.