# Java in Science: Data Interpolation and Extrapolation Using Numerical Methods of Polynomial Fittings, Part 1

The value of a function **g(x)** at a specific set of points **x _{1},x_{2},x_{3},...,x_{n}** is sometimes known, but there is no analytical expression for

**g(x)**that enables a calculation of its value at an arbitrary point. An estimation of

**g(x)**for an arbitrary

**in a way that drawing a smooth curve through (**

*x***) can be achieved by numerical analysis.**

*x*,g(_{i}*x*)_{i}As an example of looking at a sales profit (in millions of dollars) **P = [3.4, 5.1, 7.6, 9]** for every two month period **T = [2, 4, 6, 8]** from a manufacturing company, and when one is trying to estimate **P** where **T = 5**(inside ** T**-boundary) or

**T = 10**(outside

**-boundary) it would be inaccurate without data smoothing techniques of numerical analysis. If the desired**

*T***is bounded between**

*x***[x**that is

_{1},...,x_{n}]**is in between the largest and the smallest of the**

*x***, this is called**

*x*_{i}'s*interpolation*and if

**is outside the boundaries then it is called**

*x**extrapolation*.

#### Polynomials

*Interpolation* and *extrapolation* techniques must model the function, within and beyond the boundaries, **[x _{1},...,x_{n}]** by some functional form. The functional form that is going to be discussed in this tutorial is known as

**. Polynomials of order**

*polynomial fittings***have the general form of:**

*n***y = a**

_{n}x^{n}+ a_{(n-1)}x^{(n-1)}+ ... + a_{2}x^{2}+ a_{1}x + a_{0}**n**is an

**integer**

**n >= 0**.

**a**is a Real number.

_{n}**a**is the coefficient of the

_{n}**th term.Example of a**

*n**4th-order polynomial*:

**y = -2x**, thus polynomial coefficients are:

^{4}+ 5x^{3}+ 3x^{2}- 3x^{}+ 7**a**

_{4}= -2, a_{3}= 5, a_{2}= 3, a_{1}= -3, a_{0}= 7In this tutorial, we will define and derive mathematical operations mainly in *matrix algebra* for how to fit a polynomial of order ** n** into a set of known or observed data pairs ,

**[x**. A

_{i},y_{i}]*polynomial*of any order is specified and then the

*coefficients*are returned as an array of

*doubles*(Java data-type).

Linear (straight-line) fitting is a polynomial of *order 1* (also known as a *first-order* polynomial, and it has wide applications in modeling from simulations, graphics to financial forecast . Before we delve in to the Java coding , I want to introduce the concept of *linear algebra* and *matrix operations* so the general reader
will understand the code with minimum difficulties.

#### Matrix Algebra

CAD (computer-aided design) and CAM (computer-aided manufacturing) software have revolutionized the automotive industry, for instance. Today computer graphics forms the heart, and *linear algebra* the soul of modern car design. Before a new car is built, engineers design and construct a mathematical car -- a wire-frame model that exists only in computer memory and on graphics display terminals. The car is also mathematically tested (to simulate impact crash-force, engine heat conduction, metal vibration-fatigue frequency, etc.) before even a prototype is built. *Linear algebra* is indeed the foundation of any graphics software because all the manipulations of screen images are accomplished via algebraic techniques.

If ** A** is an

**matrix -- that is, a matrix with**

*m x n***rows and**

*m***columns -- then the scalar entry in the**

*n***th row and the**

*i***th column of**

*j***is denoted by**

*A***and is called the (**

*a*_{ij}**) -- entry of**

*i,j***. The class**

*A***has an internal array storage as a private instance variable named**

*Matrix.java***and it has an accessor method to read its value.**

*A*#### Jama and Jamlab

*Jama* is a basic linear algebra package for Java. The classes in this package enable the construction and manipulation of real, dense matrices. Jama is sufficient enough to provide functionality for routine problems and packaged in a way that is natural and understandable to non-experts. It is intended to serve as the standard matrix class for Java and will be proposed as such to the Java Grande Forum, which represents those from academia and corporations drafting proposals to Sun on Java APIs for scientific and engineering applications. Jama was developed by the National Institute of Standards and Technology (NIST) and The MathWorks Inc.. It is available for free download.

The Jama package has six classes:

- Matrix
- CholeskyDecomposition
- LUDecomposition
- QRDecomposition
- SingularValueDecomposition
- EigenvalueDecomposition

The Matrix class provides the fundamental operations of numerical linear algebra. Various constructors create matrices from two-dimensional arrays of double-precision floating point numbers. Various gets and sets provide access to submatrices and matrix elements. The basic arithmetic operations include matrix addition and multiplication, matrix norms and selected element-by-element array operations. In this tutorial , we discuss matrix operations using ** Jama**.

*Jamlab* (Java Matrix Laboratory) is my own developed Java matrix package, which is based on the functionality of the popular scientific and engineering language called *MatLab* from MathWorks. *Jamlab* is my own attempt to write or translate functions written in *MatLab* to a *Java* version. This package contain classes that are subclassed from class Matrix of the package *Jama*. There are currently six classes in *Jamlab*:

- Jdatafun
- Jelfun
- Jelmat
- RowColumnIndex
- Polyfit
- Polyval

*Jamlab* is not yet complete -- a full version will be available for free download in the future. The reader is encouraged to download *Jama* and *Jamlab* prior to continuing on to the next part of the series.

Download ** Jama** and

*Jamlab**and*

**.***Jamlab documentation*

#### References

*Java for Engineers and Scientists*by Stephen J. Chapman, Prentice Hall, 1999.*Introductory Java for Scientists and Engineers*by Richard J. Davies, Addison-Wesley Pub. Co., 1999.*Applied Numerical Analysis (Sixth Edition)*by Curtis F. Gerald and Patrick O. Wheatly, Addison-Wesley Pub. Co., 1999.*Linear Algebra and Its Applications (Second Edition)*, by David C. Lay, Addison-Wesley Pub. Co.*Numerical Recipes in Fortran 77, the Art of Scientific Computing (Volume 1)*by William H. Press, Saul A. Teukolsky, William T. Vetterling, and Brian P. Flannery, Cambridge University Press, 1997.*Mastering MATLAB 5: A Comprehensive Tutorial and Reference*by Duane Hanselman and Bruce Littlefield, Prentice Hall, 1997.*Advanced Mathematics and Mechanics Applications using MATLAB*by Louis H. Turcotte and Howard B. Wilson, CRC Press, 1998.

#### Related Articles

- Java as a Scientific Programming Language (Part 1): More Issues for Scientific Programming in Java
- Scientific Computing in Java (Part 2): Writing Scientific Programs in Java
- Using Java in Scientific Research: An Introduction

#### About the Author

*Sione Palu is a Java developer at Datacom in Auckland, New Zealand, currently involved in a Web application development project. Palu graduated from the University of Auckland, New Zealand, double majoring in mathematics and computer science. He has a personal interest in applying Java and mathematics in the fields of mathematical modeling and simulations, expert systems, neural and soft computation, wavelets, digital signal processing, and control systems.*

*This article was originally published on May 9, 2001*

*
*