Java Reference
In-Depth Information
282
΢΢ Exercise P6.8. The Heron method is a method for computing square roots
that was known to the ancient Greeks. If x is a guess for the value
a
, then
the average of x and a/x is a better guess.
Implement a class RootApproximator that starts with an initial guess
of 1 and whose nextGuess method produces a sequence of increasingly
better guesses. Supply a method hasMoreGuesses that returns false
if two successive guesses are sufficiently close to each other (that is, they
differ by no more than a small value Ш). Then test your class like this:
RootApproximator approx = new RootApproximator(a,
epsilon);
while (approx.hasMoreGuesses())
System.out.println(approx.nextGuess());
΢΢ Exercise P6.9. The best known iterative method for computing the roots of
a function f (that is, the x-values for which f(x) is 0) is NewtonȋRaphson
approximation. To find the zero of a function whose derivative is also
known, compute
( x old ) /
( x old )
x new
=
x old
ɨ f
f Ƀ
.
For this exercise, write a program to compute nth roots of floating-point
numbers. Prompt the user for a and n, then obtain by computing a zero
of the function f(x) x n ɨ a. Follow the approach of Exercise P6.8.
n
a
΢΢ Exercise P6.10. The value of e x can be computed as the power series
¯
ɧ n = 0
x n
n !
e x
=
where n!=1 – 2 – 3 – ș – n.
 
Search WWH ::




Custom Search