Taylor.java


Below is the syntax highlighted version of Taylor.java from §9.2 Floating Point.


/******************************************************************************
 *  Compilation:  javac Taylor.java
 *  Execution:    java Taylor N
 *  Dependencies: RationalPolynomial.java
 *
 *  Taylor series of
 *
 *     e^x     = 1 + x + x^2/2! + x^3/3! + x^4/4! + ...
 *   sin x     = x - x^3/3! + x^5/5! - x^7/7! + ...
 *   e^x sin x = x + x^2 + x^3/3 -x^5/30 -x^6/90 + ...
 *
 *  % java Taylor 5
 *  e^x = 1/24 x^4 + 1/6 x^3 + 1/2 x^2 + 1 x + 1
 *  sin(x) = -1/6 x^3 + 1 x
 *  e^x sin(x) = -1/24 x^5 + 1/3 x^3 + 1 x^2 + 1 x
 *
 ******************************************************************************/

public class Taylor {

    // return n!
    public static int factorial(int n) {
        if (n == 0) return 1;
        return n * factorial(n-1);
    }


    public static void main(String[] args) { 

        // number of terms
        int N = Integer.parseInt(args[0]);

        // Taylor series for e^x
        RationalPolynomial e = RationalPolynomial.ZERO;
        for (int i = 0; i < N; i++) {
            BigRational coef = new BigRational(1, factorial(i));
            RationalPolynomial term = new RationalPolynomial(coef, i);
            e = e.plus(term);
        }
        StdOut.println("e^x = " + e);

        // Taylor series for sin(x)
        RationalPolynomial sin = RationalPolynomial.ZERO;
        for (int i = 0; i < N; i++) {
            BigRational coef;
            if      (i % 4 == 0) coef = BigRational.ZERO;
            else if (i % 4 == 1) coef = new BigRational(1, factorial(i));
            else if (i % 4 == 2) coef = BigRational.ZERO;
            else                 coef = new BigRational(-1, factorial(i));
            RationalPolynomial term = new RationalPolynomial(coef, i);
            sin = sin.plus(term);
        }
        StdOut.println("sin(x) = " + sin);

        StdOut.println("e^x sin(x) = " + e.times(sin).truncate(N));

   }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.