1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.math.analysis.solvers;
18
19 import org.apache.commons.math.ConvergenceException;
20 import org.apache.commons.math.FunctionEvaluationException;
21 import org.apache.commons.math.MathRuntimeException;
22 import org.apache.commons.math.MaxIterationsExceededException;
23 import org.apache.commons.math.analysis.UnivariateRealFunction;
24
25
26 /**
27 * Implements a modified version of the
28 * <a href="http://mathworld.wolfram.com/SecantMethod.html">secant method</a>
29 * for approximating a zero of a real univariate function.
30 * <p>
31 * The algorithm is modified to maintain bracketing of a root by successive
32 * approximations. Because of forced bracketing, convergence may be slower than
33 * the unrestricted secant algorithm. However, this implementation should in
34 * general outperform the
35 * <a href="http://mathworld.wolfram.com/MethodofFalsePosition.html">
36 * regula falsi method.</a></p>
37 * <p>
38 * The function is assumed to be continuous but not necessarily smooth.</p>
39 *
40 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
41 */
42 public class SecantSolver extends UnivariateRealSolverImpl {
43
44 /**
45 * Construct a solver for the given function.
46 * @param f function to solve.
47 * @deprecated as of 2.0 the function to solve is passed as an argument
48 * to the {@link #solve(UnivariateRealFunction, double, double)} or
49 * {@link UnivariateRealSolverImpl#solve(UnivariateRealFunction, double, double, double)}
50 * method.
51 */
52 @Deprecated
53 public SecantSolver(UnivariateRealFunction f) {
54 super(f, 100, 1E-6);
55 }
56
57 /**
58 * Construct a solver.
59 */
60 public SecantSolver() {
61 super(100, 1E-6);
62 }
63
64 /** {@inheritDoc} */
65 @Deprecated
66 public double solve(final double min, final double max)
67 throws ConvergenceException, FunctionEvaluationException {
68 return solve(f, min, max);
69 }
70
71 /** {@inheritDoc} */
72 @Deprecated
73 public double solve(final double min, final double max, final double initial)
74 throws ConvergenceException, FunctionEvaluationException {
75 return solve(f, min, max, initial);
76 }
77
78 /**
79 * Find a zero in the given interval.
80 *
81 * @param f the function to solve
82 * @param min the lower bound for the interval
83 * @param max the upper bound for the interval
84 * @param initial the start value to use (ignored)
85 * @return the value where the function is zero
86 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
87 * @throws FunctionEvaluationException if an error occurs evaluating the
88 * function
89 * @throws IllegalArgumentException if min is not less than max or the
90 * signs of the values of the function at the endpoints are not opposites
91 */
92 public double solve(final UnivariateRealFunction f,
93 final double min, final double max, final double initial)
94 throws MaxIterationsExceededException, FunctionEvaluationException {
95 return solve(f, min, max);
96 }
97
98 /**
99 * 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 }