Example usage for org.apache.commons.math3.analysis.polynomials PolynomialFunction toString

List of usage examples for org.apache.commons.math3.analysis.polynomials PolynomialFunction toString

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.polynomials PolynomialFunction toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string representation of the polynomial.

Usage

From source file:net.liuxuan.temp.mathtest.java

public static void main(String[] args) {

    final CurveFitter fitter = new CurveFitter(new LevenbergMarquardtOptimizer());
    fitter.addObservedPoint(-1.00, 2.021170021833143);
    fitter.addObservedPoint(-0.99, 2.221135431136975);
    fitter.addObservedPoint(-0.98, 2.09985277659314);
    fitter.addObservedPoint(-0.97, 2.0211192647627025);
    // ... Lots of lines omitted ...
    fitter.addObservedPoint(0.99, -2.4345814727089854);

    // The degree of the polynomial is deduced from the length of the array containing
    // the initial guess for the coefficients of the polynomial.
    final double[] init = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2

    // Compute optimal coefficients.
    final double[] best = fitter.fit(new PolynomialFunction.Parametric(), init);

    // Construct the polynomial that best fits the data.
    final PolynomialFunction fitted = new PolynomialFunction(best);
    System.out.println(fitted.value(-0.995));
    ;//from  w  w  w.j av a2s .c o m
    System.out.println(fitted.value(0.995));
    ;
    System.out.println(fitted.toString());
    ;
    System.out.println("=============================================================");
    PolynomialCurveFitter pcf = PolynomialCurveFitter.create(3);
    WeightedObservedPoints s;
    List<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>();
    points.add(new WeightedObservedPoint(1, -1.00, 2.021170021833143));
    points.add(new WeightedObservedPoint(1, -0.99, 2.221135431136975));
    points.add(new WeightedObservedPoint(1, -0.98, 2.09985277659314));
    points.add(new WeightedObservedPoint(1, -0.97, 2.0211192647627025));
    points.add(new WeightedObservedPoint(1, 0.99, 2.4345814727089854));
    double a[] = pcf.fit(points);
    for (int i = 0; i < a.length; i++) {
        double d = a[i];
        System.out.println(d);
    }
    System.out.println(compute(a, -0.995));
    System.out.println(compute(a, 0.99));
    System.out.println(compute(a, 0.995));

}