To use the numeric library associated with the textbook (the Java Scientific Graphics Library) add the following import statement:
import VisualNumerics.math.*;
to the start of your .java files. You can then call all of the methods in the library using the syntax:
<method name>(<arguments>);
To use the simple graphics library associated with the textbook (the Java Scientific Graphics Library) add the following import statement:
import uk.co.jscieng.*;
to the start of your .java files. You can then call all of the methods in the library using the syntax:
SciGraph.<method name>(<arguments>);
After installing JSGL the following textbook examples will be discussed:
Your Task: Over 5000 years ago the ancient Babylonians discovered a method for computing the square root of 2. They probably used that number (about 1.4) to construct right angles for the foundations of their buildings. This iterative algorithm is still the simplest way to compute square roots.
If x is any number close to sqrt(2), then x2 will be close to 2, which makes x close to 2/x. But 2/x will be on the other side of sqrt(2) from x. That is, if x is less than sqrt(2), then 2/x will be greater than sqrt(2), and vice versa.
For example, suppose that x = 1.6. Then 2/x = 1.25, which is on the other side of sqrt(2). This crossing over the limit is the key to the algorithm because it means that the average of x and 2/x must be between them and therefore closer to the objective sqrt(2). So the Babylonian Algorithm consists of choosing some number x that is close to sqrt(2), and then repeatedly replacing x by its average with 2/x.
Note that a constant (say TOL = 0.5E-15) is necessary to ensure that the algorithm terminates after a solution of sufficient accuracy has been found.