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 ???ℎ. ?. (20
marks)
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. (20
marks)
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?
Enclose your solution in a PDF file. Make sure your code is
included in text. Remember to
add all the requested snapshots.
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=20:
For n= 16:
For n=17:
For n =18:
For n=19
Recommendation for improving the method:
Round-off error: