In: Computer Science
The following is the Maclaurin expansion series for calculating ? ? : ? ? = 1 + ? + ? 2 2! + ? 3 3! + ? 4 4! + ⋯ + ? ? ?! This natural exponential function uses the irrational number ? as a base. You can find the number ? in Java as ???ℎ. ?, as well as, the exponential method as ???ℎ. ???(). You will implement your own exponential method following these criteria: 1. Determine the best returning datatype for your method. Remember it is an exponential function, as so, it grows very fast.
2. Make it public static and accept one parameter: x, as found in the formula above. Create your own method for calculating the factorial and invoke it from your exponential method as needed.
3. For testing purposes, you can match the results of your method to ???ℎ. ? while keeping your argument fixed as 1, e.g., exponential(1). Start by calculating only 5 terms of your series. Then extend the number of terms to 10, 15 and 20. In each case, provide a snapshot of the results where your calculated value appears in parallel with ???ℎ. ?.
4. Repeat the process followed in the previous item but this time compare results with 15, 16, 17, 18, 19 and 20 terms. In each case, provide a snapshot of the results where your calculated value appears in parallel with ???ℎ. ?.
5. How many iterations (addition of terms) did your method require to converge? You know your method converged when the values stop changing as more terms are added.
6. Provide some recommendations on how to improve your method without the experimental process of running for a specific number of iterations.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
import java.math.*;
class Maclaurin {
//method to compute the factorial
public static int myFactorial(int n) {
int facto = 1;
for (int i = 1; i <= n; i++) {
facto = facto * i;
}
return facto;
}
//method to compute the Maclaurin series expansion
public static double Maclaurinseries(double x) {
double myExp = 1;
//set the value of n
//vary this vlaue to generate various n
int n = 5;
System.out.println("The value of n= " + n);
//to sum the various terms upto n
for (int i = 1; i <= n; i++) {
double xi = x;
//for loop to compute the powers of x
for (int j = 1; j < i; j++) {
//compute the power of x
xi = xi * x;
}
//find the current term
double cu = xi / myFactorial(i);
//the my exponent term
myExp = myExp + cu;
}
//return the exponent value
return myExp;
}
public static void main(String[] args) {
//call the method
System.out.println(
"The Maclaurin series value of e= " + Maclaurinseries(1)
);
System.out.println("The actual value= " + Math.exp(1));
}
}