lettura simple

Rational Approximation in Matlab

In this lesson, I'll be going over how rational approximation functions and its applications in Matlab.

What is rational approximation? It's a numerical format that allows you to display real numbers as fractions or as a sum of fractions (rational expression).

To use rational approximation, type the rat() command.

rat(n)

Where the parameter n is a real numeric value.

How does it work?

Rational approximation transforms a real number n into a sum of fractions.

  • If the real number (n) is rational, the sum of fractions is equal to the real number.
  • If the real number (n) is irrational, the sum of fractions is an approximation of the real number.

What's the difference between rational and irrational real numbers? Real numbers are positive and negative numbers with or without a decimal point. For example, 3.3, 4.138, 5, etc. The set of real numbers is divided into rational and irrational numbers. Rational numbers are real numbers that can be obtained as a ratio of two integers m/q $$ n = \frac{m}{q} $$ For example, the number 2.5 is a rational number because you can obtain it with the fraction 5/2. Irrational numbers, on the other hand, are all real numbers that cannot be obtained by the ratio of two integers m/q. For example, the value of pi (π) is an irrational number.

Here's a practical example.

Approximate the real number with the function rat(1.2)

>> rat(1.2)

Matlab approximates the number and writes it as a sum of fractions

ans = 1 + 1/5

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

Note. The number 1.2 can be expressed as a ratio of two integers, which makes it a rational number. Specifically, it can be written as 1.2 $$ 1.2 = \frac{12}{10}$$ Alternatively, it can also be written as 1+1/5 $$ 1.2 = 1 + \frac{1}{5} = 1 + 0.2 $$

Now let's approximate the real number pi, which is approximately equal to 3.1416.

To do this in Matlab, you can use the "rat" function as follows:

>> rat(3.1416)

Matlab will provide an approximation of the real number as a sum of fractions.

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

In this case, the output result is a rational approximation because pi is an irrational number.

You cannot obtain the value of pi by the ratio of two integers.

How do you activate the default rational approximation?

If you want to activate the default rational approximation for all results in the current session, type the command format rat

>> format rat

This will display all results by default using rational approximation, even without using the "rat()" function.

For example:

>> 3.3

ans = 33/10

To return to the normal format, type the command format.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin