#### Running the Complete Program

Here is the complete program. Please review the lessons of Parts 1 through 3 before using it.

import Jama.*;

import jamlab.*;

public Class Main

{

public static void main(String[] args) throws Exception

{

//actual array data pairs (xVal, yVal)

double[] xVal = {-8.,-6.,-4.,-2.,0.,2.,4.,6.,8.,10.,12.};

double[] yVal = {-4.,-7.,-2.,1.,2.,1.5,4.,5.,4.9,6.7,9.};

//the total number of polynomials to be fitted to the data pairs

int MAXIMUM_ORDER = 8;

//store the polynomial coefficients

double[][] Coef = new double[MAXIMUM_ORDER][];

//upper error-bounds

double[][] U = new double[MAXIMUM_ORDER][];

//lower error-bounds

double[][] L = new double[MAXIMUM_ORDER][];

//evaluation of best fit polynomial

double[][] yEval = new double[MAXIMUM_ORDER][];

//order of polynomial

int ORDER = 0;

//number of points for smoothing the interpolation and extrapolation of data pairs

int number_of_points = 2000;

//Array of Polyfit

Polyfit[] polyf = new Polyfit[MAXIMUM_ORDER];

//array of Polyval

Polyval[] polyv = new Polyval[MAXIMUM_ORDER];

//array of matrices for error estimation for each polynomial order

Matrix[] error = new Matrix[MAXIMUM_ORDER];

//array of matrices for polynomial evaluation for each polynomial order

Matrix[] yOutMat = new Matrix[MAXIMUM_ORDER];

//array of matrices for upper-bound error estimation for each polynomial order

Matrix[] upper = new Matrix[MAXIMUM_ORDER];

//array of matrices for lower-bound error estimation for each polynomial order

Matrix[] lower = new Matrix[MAXIMUM_ORDER];

try{

for(int i=0; i < MAXIMUM_ORDER ; i++){
ORDER = i ;
//instantiate polyfit objects with different order, starting at order=0
polyf[i] = new Polyfit(xVal,yVal,ORDER);
//array of polynomial coefficients at order = ORDER
Coef[ORDER] = polyf.getPolyCoeffMatrix().getColumnPackedCopy();
}
}
catch(Exception ex)
{ throw new Exception(ex.getMessage());}
Matrix xm = JElmat.linspace(-8,22,number_of_points);
double[] xEval = new JElmat(xm.getArrayCopy()).matrixArrayRow(0);
for(int k=0 ; k < MAXIMUM_ORDER ; k++){
polyv[k] = new Polyval(xEval,polyf[k]);
//matrix estimate of errorBounds
error[k] = polyv[k].getErrorBoundsMatrix();
//matrix evaluation of polynomial fitting
yOutMat[k] = polyv[k].getYoutMatrix();
//matrix estimate of upper-limit errorBounds
upper[k] = yOutMat[k].plus(error[k]);
//matrix estimate of lower-limit errorBounds
lower[k] = yOutMat.minus(error[k]);
//array estimate of upper-limit errorBounds
U[k] = new JElmat( upper[k].getArrayCopy()).matrixArrayRow(0);
//array estimate of lower-limit errorBounds
L[k] = new JElmat( lower[k].getArrayCopy()).matrixArrayRow(0);
//array evaluation of polynomial fitting
yEval[k] = polyv[k].getYout();
}
//Readers who use Java2D graphics for plotting, can implement the following loop
// to plot the output evaluation and error-bounds (upper and lower).
for(int j=0 ; j < MAXIMUM_ORDER ; j++) {
// (xVal,yVal) -> plot actual data pairs

// (xEval,yEval[j]) -> plot evaluation data pairs of best fit polynomial with order = j

// (xEval,L[j]) -> plot lower error-bounds for best fit polynomial of order = j

// (xEval,U[j]) -> plot upper error-bounds for best fit polynomial of order = j

}

}//– end main —

}//— End Class Definition —

#### Interpolations

Intermediate values between data-pairs can be estimated for any polynomial order by

