| | The initial operation in chemistry is to transform a chemical formula, which is a string corresponding to a sequence of characters and digits, into a molecular weight (a real number). It is obvious for the intellect of the chemist but cannot be easily achieved by the computer. | Java was designed as a modern, object-oriented programming language. Its features such as platform-independence, ability to manage libraries, threads, etc. are important for modern scientific programs. These are good reasons for a scientist to choose Java. | For scientists, learning Java and learning to think scientifically in Java may not be an easy step. Here is some additional commentary on the following issues, all of them important for scientists [ Java as a Scientific Programming Language (Part 1): More Issues for Scientific Programming in Java | The basic language of scientific rules is coded to be platform independent. By knowing a few basic definitions, classes, and procedures, even aliens landing on earth should be able to quickly understand them. Consequently, a tool to communicate and simulate these rules should also obey this philosophy. Since it is unlikely the aliens could immediately run a Java-capable browser on their systems (though it would be one of their first efforts), I'll focus on human researchers around the world. | Previous article: Java in Science: Data Interpolation and Extrapolation Using Numerical Methods of Polynomial Fittings, Part 1 Next article: Java in Science: Data Inter- and Extrapolation Using Numerical Methods of Polynomial Fittings, Part 3 | Furthermore, because if its inherent simplicity, and the availability of a large Math library, Java provides an excellent programming language for engineers and scientists who want to do their own programming, but who have no desire to become programming experts. The code required to conduct an engineering or scientific computational experiment often consists of little more than the most rudimentary application of arithmetic in loops using data stored in arrays or read from disk files. | 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 --- | | 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 T-boundary) it would be inaccurate without data smoothing techniques of numerical analysis. If the desired x is bounded between [x1,...,xn] that is x is in between the largest and the smallest of the xi's, this is called interpolation and if x is outside the boundaries then it is called extrapolation. | Previous article: Java in Science: Data Inter- and Extrapolation Using Numerical Methods of Polynomial Fittings, Part 2 Next article: Java in Science: Data Inter- and Extrapolation Using Numerical Methods of Polynomial Fittings, Part 4 | Part 1 of this article discussed how scientists can benefit from Java. We've said that despite a few pitfalls which scientists should watch out for, the future of Java as a scientific programming language looks bright. Here in Part II we examine the structure of a scientific program more closely. We'll define a few scientific OOP design patterns in Java, and we'll give you a short style guide that can help scientists write good Java programs (please see the sidebar "A Style Guide for Scientific Programs in Java"). | As you can see in Listing 1, the class implements the interface named GraphIntfc01. I introduced this interface in the earlier lesson entitled Plotting Engineering and Scientific Data using Java and also discussed it in the previous lesson entitled Spectrum Analysis using Java, Sampling Frequency, Folding Frequency, and the FFT Algorithm. | Figure 9 clearly shows the frequency folding effect of the sampling process illustrated earlier. As you can see, the peaks in the various graphs to the right of the folding frequency are mirror images of the peaks to the left of the folding frequency. In other words, given a set of samples of a sinusoid, the spectral analysis process is unable to determine whether the peak is above or below the folding frequency, so the energy is equally distributed between two peaks on opposite sides of the folding frequency. | The lesson entitled Spectrum Analysis using Java, Sampling Frequency, Folding Frequency, and the FFT Algorithm presented and explained several Java programs for doing spectral analysis, including both DFT programs and FFT programs. That lesson illustrated the fundamental aspects of spectral analysis that center around the sampling frequency and the Nyquist folding frequency. | In an earlier lesson entitled Spectrum Analysis using Java, Sampling Frequency, Folding Frequency, and the FFT Algorithm, I explained most of the code in the method named transform belonging to the class named ForwardRealToComplex01. However, I skipped over that portion of the code that computes the phase angle on the basis of the values of the real and imaginary parts. At this time, I am going to explain that code. For an explanation of the rest of the code in the transform method, go back and review the lesson entitled Spectrum Analysis using Java, Sampling Frequency, Folding Frequency, and the FFT Algorithm. |
|