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.MathRuntimeException;
022    import org.apache.commons.math.MaxIterationsExceededException;
023    import org.apache.commons.math.analysis.UnivariateRealFunction;
024    
025    
026    /**
027     * Implements a modified version of the
028     * <a href="http://mathworld.wolfram.com/SecantMethod.html">secant method</a>
029     * for approximating a zero of a real univariate function.
030     * <p>
031     * The algorithm is modified to maintain bracketing of a root by successive
032     * approximations. Because of forced bracketing, convergence may be slower than
033     * the unrestricted secant algorithm. However, this implementation should in
034     * general outperform the
035     * <a href="http://mathworld.wolfram.com/MethodofFalsePosition.html">
036     * regula falsi method.</a></p>
037     * <p>
038     * The function is assumed to be continuous but not necessarily smooth.</p>
039     *
040     * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
041     */
042    public class SecantSolver extends UnivariateRealSolverImpl {
043    
044        /**
045         * Construct a solver for the given function.
046         * @param f function to solve.
047         * @deprecated as of 2.0 the function to solve is passed as an argument
048         * to the {@link #solve(UnivariateRealFunction, double, double)} or
049         * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
050         * method.
051         */
052        @Deprecated
053        public SecantSolver(UnivariateRealFunction f) {
054            super(f, 100, 1E-6);
055        }
056    
057        /**
058         * Construct a solver.
059         */
060        public SecantSolver() {
061            super(100, 1E-6);
062        }
063    
064        /** {@inheritDoc} */
065        @Deprecated
066        public double solve(final double min, final double max)
067            throws ConvergenceException, FunctionEvaluationException {
068            return solve(f, min, max);
069        }
070    
071        /** {@inheritDoc} */
072        @Deprecated
073        public double solve(final double min, final double max, final double initial)
074            throws ConvergenceException, FunctionEvaluationException {
075            return solve(f, min, max, initial);
076        }
077    
078        /**
079         * Find a zero in the given interval.
080         *
081         * @param f the function to solve
082         * @param min the lower bound for the interval
083         * @param max the upper bound for the interval
084         * @param initial the start value to use (ignored)
085         * @return the value where the function is zero
086         * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
087         * @throws FunctionEvaluationException if an error occurs evaluating the
088         * function
089         * @throws IllegalArgumentException if min is not less than max or the
090         * signs of the values of the function at the endpoints are not opposites
091         */
092        public double solve(final UnivariateRealFunction f,
093                            final double min, final double max, final double initial)
094            throws MaxIterationsExceededException, FunctionEvaluationException {
095            return solve(f, min, max);
096        }
097    
098        /**
099         * Find a zero in the given interval.
100         * @param f the function to solve
101         * @param min the lower bound for the interval.
102         * @param max the upper bound for the interval.
103         * @return the value where the function is zero
104         * @throws MaxIterationsExceededException  if the maximum iteration count is exceeded
105         * @throws FunctionEvaluationException if an error occurs evaluating the
106         * function
107         * @throws IllegalArgumentException if min is not less than max or the
108         * signs of the values of the function at the endpoints are not opposites
109         */
110        public double solve(final UnivariateRealFunction f,
111                            final double min, final double max)
112            throws MaxIterationsExceededException, FunctionEvaluationException {
113    
114            clearResult();
115            verifyInterval(min, max);
116    
117            // Index 0 is the old approximation for the root.
118            // Index 1 is the last calculated approximation  for the root.
119            // Index 2 is a bracket for the root with respect to x0.
120            // OldDelta is the length of the bracketing interval of the last
121            // iteration.
122            double x0 = min;
123            double x1 = max;
124            double y0 = f.value(x0);
125            double y1 = f.value(x1);
126    
127            // Verify bracketing
128            if (y0 * y1 >= 0) {
129                throw MathRuntimeException.createIllegalArgumentException(
130                      "function values at endpoints do not have different signs, " +
131                      "endpoints: [{0}, {1}], values: [{2}, {3}]",
132                      min, max, y0, y1);
133            }
134    
135            double x2 = x0;
136            double y2 = y0;
137            double oldDelta = x2 - x1;
138            int i = 0;
139            while (i < maximalIterationCount) {
140                if (Math.abs(y2) < Math.abs(y1)) {
141                    x0 = x1;
142                    x1 = x2;
143                    x2 = x0;
144                    y0 = y1;
145                    y1 = y2;
146                    y2 = y0;
147                }
148                if (Math.abs(y1) <= functionValueAccuracy) {
149                    setResult(x1, i);
150                    return result;
151                }
152                if (Math.abs(oldDelta) <
153                    Math.max(relativeAccuracy * Math.abs(x1), absoluteAccuracy)) {
154                    setResult(x1, i);
155                    return result;
156                }
157                double delta;
158                if (Math.abs(y1) > Math.abs(y0)) {
159                    // Function value increased in last iteration. Force bisection.
160                    delta = 0.5 * oldDelta;
161                } else {
162                    delta = (x0 - x1) / (1 - y0 / y1);
163                    if (delta / oldDelta > 1) {
164                        // New approximation falls outside bracket.
165                        // Fall back to bisection.
166                        delta = 0.5 * oldDelta;
167                    }
168                }
169                x0 = x1;
170                y0 = y1;
171                x1 = x1 + delta;
172                y1 = f.value(x1);
173                if ((y1 > 0) == (y2 > 0)) {
174                    // New bracket is (x0,x1).
175                    x2 = x0;
176                    y2 = y0;
177                }
178                oldDelta = x2 - x1;
179                i++;
180            }
181            throw new MaxIterationsExceededException(maximalIterationCount);
182        }
183    
184    }