001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.analysis.solvers;
018    
019    import org.apache.commons.math.ConvergenceException;
020    import org.apache.commons.math.FunctionEvaluationException;
021    import org.apache.commons.math.MaxIterationsExceededException;
022    import org.apache.commons.math.analysis.UnivariateRealFunction;
023    import org.apache.commons.math.util.MathUtils;
024    
025    /**
026     * Implements the <a href="http://mathworld.wolfram.com/RiddersMethod.html">
027     * Ridders' Method</a> for root finding of real univariate functions. For
028     * reference, see C. Ridders, <i>A new algorithm for computing a single root
029     * of a real continuous function </i>, IEEE Transactions on Circuits and
030     * Systems, 26 (1979), 979 - 980.
031     * <p>
032     * The function should be continuous but not necessarily smooth.</p>
033     *
034     * @version $Revision: 825919 $ $Date: 2009-10-16 10:51:55 -0400 (Fri, 16 Oct 2009) $
035     * @since 1.2
036     */
037    public class RiddersSolver extends UnivariateRealSolverImpl {
038    
039        /**
040         * Construct a solver for the given function.
041         *
042         * @param f function to solve
043         * @deprecated as of 2.0 the function to solve is passed as an argument
044         * to the {@link #solve(UnivariateRealFunction, double, double)} or
045         * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
046         * method.
047         */
048        @Deprecated
049        public RiddersSolver(UnivariateRealFunction f) {
050            super(f, 100, 1E-6);
051        }
052    
053        /**
054         * Construct a solver.
055         */
056        public RiddersSolver() {
057            super(100, 1E-6);
058        }
059    
060        /** {@inheritDoc} */
061        @Deprecated
062        public double solve(final double min, final double max)
063            throws ConvergenceException, FunctionEvaluationException {
064            return solve(f, min, max);
065        }
066    
067        /** {@inheritDoc} */
068        @Deprecated
069        public double solve(final double min, final double max, final double initial)
070            throws ConvergenceException, FunctionEvaluationException {
071            return solve(f, min, max, initial);
072        }
073    
074        /**
075         * Find a root in the given interval with initial value.
076         * <p>
077         * Requires bracketing condition.</p>
078         *
079         * @param f the function to solve
080         * @param min the lower bound for the interval
081         * @param max the upper bound for the interval
082         * @param initial the start value to use
083         * @return the point at which the function value is zero
084         * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
085         * @throws FunctionEvaluationException if an error occurs evaluating the
086         * function
087         * @throws IllegalArgumentException if any parameters are invalid
088         */
089        public double solve(final UnivariateRealFunction f,
090                            final double min, final double max, final double initial)
091            throws MaxIterationsExceededException, FunctionEvaluationException {
092    
093            // check for zeros before verifying bracketing
094            if (f.value(min) == 0.0) { return min; }
095            if (f.value(max) == 0.0) { return max; }
096            if (f.value(initial) == 0.0) { return initial; }
097    
098            verifyBracketing(min, max, f);
099            verifySequence(min, initial, max);
100            if (isBracketing(min, initial, f)) {
101                return solve(f, min, initial);
102            } else {
103                return solve(f, initial, max);
104            }
105        }
106    
107        /**
108         * Find a root in the given interval.
109         * <p>
110         * Requires bracketing condition.</p>
111         *
112         * @param f the function to solve
113         * @param min the lower bound for the interval
114         * @param max the upper bound for the interval
115         * @return the point at which the function value is zero
116         * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
117         * @throws FunctionEvaluationException if an error occurs evaluating the
118         * function
119         * @throws IllegalArgumentException if any parameters are invalid
120         */
121        public double solve(final UnivariateRealFunction f,
122                            final double min, final double max)
123            throws MaxIterationsExceededException, FunctionEvaluationException {
124    
125            // [x1, x2] is the bracketing interval in each iteration
126            // x3 is the midpoint of [x1, x2]
127            // x is the new root approximation and an endpoint of the new interval
128            double x1 = min;
129            double y1 = f.value(x1);
130            double x2 = max;
131            double y2 = f.value(x2);
132    
133            // check for zeros before verifying bracketing
134            if (y1 == 0.0) {
135                return min;
136            }
137            if (y2 == 0.0) {
138                return max;
139            }
140            verifyBracketing(min, max, f);
141    
142            int i = 1;
143            double oldx = Double.POSITIVE_INFINITY;
144            while (i <= maximalIterationCount) {
145                // calculate the new root approximation
146                final double x3 = 0.5 * (x1 + x2);
147                final double y3 = f.value(x3);
148                if (Math.abs(y3) <= functionValueAccuracy) {
149                    setResult(x3, i);
150                    return result;
151                }
152                final double delta = 1 - (y1 * y2) / (y3 * y3);  // delta > 1 due to bracketing
153                final double correction = (MathUtils.sign(y2) * MathUtils.sign(y3)) *
154                                          (x3 - x1) / Math.sqrt(delta);
155                final double x = x3 - correction;                // correction != 0
156                final double y = f.value(x);
157    
158                // check for convergence
159                final double tolerance = Math.max(relativeAccuracy * Math.abs(x), absoluteAccuracy);
160                if (Math.abs(x - oldx) <= tolerance) {
161                    setResult(x, i);
162                    return result;
163                }
164                if (Math.abs(y) <= functionValueAccuracy) {
165                    setResult(x, i);
166                    return result;
167                }
168    
169                // prepare the new interval for next iteration
170                // Ridders' method guarantees x1 < x < x2
171                if (correction > 0.0) {             // x1 < x < x3
172                    if (MathUtils.sign(y1) + MathUtils.sign(y) == 0.0) {
173                        x2 = x;
174                        y2 = y;
175                    } else {
176                        x1 = x;
177                        x2 = x3;
178                        y1 = y;
179                        y2 = y3;
180                    }
181                } else {                            // x3 < x < x2
182                    if (MathUtils.sign(y2) + MathUtils.sign(y) == 0.0) {
183                        x1 = x;
184                        y1 = y;
185                    } else {
186                        x1 = x3;
187                        x2 = x;
188                        y1 = y3;
189                        y2 = y;
190                    }
191                }
192                oldx = x;
193                i++;
194            }
195            throw new MaxIterationsExceededException(maximalIterationCount);
196        }
197    }