[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: continued fraction expansion
From: |
Henry F. Mollet |
Subject: |
Re: continued fraction expansion |
Date: |
Thu, 27 May 2004 18:05:00 -0700 |
User-agent: |
Microsoft-Entourage/10.1.1.2418 |
Thanks for explaining what the problem was after I've downloaded rat.m for
practice and tried it.
Henry
octave:6> x=rand
x = 0.31647
octave:7> rat(x)
usage: [n,d] = rat(x,tol)
error: evaluating if command near line 33, column 3
error: called from `rat' in file `/Users/mollet/CandO/rat.m'
octave:7> [n,d]=rat(x)
n = 632
d = 1997
octave:8> [n,d]=rat(x, 0.0000001)
n = 632
d = 1997
on 5/27/04 4:42 PM, Paul Kienzle at address@hidden wrote:
> 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
> address@hidden
>
> 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 !!!!!!!!!!!!!!!!!!!!
>> My email address is now address@hidden
>> Please update your addressbook!
>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>>
>>
>>
>> -------------------------------------------------------------
>> 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
> -------------------------------------------------------------
-------------------------------------------------------------
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
-------------------------------------------------------------