/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math.analysis; import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.ConvergenceException; /** * Utility routines for {@link UnivariateRealSolver} objects. * * @version $Revision: 615734 $ $Date: 2008-01-27 23:10:03 -0700 (Sun, 27 Jan 2008) $ */ public class UnivariateRealSolverUtils { /** * Default constructor. */ private UnivariateRealSolverUtils() { super(); } /** Cached solver factory */ private static UnivariateRealSolverFactoryImpl factory = null; /** * Convenience method to find a zero of a univariate real function. A default * solver is used. * * @param f the function. * @param x0 the lower bound for the interval. * @param x1 the upper bound for the interval. * @return a value where the function is zero. * @throws ConvergenceException if the iteration count was exceeded * @throws FunctionEvaluationException if an error occurs evaluating * the function * @throws IllegalArgumentException if f is null or the endpoints do not * specify a valid interval */ public static double solve(UnivariateRealFunction f, double x0, double x1) throws ConvergenceException, FunctionEvaluationException { setup(f); return factory.newDefaultSolver(f).solve(x0, x1); } /** * Convenience method to find a zero of a univariate real function. A default * solver is used. * * @param f the function * @param x0 the lower bound for the interval * @param x1 the upper bound for the interval * @param absoluteAccuracy the accuracy to be used by the solver * @return a value where the function is zero * @throws ConvergenceException if the iteration count is exceeded * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if f is null, the endpoints do not * specify a valid interval, or the absoluteAccuracy is not valid for the * default solver */ public static double solve(UnivariateRealFunction f, double x0, double x1, double absoluteAccuracy) throws ConvergenceException, FunctionEvaluationException { setup(f); UnivariateRealSolver solver = factory.newDefaultSolver(f); solver.setAbsoluteAccuracy(absoluteAccuracy); return solver.solve(x0, x1); } /** * This method attempts to find two values a and b satisfying
lowerBound <= a < initial < b <= upperBound
f(a) * f(b) < 0
[a,b],
this means that a
* and b
bracket a root of f.
*
* The algorithm starts by setting
* a := initial -1; b := initial +1,
examines the value of the
* function at a
and b
and keeps moving
* the endpoints out by one unit each time through a loop that terminates
* when one of the following happens:
f(a) * f(b) < 0
-- success! a = lower
and b = upper
* -- ConvergenceException Integer.MAX_VALUE
iterations elapse
* -- ConvergenceException
* Note: this method can take
* Integer.MAX_VALUE
iterations to throw a
* ConvergenceException.
Unless you are confident that there
* is a root between lowerBound
and upperBound
* near initial,
it is better to use
* {@link #bracket(UnivariateRealFunction, double, double, double, int)},
* explicitly specifying the maximum number of iterations.
lowerBound <= a < initial < b <= upperBound
f(a) * f(b) < 0
[a,b],
this means that a
* and b
bracket a root of f.
*
* The algorithm starts by setting
* a := initial -1; b := initial +1,
examines the value of the
* function at a
and b
and keeps moving
* the endpoints out by one unit each time through a loop that terminates
* when one of the following happens:
f(a) * f(b) < 0
-- success! a = lower
and b = upper
* -- ConvergenceException maximumIterations
iterations elapse
* -- ConvergenceException