September 18, 2018
Hot Topics:

Java in Science: Data Inter- and Extrapolation Using Numerical Methods of Polynomial Fittings, Part 4

  • October 10, 2001
  • By Sione Palu
  • Send Email »
  • More Articles »

Review Part 3

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&#91;&#93; args) throws Exception { //actual array data pairs (xVal, yVal) double&#91;&#93; xVal = {-8.,-6.,-4.,-2.,0.,2.,4.,6.,8.,10.,12.}; double&#91;&#93; 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&#91;&#93;&#91;&#93; Coef = new double&#91;MAXIMUM_ORDER&#93;&#91;&#93;; //upper error-bounds double&#91;&#93;&#91;&#93; U = new double&#91;MAXIMUM_ORDER&#93;&#91;&#93;; //lower error-bounds double&#91;&#93;&#91;&#93; L = new double&#91;MAXIMUM_ORDER&#93;&#91;&#93;; //evaluation of best fit polynomial double&#91;&#93;&#91;&#93; yEval = new double&#91;MAXIMUM_ORDER&#93;&#91;&#93;; //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&#91;&#93; polyf = new Polyfit&#91;MAXIMUM_ORDER&#93;; //array of Polyval Polyval&#91;&#93; polyv = new Polyval&#91;MAXIMUM_ORDER&#93;; //array of matrices for error estimation for each polynomial order Matrix&#91;&#93; error = new Matrix&#91;MAXIMUM_ORDER&#93;; //array of matrices for polynomial evaluation for each polynomial order Matrix&#91;&#93; yOutMat = new Matrix&#91;MAXIMUM_ORDER&#93;; //array of matrices for upper-bound error estimation for each polynomial order Matrix&#91;&#93; upper = new Matrix&#91;MAXIMUM_ORDER&#93;; //array of matrices for lower-bound error estimation for each polynomial order Matrix&#91;&#93; lower = new Matrix&#91;MAXIMUM_ORDER&#93;; try{ for(int i=0; i < MAXIMUM_ORDER ; i++){ ORDER = i ; //instantiate polyfit objects with different order, starting at order=0 polyf&#91;i&#93; = new Polyfit(xVal,yVal,ORDER); //array of polynomial coefficients at order = ORDER Coef&#91;ORDER&#93; = polyf.getPolyCoeffMatrix().getColumnPackedCopy(); } } catch(Exception ex) { throw new Exception(ex.getMessage());} Matrix xm = JElmat.linspace(-8,22,number_of_points); double&#91;&#93; xEval = new JElmat(xm.getArrayCopy()).matrixArrayRow(0); for(int k=0 ; k < MAXIMUM_ORDER ; k++){ polyv&#91;k&#93; = new Polyval(xEval,polyf&#91;k&#93;); //matrix estimate of errorBounds error&#91;k&#93; = polyv&#91;k&#93;.getErrorBoundsMatrix(); //matrix evaluation of polynomial fitting yOutMat&#91;k&#93; = polyv&#91;k&#93;.getYoutMatrix(); //matrix estimate of upper-limit errorBounds upper&#91;k&#93; = yOutMat&#91;k&#93;.plus(error&#91;k&#93;); //matrix estimate of lower-limit errorBounds lower&#91;k&#93; = yOutMat.minus(error&#91;k&#93;); //array estimate of upper-limit errorBounds U&#91;k&#93; = new JElmat( upper&#91;k&#93;.getArrayCopy()).matrixArrayRow(0); //array estimate of lower-limit errorBounds L&#91;k&#93; = new JElmat( lower&#91;k&#93;.getArrayCopy()).matrixArrayRow(0); //array evaluation of polynomial fitting yEval&#91;k&#93; = polyv&#91;k&#93;.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&#91;j&#93;) -> plot evaluation data pairs of best fit polynomial with order = j // (xEval,L&#91;j&#93;) -> plot lower error-bounds for best fit polynomial of order = j // (xEval,U&#91;j&#93;) -> plot upper error-bounds for best fit polynomial of order = j } }//-- end main -- }//--- End Class Definition ---


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
    red line - best fit polynomial
    black line - upper bound error estimate
    yellow line - lower bound error estimate

Y = 1.918182

Y = 0.684091*X + 0.55

Y = 0.010111*X2 + 0.724534*X + 0.913986

Y = 0.000874*X3 - 0.015355*X2 + 0.672786*X + 1.031469

Y = 0.000754*X4 - 0.005157*X3 - 0.072654*X2 + 0.950233*X + 1.61049

Y = 0.000131*X5 + 0.002061*X4 + 0.006171*X3 - 0.161537*X2 + 0.739704*X + 2.27972

Y = 0.000033*X6 - 0.000525*X5 - 0.000927*X4 + 0.040596*X3 - 0.092734*X2 + 0.172263*X + 2.004319

Y = 0.000004*X7 + 0.000088*X6 - 0.000177*X5 - 0.006612*X4 + 0.033296*X3 + 0.049072*X2 + 0.205033*X + 1.506746


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:


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 Jamlab and Jamlab documentation.


  • 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

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.

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Thanks for your registration, follow us on our social networks to keep up-to-date