help-octave
[Top][All Lists]

## continued fraction expansion

 From: Bart Vandewoestyne Subject: continued fraction expansion Date: Thu, 27 May 2004 03:46:04 -0500 User-agent: Mutt/1.3.28i

```Hello all,

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:

>> x = rand

x =

0.6068

>> rat(x)

ans =

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

>> rat(x, 0.0000001)

ans =

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

The problem is that I don't need the result as a string, rather i need
it as a vector, so in the above two cases this would be something like:

>> rat(x)

ans =

[1 -3 2 5 4 14]

>> rat(x, 0.0000001)

ans =

[1 -3 2 5 4 14 2]

Does anybody know of an implementation for this?  Or will I have to look
at how the Matlab script builds the string and try to adapt it to my own
needs?

Thanks,
Bart

PS: the algorithm that calculates the continued fraction expansion up to
a certain number n of digits is easy and can be found at
http://mathworld.wolfram.com/ContinuedFraction.html.  I've implemented
it as:

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...

--
!!!!!!!!!!!!!!!!!!! email change !!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

-------------------------------------------------------------
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
-------------------------------------------------------------

```