Rational fraction approximation - MATLAB rat (2024)

Rational fraction approximation

collapse all in page

Syntax

R = rat(X)

R = rat(X,tol)

[N,D] =rat(___)

Description

example

R = rat(X) returnsthe rational fraction approximation of X towithin the default tolerance, 1e-6*norm(X(:),1).The approximation is a character array containing the truncated continuedfractional expansion.

example

R = rat(X,tol) approximates X towithin the tolerance, tol.

example

[N,D] =rat(___) returns two arrays, N and D,such that N./D approximates X,using any of the above syntaxes.

Examples

collapse all

Approximate Value of π

Open Live Script

Approximate the value of π using a rational representation of the quantity pi.

The mathematical quantity π is not a rational number, but the quantity pi that approximates it is a rational number since all floating-point numbers are rational.

Find the rational representation of pi.

ans = 355/113 

The resulting expression is a character vector. You also can use rats(pi) to get the same answer.

Use rat to see the continued fractional expansion of pi.

R = rat(pi)
R = '3 + 1/(7 + 1/(16))'

The result is an approximation by continued fractional expansion. If you consider the first two terms of the expansion, you get the approximation 3+17=227, which only agrees with pi to 2 decimals.

However, if you consider all three terms printed by rat, you can recover the value 355/113, which agrees with pi to 6 decimals.

3+17+116=355113

Specify a tolerance for additional accuracy in the approximation.

R = rat(pi,1e-7)
R = '3 + 1/(7 + 1/(16 + 1/(-294)))'

The resulting approximation, 104348/33215, agrees with pi to 9 decimals.

Express Array Elements as Ratios

Open Live Script

Create a 4-by-4 matrix.

format short;X = hilb(4)
X = 4×4 1.0000 0.5000 0.3333 0.2500 0.5000 0.3333 0.2500 0.2000 0.3333 0.2500 0.2000 0.1667 0.2500 0.2000 0.1667 0.1429

Express the elements of X as ratios of small integers using rat.

[N,D] = rat(X)
N = 4×4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
D = 4×4 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7

The two matrices, N and D, approximate X with N./D.

View the elements of X as ratios using format rational.

format rationalX
X = 1 1/2 1/3 1/4 1/2 1/3 1/4 1/5 1/3 1/4 1/5 1/6 1/4 1/5 1/6 1/7 

In this form, it is clear that N contains the numerators of each fraction and D contains the denominators.

Input Arguments

collapse all

XInput array
numeric array

Input array, specified as a numeric array of class single or double.

Data Types: single | double
Complex Number Support: Yes

tolTolerance
scalar

Tolerance, specified as a scalar. N and D approximate X, such that abs(N./D - X) <= tol. The default tolerance is 1e-6*norm(X(:),1).

Output Arguments

collapse all

R — Continued fraction
character array

Continued fraction, returned as a character array with m rows,where m is the number of elements in X.The accuracy of the rational approximation via continued fractionsincreases with the number of terms.

N — Numerator
numeric array

Numerator, returned as a numeric array. N./D approximates X.

D — Denominator
numeric array

Denominator, returned as a numeric array. N./D approximates X.

Algorithms

Even though all floating-point numbers are rational numbers,it is sometimes desirable to approximate them by simple rational numbers,which are fractions whose numerator and denominator are small integers.Rational approximations are generated by truncating continued fractionexpansions.

The rat function approximates each elementof X by a continued fraction of the form

ND=D1+1D2+1+1Dk.

The Ds are obtained by repeatedly pickingoff the integer part and then taking the reciprocal of the fractionalpart. The accuracy of the approximation increases exponentially withthe number of terms and is worst when X = sqrt(2).For X = sqrt(2) , the error with k termsis about 2.68*(.173)^k, so each additional termincreases the accuracy by less than one decimal digit. It takes 21terms to get full floating-point accuracy.

Extended Capabilities

Version History

Introduced before R2006a

See Also

rats | format

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Rational fraction approximation - MATLAB rat (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Rational fraction approximation - MATLAB rat (2024)
Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6446

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.