Class PolynomialFunction

  • All Implemented Interfaces:
    Function


    public class PolynomialFunction
    extends Object
    implements Function
    This function evaluates a polynomial expression.
    • Constructor Detail

      • PolynomialFunction

        public PolynomialFunction(double[] coeffs)
        Create a new PolynomialFunction.
        Parameters:
        coeffs - the coefficients of this polynomial. The first coefficient corresponds to the highest power of x. So if coeffs is [2, 3, 4] then this function will evaluate as (2*t*t+3*t+4).
    • Method Detail

      • createFit

        public static PolynomialFunction createFit(double x1,
                                                   double y1,
                                                   double x2,
                                                   double y2)
        Creates a linear PolynomialFunction that passes through the two points provided.
        Parameters:
        x1 - the x-coordinate of the first point.
        y1 - the y-coordinate of the first point.
        x2 - the x-coordinate of the second point.
        y2 - the y-coordinate of the second point.
        Returns:
        a PolynomialFunction that passes through the points provided.
      • createFit

        public static PolynomialFunction createFit(double[] xs,
                                                   double[] ys)
        Creates a PolynomialFunction that uses the coordinates provided.
        Parameters:
        xs - an array of x-coordinates.
        ys - an array of y-coordinates. Each element in this array corresponds to an element of the x coordinates.
        Returns:
        a function that matches the coordinates provided.
      • createFit

        public static PolynomialFunction createFit(double[] xs,
                                                   double[] ys,
                                                   double[] yDerivatives)
        Creates a PolynomialFunction that uses the coordinates provided.
        The function returned will pass through all the points provided, with the dy/dx values provided.
        Parameters:
        xs - an array of x-coordinates.
        ys - an array of y-coordinates. Each element in this array corresponds to an element of the x coordinates.
        yDerivatives - an array of dy/dx values. Each element in this array corresponds to an element of the x coordinates.
        Returns:
        a function that matches the coordinates provided.
      • evaluate

        public double evaluate(double x)
        Description copied from interface: Function
        Evaluates f(x).
        Specified by:
        evaluate in interface  Function
        Parameters:
        x - the input for this function.
        Returns:
        the output of this function.
      • toString

        public String toString()
      • evaluateInverse

        public double[] evaluateInverse(double y)
        Solve this polynomial function by recursive exploring all the derivatives and strategically applying Newton's Method. This is imperfect, but a decent analytical guess.
        Specified by:
        evaluateInverse in interface  Function
        Parameters:
        y - a possible output of this function.
        Returns:
        all the possible inputs that would map to the argument.
      • solve

        public double[] solve()
        Calls evaluateInverse(0)
        Returns:
        calls evaluateInverse(0)