Commons Math学习笔记(2)

private static void integration() {
  // TODO Auto-generated method stub
  UnivariateFunction f = new Sin();
  BaseAbstractUnivariateIntegrator integrator = new SimpsonIntegrator();

// integrate
  System.out.println("f(x)=sin(x)");
  try {
   System.out.println("integration of f(x) from 0 to Pi is "
     + integrator.integrate(100,f, 0, Math.PI));
  } catch (ConvergenceException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

3.函数插值

package apache.commons.math.test;

import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;
import org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionLagrangeForm;
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
import org.apache.commons.math3.exception.MathArithmeticException;

/**
 *
 * @ClassName: InterpolationTest
 * @Description: 函数插值
 * @author zengfh
 * @date 2014年11月21日 下午3:13:39
 *
 */
public class InterpolationTest {

public static void main(String[] args) {
  // TODO Auto-generated method stub
  polynomialsInterpolation();
  System.out.println("-------------------------------------------");
  interpolatioin();
 }

private static void interpolatioin() {
  // TODO Auto-generated method stub
  // double x[] = { 0.0, 0.5, 1.0 };
  // double y[] = { 0.0, 0.5, 1.0 };
  double x[] = { 0.0, Math.PI / 6d, Math.PI / 2d, 5d * Math.PI / 6d,
    Math.PI, 7d * Math.PI / 6d, 3d * Math.PI / 2d,
    11d * Math.PI / 6d, 2.d * Math.PI };
  double y[] = { 0d, 0.5d, 1d, 0.5d, 0d, -0.5d, -1d, -0.5d, 0d };
  UnivariateInterpolator i = new SplineInterpolator();
  UnivariateFunction f = null;
  // interpolate y when x = 0.5
  try {
   f = i.interpolate(x, y);
   System.out.println("when x = 0.5, y = " + f.value(0.5));
  } catch (MathArithmeticException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

// check polynomials functions
  PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f)
    .getPolynomials();
  for (int j = 0; j < polynomials.length; j++) {
   System.out
     .println("cubic spline:f" + j + "(x) = " + polynomials[j]);
  }
 }

private static void polynomialsInterpolation() {
  // TODO Auto-generated method stub
  double x[] = { 0.0, -1.0, 0.5 };
  double y[] = { -3.0, -6.0, 0.0 };
  PolynomialFunctionLagrangeForm p = new PolynomialFunctionLagrangeForm(
    x, y);
  // output directly
  System.out.println("ugly output is " + p);
  // interpolate y when x = 1.0
  try {
   System.out.println("when x = 1.0, y = " + p.value(1.0));
  } catch (MathArithmeticException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // degree
  System.out.println("polynomial degree is " + p.degree());
  // coefficients
  for (int i = 0; i < p.getCoefficients().length; i++) {
   System.out.println("coeff[" + i + "] is " + p.getCoefficients()[i]);
  }
  //
 }

}

4.多项式函数

package apache.commons.math.test;

import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;

/**
 *
 * @ClassName: PolinomialsFunctionTest
 * @Description: 多项式函数
 * @author zengfh
 * @date 2014年11月21日 下午1:38:13
 *
 */
public class PolinomialsFunctionTest {

/**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  polynomials();
  System.out.println("-----------------------------------------------");
  polynomialsSpline();
 }

private static void polynomialsSpline() {
  // TODO Auto-generated method stub
  PolynomialFunction[] polynomials = {
    new PolynomialFunction(new double[] { 0d, 1d, 1d }),
    new PolynomialFunction(new double[] { 2d, 1d, 1d }),
    new PolynomialFunction(new double[] { 4d, 1d, 1d }) };
  double[] knots = { -1, 0, 1, 2 };
  PolynomialSplineFunction spline = new PolynomialSplineFunction(knots,
    polynomials);
  // output directly
  System.out.println("poly spline func is " + spline);
  // get the value when x = 0.5
  try {
   System.out.println("f(0.5) = " + spline.value(0.5));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // the number of spline segments
  System.out.println("spline segments number is " + spline.getN());
  // the polynomials functions
  for (int i = 0; i < spline.getN(); i++) {
   System.out.println("spline:f" + i + "(x) = "
     + spline.getPolynomials()[i]);
  }
  // function derivative
  System.out.println("spline func derivative is " + spline.derivative());
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2b3344ab111c0a6f8b85c9be183ea318.html