help-octave
[Top][All Lists]

Re: continued fraction expansion

 From: Paul Kienzle Subject: Re: continued fraction expansion Date: Thu, 27 May 2004 19:42:59 -0400

```octave-forge (http://octave.sf.net) has rat.  It doesn't print out the
continued fraction, but only returns the fractional form.

Please patch it so that it prints out continued fraction if no
output arguments are requested.  I would suggest
returning the continued fraction vector (or matrix if
multiple values) if one output argument is requested.

That behaviour is incompatible (though not with the
documentation), but seems more useful than returning
a string.

Other options are creating yet another function (ratv?)
or adding another argument 'vector'  if you want to
return the vector rather than a string.  None of these
appeal to me much.

Paul Kienzle

On May 27, 2004, at 4:46 AM, Bart Vandewoestyne wrote:

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

```
```

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

```