001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.analysis;
017:
018: import java.io.Serializable;
019:
020: import org.apache.commons.math.ConvergenceException;
021: import org.apache.commons.math.FunctionEvaluationException;
022:
023: /**
024: * Implements a modified version of the
025: * <a href="http://mathworld.wolfram.com/SecantMethod.html">secant method</a>
026: * for approximating a zero of a real univariate function.
027: * <p>
028: * The algorithm is modified to maintain bracketing of a root by successive
029: * approximations. Because of forced bracketing, convergence may be slower than
030: * the unrestricted secant algorithm. However, this implementation should in
031: * general outperform the
032: * <a href="http://mathworld.wolfram.com/MethodofFalsePosition.html">
033: * regula falsi method.</a>
034: * <p>
035: * The function is assumed to be continuous but not necessarily smooth.
036: *
037: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
038: */
039: public class SecantSolver extends UnivariateRealSolverImpl implements
040: Serializable {
041:
042: /** Serializable version identifier */
043: private static final long serialVersionUID = 1984971194738974867L;
044:
045: /**
046: * Construct a solver for the given function.
047: * @param f function to solve.
048: */
049: public SecantSolver(UnivariateRealFunction f) {
050: super (f, 100, 1E-6);
051: }
052:
053: /**
054: * Find a zero in the given interval.
055: *
056: * @param min the lower bound for the interval
057: * @param max the upper bound for the interval
058: * @param initial the start value to use (ignored)
059: * @return the value where the function is zero
060: * @throws ConvergenceException if the maximum iteration count is exceeded
061: * @throws FunctionEvaluationException if an error occurs evaluating the
062: * function
063: * @throws IllegalArgumentException if min is not less than max or the
064: * signs of the values of the function at the endpoints are not opposites
065: */
066: public double solve(double min, double max, double initial)
067: throws ConvergenceException, FunctionEvaluationException {
068:
069: return solve(min, max);
070: }
071:
072: /**
073: * Find a zero in the given interval.
074: * @param min the lower bound for the interval.
075: * @param max the upper bound for the interval.
076: * @return the value where the function is zero
077: * @throws ConvergenceException if the maximum iteration count is exceeded
078: * @throws FunctionEvaluationException if an error occurs evaluating the
079: * function
080: * @throws IllegalArgumentException if min is not less than max or the
081: * signs of the values of the function at the endpoints are not opposites
082: */
083: public double solve(double min, double max)
084: throws ConvergenceException, FunctionEvaluationException {
085:
086: clearResult();
087: verifyInterval(min, max);
088:
089: // Index 0 is the old approximation for the root.
090: // Index 1 is the last calculated approximation for the root.
091: // Index 2 is a bracket for the root with respect to x0.
092: // OldDelta is the length of the bracketing interval of the last
093: // iteration.
094: double x0 = min;
095: double x1 = max;
096: double y0 = f.value(x0);
097: double y1 = f.value(x1);
098:
099: // Verify bracketing
100: if (y0 * y1 >= 0) {
101: throw new IllegalArgumentException(
102: "Function values at endpoints do not have different signs."
103: + " Endpoints: [" + min + "," + max + "]"
104: + " Values: [" + y0 + "," + y1 + "]");
105: }
106:
107: double x2 = x0;
108: double y2 = y0;
109: double oldDelta = x2 - x1;
110: int i = 0;
111: while (i < maximalIterationCount) {
112: if (Math.abs(y2) < Math.abs(y1)) {
113: x0 = x1;
114: x1 = x2;
115: x2 = x0;
116: y0 = y1;
117: y1 = y2;
118: y2 = y0;
119: }
120: if (Math.abs(y1) <= functionValueAccuracy) {
121: setResult(x1, i);
122: return result;
123: }
124: if (Math.abs(oldDelta) < Math.max(relativeAccuracy
125: * Math.abs(x1), absoluteAccuracy)) {
126: setResult(x1, i);
127: return result;
128: }
129: double delta;
130: if (Math.abs(y1) > Math.abs(y0)) {
131: // Function value increased in last iteration. Force bisection.
132: delta = 0.5 * oldDelta;
133: } else {
134: delta = (x0 - x1) / (1 - y0 / y1);
135: if (delta / oldDelta > 1) {
136: // New approximation falls outside bracket.
137: // Fall back to bisection.
138: delta = 0.5 * oldDelta;
139: }
140: }
141: x0 = x1;
142: y0 = y1;
143: x1 = x1 + delta;
144: y1 = f.value(x1);
145: if ((y1 > 0) == (y2 > 0)) {
146: // New bracket is (x0,x1).
147: x2 = x0;
148: y2 = y0;
149: }
150: oldDelta = x2 - x1;
151: i++;
152: }
153: throw new ConvergenceException(
154: "Maximal iteration number exceeded" + i);
155: }
156:
157: }
|