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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.analysis;
19
import org.apache.commons.math.FunctionEvaluationException;
20
import org.apache.commons.math.MaxIterationsExceededException;
21
import org.apache.commons.math.util.MathUtils;
24
* Implements the <a href="http://mathworld.wolfram.com/RiddersMethod.html">
25
* Ridders' Method</a> for root finding of real univariate functions. For
26
* reference, see C. Ridders, <i>A new algorithm for computing a single root
27
* of a real continuous function </i>, IEEE Transactions on Circuits and
28
* Systems, 26 (1979), 979 - 980.
30
* The function should be continuous but not necessarily smooth.</p>
32
* @version $Revision: 620312 $ $Date: 2008-02-10 12:28:59 -0700 (Sun, 10 Feb 2008) $
35
public class RiddersSolver extends UnivariateRealSolverImpl {
37
/** serializable version identifier */
38
private static final long serialVersionUID = -4703139035737911735L;
41
* Construct a solver for the given function.
43
* @param f function to solve
45
public RiddersSolver(UnivariateRealFunction f) {
50
* Find a root in the given interval with initial value.
52
* Requires bracketing condition.</p>
54
* @param min the lower bound for the interval
55
* @param max the upper bound for the interval
56
* @param initial the start value to use
57
* @return the point at which the function value is zero
58
* @throws MaxIterationsExceededException if the maximum iteration count is exceeded
59
* @throws FunctionEvaluationException if an error occurs evaluating the
61
* @throws IllegalArgumentException if any parameters are invalid
63
public double solve(double min, double max, double initial) throws
64
MaxIterationsExceededException, FunctionEvaluationException {
66
// check for zeros before verifying bracketing
67
if (f.value(min) == 0.0) { return min; }
68
if (f.value(max) == 0.0) { return max; }
69
if (f.value(initial) == 0.0) { return initial; }
71
verifyBracketing(min, max, f);
72
verifySequence(min, initial, max);
73
if (isBracketing(min, initial, f)) {
74
return solve(min, initial);
76
return solve(initial, max);
81
* Find a root in the given interval.
83
* Requires bracketing condition.</p>
85
* @param min the lower bound for the interval
86
* @param max the upper bound for the interval
87
* @return the point at which the function value is zero
88
* @throws MaxIterationsExceededException if the maximum iteration count is exceeded
89
* @throws FunctionEvaluationException if an error occurs evaluating the
91
* @throws IllegalArgumentException if any parameters are invalid
93
public double solve(double min, double max) throws MaxIterationsExceededException,
94
FunctionEvaluationException {
96
// [x1, x2] is the bracketing interval in each iteration
97
// x3 is the midpoint of [x1, x2]
98
// x is the new root approximation and an endpoint of the new interval
99
double x1, x2, x3, x, oldx, y1, y2, y3, y, delta, correction, tolerance;
101
x1 = min; y1 = f.value(x1);
102
x2 = max; y2 = f.value(x2);
104
// check for zeros before verifying bracketing
105
if (y1 == 0.0) { return min; }
106
if (y2 == 0.0) { return max; }
107
verifyBracketing(min, max, f);
110
oldx = Double.POSITIVE_INFINITY;
111
while (i <= maximalIterationCount) {
112
// calculate the new root approximation
113
x3 = 0.5 * (x1 + x2);
115
if (Math.abs(y3) <= functionValueAccuracy) {
119
delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing
120
correction = (MathUtils.sign(y2) * MathUtils.sign(y3)) *
121
(x3 - x1) / Math.sqrt(delta);
122
x = x3 - correction; // correction != 0
125
// check for convergence
126
tolerance = Math.max(relativeAccuracy * Math.abs(x), absoluteAccuracy);
127
if (Math.abs(x - oldx) <= tolerance) {
131
if (Math.abs(y) <= functionValueAccuracy) {
136
// prepare the new interval for next iteration
137
// Ridders' method guarantees x1 < x < x2
138
if (correction > 0.0) { // x1 < x < x3
139
if (MathUtils.sign(y1) + MathUtils.sign(y) == 0.0) {
145
} else { // x3 < x < x2
146
if (MathUtils.sign(y2) + MathUtils.sign(y) == 0.0) {
156
throw new MaxIterationsExceededException(maximalIterationCount);