Aug 14, 2014

13. Calculating pi

We will calculate pi using one of its series approximation. We can only calculate upto 16-decimal positions with 64-bit double. This is done after about 25 terms. The user has to type the number of terms.




This is the arctan series. It converges for x less than 1. It converges at 1, but extremely slowly. The graph, or convergence, is shown in a later slide. As the angle x decreases, the rate of convergence increases rapidly. It is based on the geometric series, as the derivative can be written as an infinite series.




For angle of x = 1 radians (57.3 degrees), we have a slowly converging series. It is too slow to be useful.




Here, two small angles are chosen according to this right triangle. Both alpha and beta are smaller than 45 degrees (pi/4=0.785). In fact, the sum of alpha and beta is exactly 45 degrees.




These are equations to find the two angles, and the identity is shown here, as the last equation.




In the blue line, we can see the slow convergence for x = 1. The faster convergence is shown by red line, which is based on the angles atan(1/2) = 26.565 degrees, and atan(1/3) = 18.435 degrees. As angles decrease, we have even faster convergence, that is, it will go to machine limit, at smaller N.




This shows the recursive equations in for loop. The program prompts for N, corresponding to the number of terms. Should the user type a letter or anything which is not-a-number, an Exception is raised and a default value of N used.


/*
 * Using identity: arctan(1/2)+arctan(1/3) = pi/4
 * As angles become smaller, the seies converge at 
 * faster rate. Many better approximations
 * with smaller angles exist.
 * arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - x^11/11 + ...
 * = x - x^2(x/3 - x^3/5 + x^5/7 - x^7/9 + x^9/11)
 * = x - x^2(x/3 - x^2(x/5 - x^3/7 + x^5/9 - x^7/11))
 * = x - x^2(x/3 - x^2(x/5 - x^2(x/7 - x^3/9 + x^5/11)))
 * = x - x^2(x/3 - x^2(x/5 - x^2(x/7 - x^2(x/9 - x^3/11))))
 * = x - x^2(x/3 - x^2(x/5 - x^2(x/7 - x^2(x/9 - x^2(x/11))))) + ...
 * arctan(1/2) = 1/2 - 1/4(1/6 - 1/4(1/10 - 1/4 (1/14 - 1/4 (1/18 - 1/4(1/22))))) + ...
 * arctan(1/3) = 1/3 - 1/9(1/9 - 1/9(1/15 - 1/9 (1/21 - 1/9 (1/27 - 1/9(1/33))))) + ...
 */

package com.javaAndroid.ex13;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ex13 {
 public static void main(String[] args) {
  double pi = Math.PI; // reference
  int N = 0; // Number of terms
  BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
  System.out.println("How many terms?");
  try {
   N = Integer.parseInt(br.readLine());
  } catch (IOException e) {
   e.printStackTrace();
  } catch (NumberFormatException e) {
   System.out.println("You probably want N = 20");
   N = 20;
  }
  System.out.print("For " + N + " terms, the error is: ");
  double a = 1.0/(4*N+2); // initial
  double b = 1.0/(6*N+3); // initial
  for (int i = N-1; i>=0; i--) {
   a = 1.0/(4*i+2) - a/4.0;
   b = 1.0/(6*i+3) - b/9.0;
  }
  double error = 4*a+4*b-pi;
  System.out.println(error);
 }
}





No comments:

Post a Comment