lettura simple

How to approximate a real number using a sum of fractions in Octave

In this lesson I'll show you how to write a real number via a rational expression in Octave using the rat() function.

rat(n)

The rational approximation writes the real number n as a sum of fractions.

  • If the real number is rational, then the sum of fractions is equal to the real number.
  • If the real number is irrational, adding fractions only provides an algebraic approximation of the real number.

What is the difference between rational and irrational real numbers? Real numbers are positive and negative numbers with commas. The set of real numbers is divided into rational and irrational numbers. Rational numbers are numbers that can be written as the ratio of two integers m/q $$ n = \frac{m}{q} $$ Irrational numbers, on the other hand, cannot be written in the m/q form

I'll give you a practical example

To approximate the real number 1.2 type

>> rat(1.2)

Octave approximates the number with a sum of fractions

ans = 1 + 1/5

In this case the sum of fractions coincides exactly with the real number because 1.2 is a rational number.

Note. The number 1.2 is a rational number because you can write it as the ratio of two integers. $$ 1.2 = \frac{12}{10}$$ So you can also write it in the form $$ 1.2 = 1 + \frac{1}{5} = 1 + 0.2 $$

Now try to approximate the real number pi 3.1416

>> rat(3.1416)

Octave approximates the number by a sum of fractions

ans = 3 + 1/(7 + 1/(16 + 1/11))

In this case it is a rational approximation because the number pi is an irrational number.

How to enable rational approximation for all results

If you want to have the rational approximation for all the results of the current session type format rat

>> format rat

After typing this command, Octave displays all results by default using rational approximation. This way you don't have to use the rat() function every time.

For example, if you type 3.3 it automatically turns it into 33/10

>> 3.3

ans = 33/10

Rational approximation remains active throughout the current session.

If you want to go back to the normal display format type the command format.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin