The Euler's number, e, is calculated for the first 17 Taylor Series.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhefa7ZgjZUFaFQRyEQDzse43B0GCzXjiDhr8ANM0VA1s5fWusJ5Fifio97zSrwEtVQXCJ9pYmmCv2Af7BU_Pq0sEBEd6lf6UXePNsWrsOWfSJz2xcvynrRsJsTWRTwju0_sgzKtmakqQBY/s1600/java11+-+1.png)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRPe8AH_AKyehJz4t4KvD9NO-_NdEtODCm9INwPCKpJ1pxuHTv-3eCDIASAwd5WKHeCbWEskMQncS71YwSto0WZ8EVpCcm0uoSRhmVJ5NIzl1-nn5e_aS2YxnFdyPU7BkHJs_Jy3bky1RJ/s1600/java11+-+2.png)
* *** 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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKxTH-00f1U7pMMVTdw_tEIrBG2znLTlvYaawt3YGtMCM3aWbLaAjQ9SkDpD7yy5gL05uUkUJ_Fnexzyg2PjUyen6vwL-lODt22_JzPxXbe5W1vX6blOF5visxowjJsMHJ7At2B65TLbZF/s1600/java11+-+3.png)
// *** 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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYyD9fD4flNNl6JTx6NlC-ekeNDKhQmGCjp8hYyRH_mNU1rqHVgnUE58flnaD9R1Fc8D4BUMgmCorXI05wDtMQ3z0-OwzpY1jSp9it9xrY6WWqNbQAM29EhtsmuRd434q8Z_uzSz5fIj6q/s1600/java11+-+4.png)
// *** 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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwWUXLVELKRbxxyGdOSehnoMN_guwE-FYYxqoQrFbaRopZwtcmz3hdJ3kYCDjxBgxeYMe_JnuiBQc0JPeAb1SVvLzetEQfF0G83zH3oe2q2CPEt_HjTAmYcIPJRQhdIskm2adnQ69b5Dq-/s1600/java11+-+5.png)
// *** 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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhop5QPfK3KztfPHL-cy6MoRg1lSZzAcdA3Z8uRU4IWsk3QVWbiyksD_hj_Hpy8MYAgGjfIUDcHK7kRRH5ftmfXDqbEagyUA3RcqHw2siZ_YZix7WUIsQv8z72ZCm7Dijel6OWozHihJZL7/s1600/java11+-+6.png)
// *** 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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiX5kS909doLe2jf2eoOWKfGi4mSy0ikydYq90fLM3rd-iFRHTXZWcIKluLPcjZRwulauR0PibeFLoxgkp60mlAlz3uQy129brt7zaHq_Nw1KUKUzgoc17Za-DGXU19TpwHGYiUWmljOxse/s1600/java11+-+7.png)
// *** 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