Discussion Room : IDC101

Best rational approximation to pi

Best rational approximation to pi

by Kapil Paranjape -
Number of replies: 7

Given a number d, find a fraction of the form p/q with q at most equal to d which is closest to π.

For example 22/7 is the closest with d=10.

Here, we define π as the smallest positive number such that sin(π)=0. Moreover, we can assume that we already know that π lies between 3 and 4.

Hint: Near π the sine function decreases linearly. Hence, the value of |sin(p/q)| can be taken to be a measure of how close p/q is to π when p/q is near π.


In reply to Kapil Paranjape

Re: Best rational approximation to pi

by Kapil Paranjape -

Note that it is good to think of a method that does not use any other aspect of \pi. This will allow you to adapt your method to find the nearest rational number to a positive root of (say) x^5-3x-7=0, by a small modification of your program.

In reply to Kapil Paranjape

Re: Best rational approximation to pi

by Suroj Dey -

Using Sin(pi)=0 as our definition of pi , and solving the taylor approximation of sin(x), using the Newton-Raphson algorithm, will probably give us the answer. 

In reply to Suroj Dey

Re: Best rational approximation to pi

by Dr. Amit Kulshrestha -

Suroj, just carefully look at the hint given by Professor Paranjape. He has essentially given you the algorithm. Nothing like Newton-Raphson and Taylor series should be needed for it. Essence of the hint lies in understanding the behaviour of sin in the vicinity of   \pi  .

Why doesn't someone write a Python code for it?



In reply to Kapil Paranjape

Re: Best rational approximation to pi

by Dr. Amit Kulshrestha -
I am bit surprised (and unhappy) that no one has shared a code for this wonderful exercise. I am sharing my code, with further addition to the discussion.

import math
d = 10000
err = abs(math.sin(3))
pgood = 3
qgood = 1
for q in range(1,d+1):
    for p in range(3*q,4*q+1):
        newerr = abs(math.sin(float(p)/float(q)))
        if newerr < err:
            err = newerr
            pgood = p
            qgood = q
print pgood,"/",qgood


Questions

  • What modifications are required if we define   \pi  as the smallest real number  x > 0 for which  {\rm tan}(x) = 0 ?
  • What modifications are required if we define   \pi  as the smallest real number  x > 0 for which  {\rm cos}(x/2) = 0 ?
  • Which rational approximation of   \pi  would you trust most? The one using sine, tangent or cosine function?

Keep \pi  -thoning!

In reply to Dr. Amit Kulshrestha

Re: Best rational approximation to pi

by Gariman Singh -

Isn't this better:


import math

d=input("Enter the maximum value of d:")

min=math.pi-3

pc=3

qc=1

for q in range(1,d+1):

    for p in range(3*q,4*q):

        l=abs(math.pi-float(p)/q)

        if l<min:

            min=l

            pc=p

            qc=q

print str(pc)+"/"+str(qc)

In reply to Dr. Amit Kulshrestha

Re: Best rational approximation to pi

by Ayushman Srivastava . -

Is the answer cos(x/2)?

I think because the slope is smaller in magnitude (i.e. 1/2) around x = pi for y = cos(x/2), but it is 1 for y = sin(x) or y = tan(x), so the function changes slowly in the first case and there is less scope for error.