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.solvers;
19
import org.apache.commons.math.MathException;
20
import org.apache.commons.math.analysis.Expm1Function;
21
import org.apache.commons.math.analysis.QuinticFunction;
22
import org.apache.commons.math.analysis.SinFunction;
23
import org.apache.commons.math.analysis.UnivariateRealFunction;
25
import junit.framework.TestCase;
28
* Testcase for Ridders solver.
30
* Ridders' method converges superlinearly, more specific, its rate of
31
* convergence is sqrt(2). Test runs show that for a default absolute
32
* accuracy of 1E-6, it generally takes less than 5 iterations for close
33
* initial bracket and 5 to 10 iterations for distant initial bracket
36
* @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
38
public final class RiddersSolverTest extends TestCase {
41
* Test the deprecated APIs.
44
public void testDeprecated() throws MathException {
45
UnivariateRealFunction f = new SinFunction();
46
UnivariateRealSolver solver = new RiddersSolver(f);
47
double min, max, expected, result, tolerance;
49
min = 3.0; max = 4.0; expected = Math.PI;
50
tolerance = Math.max(solver.getAbsoluteAccuracy(),
51
Math.abs(expected * solver.getRelativeAccuracy()));
52
result = solver.solve(min, max);
53
assertEquals(expected, result, tolerance);
55
min = -1.0; max = 1.5; expected = 0.0;
56
tolerance = Math.max(solver.getAbsoluteAccuracy(),
57
Math.abs(expected * solver.getRelativeAccuracy()));
58
result = solver.solve(min, max);
59
assertEquals(expected, result, tolerance);
63
* Test of solver for the sine function.
65
public void testSinFunction() throws MathException {
66
UnivariateRealFunction f = new SinFunction();
67
UnivariateRealSolver solver = new RiddersSolver();
68
double min, max, expected, result, tolerance;
70
min = 3.0; max = 4.0; expected = Math.PI;
71
tolerance = Math.max(solver.getAbsoluteAccuracy(),
72
Math.abs(expected * solver.getRelativeAccuracy()));
73
result = solver.solve(f, min, max);
74
assertEquals(expected, result, tolerance);
76
min = -1.0; max = 1.5; expected = 0.0;
77
tolerance = Math.max(solver.getAbsoluteAccuracy(),
78
Math.abs(expected * solver.getRelativeAccuracy()));
79
result = solver.solve(f, min, max);
80
assertEquals(expected, result, tolerance);
84
* Test of solver for the quintic function.
86
public void testQuinticFunction() throws MathException {
87
UnivariateRealFunction f = new QuinticFunction();
88
UnivariateRealSolver solver = new RiddersSolver();
89
double min, max, expected, result, tolerance;
91
min = -0.4; max = 0.2; expected = 0.0;
92
tolerance = Math.max(solver.getAbsoluteAccuracy(),
93
Math.abs(expected * solver.getRelativeAccuracy()));
94
result = solver.solve(f, min, max);
95
assertEquals(expected, result, tolerance);
97
min = 0.75; max = 1.5; expected = 1.0;
98
tolerance = Math.max(solver.getAbsoluteAccuracy(),
99
Math.abs(expected * solver.getRelativeAccuracy()));
100
result = solver.solve(f, min, max);
101
assertEquals(expected, result, tolerance);
103
min = -0.9; max = -0.2; expected = -0.5;
104
tolerance = Math.max(solver.getAbsoluteAccuracy(),
105
Math.abs(expected * solver.getRelativeAccuracy()));
106
result = solver.solve(f, min, max);
107
assertEquals(expected, result, tolerance);
111
* Test of solver for the exponential function.
113
public void testExpm1Function() throws MathException {
114
UnivariateRealFunction f = new Expm1Function();
115
UnivariateRealSolver solver = new RiddersSolver();
116
double min, max, expected, result, tolerance;
118
min = -1.0; max = 2.0; expected = 0.0;
119
tolerance = Math.max(solver.getAbsoluteAccuracy(),
120
Math.abs(expected * solver.getRelativeAccuracy()));
121
result = solver.solve(f, min, max);
122
assertEquals(expected, result, tolerance);
124
min = -20.0; max = 10.0; expected = 0.0;
125
tolerance = Math.max(solver.getAbsoluteAccuracy(),
126
Math.abs(expected * solver.getRelativeAccuracy()));
127
result = solver.solve(f, min, max);
128
assertEquals(expected, result, tolerance);
130
min = -50.0; max = 100.0; expected = 0.0;
131
tolerance = Math.max(solver.getAbsoluteAccuracy(),
132
Math.abs(expected * solver.getRelativeAccuracy()));
133
result = solver.solve(f, min, max);
134
assertEquals(expected, result, tolerance);
138
* Test of parameters for the solver.
140
public void testParameters() throws Exception {
141
UnivariateRealFunction f = new SinFunction();
142
UnivariateRealSolver solver = new RiddersSolver();
146
solver.solve(f, 1, -1);
147
fail("Expecting IllegalArgumentException - bad interval");
148
} catch (IllegalArgumentException ex) {
153
solver.solve(f, 2, 3);
154
fail("Expecting IllegalArgumentException - no bracketing");
155
} catch (IllegalArgumentException ex) {