evaluating the specific polynomial in the domain boundry with a large number of points.

The data pairs used in this tutorial were :

double[] **xVal = {-8.,-6.,-4.,-2.,0.,2.,4.,6.,8.,10.,12.}**;

double[] **yVal = {-4.,-7.,-2.,1.,2.,1.5,4.,5.,4.9,6.7,9.}**;

There were two thousand points between the minimum , **-8.** and the maximum,

**12**, to make the estimation as smooth as possible.

The following graphs show different plottings for different order of best fit polynomials.

** blue line** – actual data pairs

**– best fit polynomial**

*red line***– upper bound error estimate**

*black line***– lower bound error estimate**

*yellow line***Y = 1.918182**

**Y = 0.684091*X + 0.55**

**Y = 0.010111*X ^{2} + 0.724534*X + 0.913986**

**Y = 0.000874*X ^{3} – 0.015355*X^{2} + 0.672786*X + 1.031469**

**Y = 0.000754*X ^{4} – 0.005157*X^{3} – 0.072654*X^{2} + 0.950233*X + 1.61049**

**Y = 0.000131*X ^{5} + 0.002061*X^{4} + 0.006171*X^{3} – 0.161537*X^{2} + 0.739704*X + 2.27972**

**Y = 0.000033*X ^{6} – 0.000525*X^{5} – 0.000927*X^{4} + 0.040596*X^{3} – 0.092734*X^{2} + 0.172263*X + 2.004319**

**Y = 0.000004*X ^{7} + 0.000088*X^{6} – 0.000177*X^{5} – 0.006612*X^{4} + 0.033296*X^{3} + 0.049072*X^{2} + 0.205033*X + 1.506746**

#### Extrapolations

Extending the estimation of the best fit polynomials to extrapolate outside the data pairs

domain gives wildly osscilated error-bounds. Error-bounds increase as the estimation moves

away from the *minimum = -8* or *maximum = 12* point of the data pairs domain.

Error-bounds would have reasonable values around the vicinity of the minimum and the maximum.

The following graphs plot the extrapolation beyond the data pairs domain extending from

*minimum = -8* to *maximum = 22*:

#### Applications

Polynomial fittings is one of the numerical techniques that engineers and scientists use

most often in estimation, simulation, and even model prediction. There

are other numerical methods for estimation, such as *splines* for example. The choice

depends on the domain of interest for the engineer. A NASA scientist

might prefer the *cubic spline* as an estimator, while a financial engineering

economist in the Tokyo stock market prefer to use a *polynomial fitting* of ORDER = 1,

(straight line) for future outcome predictions.

#### Final Word

We have introduced the concepts of *matrix algebra* in this tutorial and how it is

applied in numerical techniques of *polynomial fittings*. *Linear algebra* is

a cornerstone of today’s scientific software. There are other branches of engineering

mathematics, such as *discrete mathematics*, *differential equations*,

*multi-variable calculus*, *statistics and probabilities*, *complex variables*,

and *operations research*, etc., which are also the foundations of scientific

software. I will try to visit some of these in future tutorials.

#### Book References

*Java for Engineers and Scientists*by*Stephen J. Chapman*, publisher :*PRENTICE HALL**Introductory Java for Scientists and Engineers*by*Richard J. Davies*, publisher :*ADDISON-WESLEY**Applied Numerical Analysis*(Sixth Edition), by*Curtis F. Gerald*and*Patrick O. Wheatly*, publisher :*ADDISON-WESLEY**Linear Algebra and its Applications*(Second Edition), by*David C. Lay*, publisher :*ADDISON-WESLEY**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*, publisher :*CAMBRIDGE UNIVERSITY PRESS**Mastering MATLAB 5*, by*Duane Hanselman*and*Bruce Littlefield*, publisher :*PRENTICE HALL**Advanced Mathematics and Mechanics Applications using MATLAB*by*Howard B. Wilson*, publisher :*CRC Press*

Download ** Jama** and

**and**

*Jamlab***.**

*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.*