The value ex can be approximated by the following sum: 1 + x + x^2 /2! + x^3 /3!
ID: 3719879 • Letter: T
Question
The value ex can be approximated by the following sum: 1 + x + x^2 /2! + x^3 /3! + …+ x^n /n! The expression n! is called the factorial of n and is defined as: n! = 1*2*3* …*n.
Write a program using java that takes a value of x as input and outputs four approximations of e^x done using four different values of n: 5, 10, 50, and 100. Output the value of x the user entered and the set of all four approximations into the screen. Sample formula use: calculating e7 using approximation with n = 5 1 + 7 + 7^2 /2! + 7^3 /3! + 7^4 /4! + 7^5 /5!
Requirements 1. Input validation is needed - do not allow the user to enter anything but integer. Use exception-handling technique discussed in class and implemented in InputValidation.java example to complete the feature.
2. Please do not do any calculations by writing out the entire sums by hand, use loops and running total calculations instead.
3. Factorial function produces large numbers and it grows very quickly. If you start using regular integers to store factorials, you’ll get integer overflows easily. I would suggest using long to store factorials. On top of that, I would recommend to adjust your calculations in such a way that you never get to calculate the actual factorial values. Look at the pattern in the sum: each next term of the sum can be derived from the previous one by means of a very simple calculation.
4. Test your code. Make sure the calculations are correct by checking the actual values of e^x online. The bigger the value of n – the closer the resulting value must be to the actual value of ex.
Explanation / Answer
ScreenShot
-------------------------------------------------------------------------------------------------------------
Program
/* packages for input/output , calculation and formatting */
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
//Class to test the program
public class Test extends Exception {
//Main method
public static void main(String[] args) {
//Variable declaration
double x,sum=0;
int val=2;
Scanner sc=new Scanner(System.in);
//Read x value
System.out.println("Please enter x value :");
x=sc.nextDouble();
if (x < 0){ throw new IllegalArgumentException("Input cannot be negative.");}
try{
//Loop to calculate e^x for n=5
for(int i=0;i<=5;i++) {
if(i>1) {
String fact=factorial(val);
double v=Double.parseDouble(fact);
sum=sum+(Math.pow(x, i)/v);
val=val+1;
}
else {
sum=sum+Math.pow(x, i);
}
}
DecimalFormat df = new DecimalFormat(".###");
System.out.println("e^"+(int)x+" for n=5 is "+df.format(sum));
//Loop to calculate e^x for n=10
sum=0;
val=2;
for(int i=0;i<=10;i++) {
if(i>1) {
String fact=factorial(val);
double v=Double.parseDouble(fact);
sum=sum+(Math.pow(x, i)/v);
val=val+1;
}
else {
sum=sum+Math.pow(x, i);
}
}
System.out.println("e^"+(int)x+" for n=10 is "+df.format(sum));
//Loop to calculate e^x for n=50
sum=0;
val=2;
for(int i=0;i<=50;i++) {
if(i>1) {
String fact=factorial(val);
double v=Double.parseDouble(fact);
sum=sum+(Math.pow(x, i)/v);
val=val+1;
}
else {
sum=sum+Math.pow(x, i);
}
}
System.out.println("e^"+(int)x+" for n=50 is "+df.format(sum));
//Loop to calculate e^x for n=100
sum=0;
val=2;
for(int i=0;i<=100;i++) {
if(i>1) {
String fact=factorial(val);
double v=Double.parseDouble(fact);
sum=sum+(Math.pow(x, i)/v);
val=val+1;
}
else {
sum=sum+Math.pow(x, i);
}
}
System.out.println("e^"+(int)x+" for n=100 is "+df.format(sum));
}
//Integer exception catch
catch (NumberFormatException e)
{
System.out.println(x + " is not a valid double number");
}
}
//Factorial of the n numbers
public static String factorial(int n) {
BigInteger fact = new BigInteger("1");
for (int i = 1; i <= n; i++) {
fact = fact.multiply(new BigInteger(i + ""));
}
return fact.toString();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.