help-octave
[Top][All Lists]

## Re: continued fraction expansion

 From: Przemek Klosowski Subject: Re: continued fraction expansion Date: Thu, 27 May 2004 12:27:35 -0400 (EDT)

```   During some experimental work, i came across a little problem where
I would need to get the digits of a continued fraction expansion of
a real x, up to a certain tolerance.  In Matlab, i can do this as:

>> rat(x, 0.0000001)

ans =  1 + 1/(-3 + 1/(2 + 1/(5 + 1/(4 + 1/(14 + 1/(2))))))

[...]

function a = continued_fraction(x, n)

a = zeros(n,1);

r = x;
a(1) = floor(x);
for i=2:n,
r = 1./(r-a(i-1));
a(i) = floor(r);
end

My problem is that i don't want to be able to calculate for n digits,
but i want to calculate up to a certain *tolerance* like in the Matlab
rat command...

Matlab's tolerance is expressed as |a-x| < tolerance * |x| whereas
your function, on a quick once-over, has the error of better than
10^-(n-1). In other words, n=1-log10(tolerance*abs(x)). Or something
like that.

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```