The Euler's number, e, is calculated for the first 17 Taylor Series.
The recursion formulas, given here, are done. The initial equation sets the first value of innermost level. Then, there are 14 recursive equations, as we go to outer levels, for N = 17. Finally, the final equation gives the approximation to Euler's number.
* *** 1. Start (recursion) * e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! + ... * e = 2 + 1/2 + 1/(2*3) + 1(2*3*4) + 1/(2*3*4*5) + ... * e = 2 + 1/2(1 + 1/3 + 1/(3*4) + 1/(3*4*5)) + ... * e = 2 + 1/2(1 + 1/3(1 + 1/4 + 1/(4*5))) + ... * e = 2 + 1/2(1 + 1/3(1 + 1/4(1 + 1/5))) + ... * *** 1. End
The factorial, is based on a different recursive equation. The numbers in the factorial become very large, very quickly, and thus it returns a long integer.
// *** 2. Start (factorial) static long factorial(int n) { if (n==0) return 1; else return n*factorial(n-1); } // *** 2. End
The Taylor Series is known for all differentiable functions. For exponent, all derivatives are the same.
// *** 3. Start (for - Taylor series) double e = Math.E; // reference int N = 17; // Number of Taylor terms double a = 0.0; // approximation int i; // iteration term for (i = 0; i<=N; i++) a = a + 1.0/factorial(i); double error = e - a; System.out.println("error = " + error); // *** 3. End
The algorithm is done using the while loop.
// *** 4. Start (while - recursion) a = 1 + 1.0/N; // initial i = 0; while ((N-i)>3) { i++; a = 1 + a/(N-i); // recursion } a = 2 + 0.5*a; // final error = e - a; // reference - final System.out.println("error = " + error); // *** 4. End
The algorithm is done using the for loop.
// *** 5. Start (for - recursion) a = 1 + 1.0/N; // initial for (i=1; i<=(N-3); i++) { a = 1 + a/(N-i); // recursion } a = 2 + 0.5*a; // final error = e - a; // reference - final System.out.println("error = " + error); // *** 5. End
The algorithm is done using the do-while loop. Even though the while condition is false in the 14th time, the check is done after loop, so this only means that there is no 15th time.
// *** 6. Start (do while - recursion) i = 0; a = 1 + 1.0/N; // initial do { i++; a = 1 + a/(N-i); // recursion } while ((N-i)>3); a = 2 + 0.5*a; // final error = e - a; // reference - final System.out.println("error = " + error); // *** 6. End
// Ex11.java /* * *** 1. Start (recursion) * e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! + ... * e = 2 + 1/2 + 1/(2*3) + 1(2*3*4) + 1/(2*3*4*5) + ... * e = 2 + 1/2(1 + 1/3 + 1/(3*4) + 1/(3*4*5)) + ... * e = 2 + 1/2(1 + 1/3(1 + 1/4 + 1/(4*5))) + ... * e = 2 + 1/2(1 + 1/3(1 + 1/4(1 + 1/5))) + ... * *** 1. End */ package com.javaAndroid.ex11; public class Ex11 { // *** 2. Start (factorial) static long factorial(int n) { if (n==0) return 1; else return n*factorial(n-1); } // *** 2. End public static void main(String[] args) { // *** 3. Start (for - Taylor series) double e = Math.E; // reference int N = 17; // Number of Taylor terms double a = 0.0; // approximation int i; // iteration term for (i = 0; i<=N; i++) a = a + 1.0/factorial(i); double error = e - a; System.out.println("error = " + error); // *** 3. End // *** 4. Start (while - recursion) a = 1 + 1.0/N; // initial i = 0; while ((N-i)>3) { i++; a = 1 + a/(N-i); // recursion } a = 2 + 0.5*a; // final error = e - a; // reference - final System.out.println("error = " + error); // *** 4. End // *** 5. Start (for - recursion) a = 1 + 1.0/N; // initial for (i=1; i<=(N-3); i++) { a = 1 + a/(N-i); // recursion } a = 2 + 0.5*a; // final error = e - a; // reference - final System.out.println("error = " + error); // *** 5. End // *** 6. Start (do while - recursion) i = 0; a = 1 + 1.0/N; // initial do { i++; a = 1 + a/(N-i); // recursion } while ((N-i)>3); a = 2 + 0.5*a; // final error = e - a; // reference - final System.out.println("error = " + error); // *** 6. End } }
No comments:
Post a Comment