Numeric Computations in Java
Introduction
Numerical computation is the study and devising of procedures, methods, and functions for solving problems with a computer. An algorithm is used in a systematic way that solves a problem. A numeric computation software developer is not just interested in computing the answer for a problem, but also the best or optimal algorithm that gets there in the most efficient way. The efficiency can be measured by the number of steps in the algorithm, CPU time, and the amount of memory allocated for a specific numerical computation. The advantage of numerical computation is that a numerical answer to a specific model problem can always be obtained even when it has no analytic solution at all. Take the following equation:
If you try to find a closed-form analytical solution to the above equation, do not bother; there is none. You cannot apply traditional methods such as integrating-by-parts or integrating-by-substitution to solve for a closed form solution. Numerical computation can solve and compute the answer to the above equation or any type of model problem by using some well-known numerical analysis techniques such as Euler, Runge-Kutta-Fehlberg, multistep, Adams-Moulton, and so forth. The only mathematical operations that require solving for the solution are addition, subtraction, multiplication, division, and numeric comparisons. Because these simple operations are exactly what a computer does, numerical computation is best achieved using it. A numerical solution to a problem model is always numerical, while analytical methods produce a result in terms of mathematical functions. Analysis of computer errors and all sources of error in numerical computation are critical to the accuracy of calculation. Numerical computation can be done in any computer language, but there are traditional ones that are in favour with the community of software developers. Fortran has been the cornerstone choice from the early days of computers; Pascal, C, C++, and MatLab have also established themselves in numerical computing. Java has risen amongst the academic institutions, government research institutes, industry, and the scientific communities to be a preferred language for developing numerical-based software.
Arithmetic and Errors
Because numerical computation is compute intensive, and there are a lot of internal iterations for a specific algorithm, it is very important to set certain parameters at the correct level. If the tolerance level is set too small, it will consume a lot of memory or create an infinite loop. When it is too high between search boundaries, the procedure will terminate early and miss the solution. The following pseudo-code illustrates this point. This is the algorithm for the bisection method, that solves the roots of a function with one independent variable z, that is f(z):
DO c = (a + b)/2 IF( f(c)*f(a) < 0) b = c ELSE a = c WHILE ( |a-b| < 2*TOL ) |
The bisection method searches for the root in an interval section (boundaries) between z=a and z=b for a value of z where f(z)=0, the value z=c. is a root solution to f(z) with an error of no more than ½ |a-b|. A reasonable value of the tolerance (TOL) is 1.0E-3 , and it should not be much less than, say, 1.0E-5 or the problem will be too slow to converge to the solution. Sometimes tolerance value depends on the type of problem to be solved. For example, if you are developing software for nuclear physics analysis, you must set the tolerance to a smaller value that is to be less than the atomic or nucleon radius which is around the order of 1.0E-9 to 1.0E-15 meters; if you do not do this, your software will never find any solution (roots).
In numerical computing, the software developer should always be looking for trade-offs and optimal parameter values so that computation is efficient. This always applies even though you are using the correct algorithm.
The following sections contain different types of common errors that arise in numerical computation; these originate from numerical analysis ways of implementation and also the inexact arithmetic of the computer:
Truncation error
This type of error is caused by the method of approximation by itself. A good example is the polynomial approximation of sin(x) by using the Taylor series. Let's say we approximate the first three terms (n=0,1,2):
Nevertheless, we do know by analytic form that to compute sin(x), it really requires an infinitely long series. The error arises because the series is truncated; it has nothing to do with the computer at all. The remaining summation represents the error. It is unavoidable, but the series must be truncated to enable any real problem or algorithm to be computable.
Round-off error
Computers always use floating-point numbers (real numbers) of a fixed word length and the exact or true value is not expressed precisely by such computer representations. When floating-point numbers are rounded and stored, the round-off error is smaller than if the trailing digits were simply lopped off.
Page 1 of 5
This article was originally published on June 11, 2002