1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/* 2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more 3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements. See the NOTICE file distributed with 4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership. 5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0 6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with 7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License. You may obtain a copy of the License at 8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * http://www.apache.org/licenses/LICENSE-2.0 10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software 12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS, 13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and 15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License. 16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.analysis.solvers; 18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.FunctionEvaluationException; 20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MaxIterationsExceededException; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.UnivariateRealFunction; 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath; 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Implements the <a href="http://mathworld.wolfram.com/Bisection.html"> 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * bisection algorithm</a> for finding zeros of univariate real functions. 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The function should be continuous but not necessarily smooth.</p> 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $ 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class BisectionSolver extends UnivariateRealSolverImpl { 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Construct a solver for the given function. 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param f function to solve. 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated as of 2.0 the function to solve is passed as an argument 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * to the {@link #solve(UnivariateRealFunction, double, double)} or 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)} 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * method. 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public BisectionSolver(UnivariateRealFunction f) { 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(f, 100, 1E-6); 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Construct a solver. 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public BisectionSolver() { 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(100, 1E-6); 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double solve(double min, double max, double initial) 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MaxIterationsExceededException, FunctionEvaluationException { 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return solve(f, min, max); 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double solve(double min, double max) 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MaxIterationsExceededException, FunctionEvaluationException { 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return solve(f, min, max); 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@inheritDoc} 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated in 2.2 (to be removed in 3.0). 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double solve(final UnivariateRealFunction f, double min, double max, double initial) 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MaxIterationsExceededException, FunctionEvaluationException { 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return solve(f, min, max); 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double solve(int maxEval, final UnivariateRealFunction f, double min, double max, double initial) 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MaxIterationsExceededException, FunctionEvaluationException { 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return solve(maxEval, f, min, max); 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double solve(int maxEval, final UnivariateRealFunction f, double min, double max) 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MaxIterationsExceededException, FunctionEvaluationException { 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond setMaximalIterationCount(maxEval); 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return solve(f, min, max); 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@inheritDoc} 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated in 2.2 (to be removed in 3.0). 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Deprecated 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double solve(final UnivariateRealFunction f, double min, double max) 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MaxIterationsExceededException, FunctionEvaluationException { 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond clearResult(); 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond verifyInterval(min,max); 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double m; 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double fm; 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double fmin; 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int i = 0; 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond while (i < maximalIterationCount) { 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m = UnivariateRealSolverUtils.midpoint(min, max); 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond fmin = f.value(min); 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond fm = f.value(m); 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (fm * fmin > 0.0) { 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // max and m bracket the root. 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond min = m; 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } else { 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // min and m bracket the root. 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond max = m; 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (FastMath.abs(max - min) <= absoluteAccuracy) { 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m = UnivariateRealSolverUtils.midpoint(min, max); 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond setResult(m, i); 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return m; 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ++i; 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new MaxIterationsExceededException(maximalIterationCount); 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 134