Error in an algorithm for continued fractions
Afficher commentaires plus anciens
Hello everyone,
I use Matlab R2020a to create an algorithm that gives all the digits of the continued fraction expansion of a real number (I know there is a function that does it more or less, but I haven't been able to use it).
function A=frac_continu(r,nmax)
% r is a real number
% nmax is the "limit rank"
A=zeros(1,nmax);
j=1;
alpha=r;
while j<= nmax && alpha-floor(alpha) ~= 0
A(j)=floor(alpha);
alpha=1/(alpha-A(j));
j=j+1;
end
end
What bothers me is that when I apply it to different real numbers, I don't get the right results from a certain rank.
For example, if I take the golden ratio (i.e. r = (1+sqrt(5))/2 and nmax=50), I should only get 1's, but the outgoing vector eventually gives me two 2's, etc.
So I wanted to know if the problem is algorithmic or if it's just due to a rounding problem or something else.
Thanks in advance!
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!