1 16 package org.apache.commons.math.analysis; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.math.ConvergenceException; 21 import org.apache.commons.math.FunctionEvaluationException; 22 23 24 40 public class SecantSolver extends UnivariateRealSolverImpl implements Serializable { 41 42 43 static final long serialVersionUID = 1984971194738974867L; 44 45 49 public SecantSolver(UnivariateRealFunction f) { 50 super(f, 100, 1E-6); 51 } 52 53 66 public double solve(double min, double max, double initial) 67 throws ConvergenceException, FunctionEvaluationException { 68 69 return solve(min, max); 70 } 71 72 83 public double solve(double min, double max) throws ConvergenceException, 84 FunctionEvaluationException { 85 86 clearResult(); 87 verifyInterval(min, max); 88 89 double x0 = min; 95 double x1 = max; 96 double y0 = f.value(x0); 97 double y1 = f.value(x1); 98 99 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) < 125 Math.max(relativeAccuracy * Math.abs(x1), absoluteAccuracy)) { 126 setResult(x1, i); 127 return result; 128 } 129 double delta; 130 if (Math.abs(y1) > Math.abs(y0)) { 131 delta = 0.5 * oldDelta; 133 } else { 134 delta = (x0 - x1) / (1 - y0 / y1); 135 if (delta / oldDelta > 1) { 136 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 x2 = x0; 148 y2 = y0; 149 } 150 oldDelta = x2 - x1; 151 i++; 152 } 153 throw new ConvergenceException("Maximal iteration number exceeded" + i); 154 } 155 156 } 157 | Popular Tags |