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. 7. What are round-off errors and what causes them?
Answer :
The Java code for the problem is provided below, please follow the basic procedure to run a Java code and run the code to obtain the results:
Note: As given in the question, it is required to change the value of n, to find the various test cases, it can be change in the code, initial value is 5,
The outputs are provided for changing the various values of n.
Java code:
File name: Maclaurin. java
import java.math.*;
public 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));
}
}
Various output test:
For n=5:
For n=10:For n=15:For n=20For n= 16:
Instead of providing the constant value for n, it can be
iterated using a for, loop, then the n values can be varied in a
single run itself.
Also use an error allowed to converge the iteration, the for loop
will converge when the computed e value and original e values are
come closer to a predefined error limit.
Round-off error:
It is the change the value of a variable when the adjustment is done to decrease the number of digits used to represent the number. For example if the value e=2.713, when rounded to three digits, it will becomes 2.71 and some error happened in the rounding off.
I hope this answer is helpful to you, please upvote my answer. I'm in need of it.. Thank you..