Assignment:

One way to calculate ex is to use the infinite series expansion

ex = 1 + x + x2/2! + x3/3! + x4/4! + ...

If the loop variable is named i, then the ith term is equal to xi/i!.

a. Write a method called myexp that adds up the first n terms of the series shown
above. You can use the factorial method from Section 5.10 or your iterative
version.

b. You can make this method much more efficient if you realize that in each iter-
ation the numerator of the term is the same as its predecessor multiplied by x
and the denominator is the same as its predecessor multiplied by i. Use this
observation to eliminate the use of Math.pow and factorial, and check that
you still get the same result.

c. Write a method called check that takes a single parameter, x, and that prints
the values of x, Math.exp(x) and myexp(x) for various values of x. The output
should look something like:

1.0 2.708333333333333 2.718281828459045

HINT: you can use the String "\t" to print a tab character between columns of
a table.

d. Vary the number of terms in the series (the second argument that check sends
to myexp) and see the effect on the accuracy of the result. Adjust this value
until the estimated value agrees with the “correct” answer when x is 1.

e. Write a loop in main that invokes check with the values 0.1, 1.0, 10.0, and 100.0.
How does the accuracy of the result vary as x varies? Compare the number of
digits of agreement rather than the difference between the actual and estimated
values.

f. Add a loop in main that checks myexp with the values -0.1, -1.0, -10.0, and
-100.0. Comment on the accuracy.

So far I have................

``````package myexp;

import java.util.Scanner;

public class Main {

public static void main(String args[]) {
double x, n, sum = 1 ;
int i;

System.out.println("Enter the value of x: ");

System.out.println("Enter the value of n: ");
System.out.println ();

for(i = 1; i <= n; i++) {
sum += myexp(x,i);
}
System.out.println (sum);
}

public static double myexp( double x, int i) {

if (i == 1)
return x;
else
return Math.pow (x,i)/ factorial (i);
}

public static double factorial(double x, int n) {

double factorial = 1.0;
int i = 1;
while (i <= n) {
factorial = factorial * i;
i = i + 1;
}
return factorial;
}
}``````

## All 4 Replies

Where are you getting stuck, what do you need help with, etc...

I am stuck on part b and I need there. Part b refers to line 32 of my code.

You can make two static variables: numeratorValue and denominatorValue. After the first time you calculate your numerator and your denominator, save the results into numeratorValue and denominatorValue. Every time you calculate your numerator and denominator after that,

``````newNumerator = numeratorValue * x;
newDenominator = denominatorValue * i;
numeratorValue = newNumerator;
denominatorValue = newDenominator;``````

Where should I put that method in my program

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.