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.MathException;
20
import junit.framework.TestCase;
23
* Testcase for Muller solver.
25
* Muller's method converges almost quadratically near roots, but it can
26
* be very slow in regions far away from zeros. Test runs show that for
27
* reasonably good initial values, for a default absolute accuracy of 1E-6,
28
* it generally takes 5 to 10 iterations for the solver to converge.
30
* Tests for the exponential function illustrate the situations where
31
* Muller solver performs poorly.
33
* @version $Revision$ $Date$
35
public final class MullerSolverTest extends TestCase {
38
* Test of solver for the sine function.
40
public void testSinFunction() throws MathException {
41
UnivariateRealFunction f = new SinFunction();
42
UnivariateRealSolver solver = new MullerSolver(f);
43
double min, max, expected, result, tolerance;
45
min = 3.0; max = 4.0; expected = Math.PI;
46
tolerance = Math.max(solver.getAbsoluteAccuracy(),
47
Math.abs(expected * solver.getRelativeAccuracy()));
48
result = solver.solve(min, max);
49
assertEquals(expected, result, tolerance);
51
min = -1.0; max = 1.5; expected = 0.0;
52
tolerance = Math.max(solver.getAbsoluteAccuracy(),
53
Math.abs(expected * solver.getRelativeAccuracy()));
54
result = solver.solve(min, max);
55
assertEquals(expected, result, tolerance);
59
* Test of solver for the sine function using solve2().
61
public void testSinFunction2() throws MathException {
62
UnivariateRealFunction f = new SinFunction();
63
MullerSolver solver = new MullerSolver(f);
64
double min, max, expected, result, tolerance;
66
min = 3.0; max = 4.0; expected = Math.PI;
67
tolerance = Math.max(solver.getAbsoluteAccuracy(),
68
Math.abs(expected * solver.getRelativeAccuracy()));
69
result = solver.solve2(min, max);
70
assertEquals(expected, result, tolerance);
72
min = -1.0; max = 1.5; expected = 0.0;
73
tolerance = Math.max(solver.getAbsoluteAccuracy(),
74
Math.abs(expected * solver.getRelativeAccuracy()));
75
result = solver.solve2(min, max);
76
assertEquals(expected, result, tolerance);
80
* Test of solver for the quintic function.
82
public void testQuinticFunction() throws MathException {
83
UnivariateRealFunction f = new QuinticFunction();
84
UnivariateRealSolver solver = new MullerSolver(f);
85
double min, max, expected, result, tolerance;
87
min = -0.4; max = 0.2; expected = 0.0;
88
tolerance = Math.max(solver.getAbsoluteAccuracy(),
89
Math.abs(expected * solver.getRelativeAccuracy()));
90
result = solver.solve(min, max);
91
assertEquals(expected, result, tolerance);
93
min = 0.75; max = 1.5; expected = 1.0;
94
tolerance = Math.max(solver.getAbsoluteAccuracy(),
95
Math.abs(expected * solver.getRelativeAccuracy()));
96
result = solver.solve(min, max);
97
assertEquals(expected, result, tolerance);
99
min = -0.9; max = -0.2; expected = -0.5;
100
tolerance = Math.max(solver.getAbsoluteAccuracy(),
101
Math.abs(expected * solver.getRelativeAccuracy()));
102
result = solver.solve(min, max);
103
assertEquals(expected, result, tolerance);
107
* Test of solver for the quintic function using solve2().
109
public void testQuinticFunction2() throws MathException {
110
UnivariateRealFunction f = new QuinticFunction();
111
MullerSolver solver = new MullerSolver(f);
112
double min, max, expected, result, tolerance;
114
min = -0.4; max = 0.2; expected = 0.0;
115
tolerance = Math.max(solver.getAbsoluteAccuracy(),
116
Math.abs(expected * solver.getRelativeAccuracy()));
117
result = solver.solve2(min, max);
118
assertEquals(expected, result, tolerance);
120
min = 0.75; max = 1.5; expected = 1.0;
121
tolerance = Math.max(solver.getAbsoluteAccuracy(),
122
Math.abs(expected * solver.getRelativeAccuracy()));
123
result = solver.solve2(min, max);
124
assertEquals(expected, result, tolerance);
126
min = -0.9; max = -0.2; expected = -0.5;
127
tolerance = Math.max(solver.getAbsoluteAccuracy(),
128
Math.abs(expected * solver.getRelativeAccuracy()));
129
result = solver.solve2(min, max);
130
assertEquals(expected, result, tolerance);
134
* Test of solver for the exponential function.
136
* It takes 10 to 15 iterations for the last two tests to converge.
137
* In fact, if not for the bisection alternative, the solver would
138
* exceed the default maximal iteration of 100.
140
public void testExpm1Function() throws MathException {
141
UnivariateRealFunction f = new Expm1Function();
142
UnivariateRealSolver solver = new MullerSolver(f);
143
double min, max, expected, result, tolerance;
145
min = -1.0; max = 2.0; expected = 0.0;
146
tolerance = Math.max(solver.getAbsoluteAccuracy(),
147
Math.abs(expected * solver.getRelativeAccuracy()));
148
result = solver.solve(min, max);
149
assertEquals(expected, result, tolerance);
151
min = -20.0; max = 10.0; expected = 0.0;
152
tolerance = Math.max(solver.getAbsoluteAccuracy(),
153
Math.abs(expected * solver.getRelativeAccuracy()));
154
result = solver.solve(min, max);
155
assertEquals(expected, result, tolerance);
157
min = -50.0; max = 100.0; expected = 0.0;
158
tolerance = Math.max(solver.getAbsoluteAccuracy(),
159
Math.abs(expected * solver.getRelativeAccuracy()));
160
result = solver.solve(min, max);
161
assertEquals(expected, result, tolerance);
165
* Test of solver for the exponential function using solve2().
167
* It takes 25 to 50 iterations for the last two tests to converge.
169
public void testExpm1Function2() throws MathException {
170
UnivariateRealFunction f = new Expm1Function();
171
MullerSolver solver = new MullerSolver(f);
172
double min, max, expected, result, tolerance;
174
min = -1.0; max = 2.0; expected = 0.0;
175
tolerance = Math.max(solver.getAbsoluteAccuracy(),
176
Math.abs(expected * solver.getRelativeAccuracy()));
177
result = solver.solve2(min, max);
178
assertEquals(expected, result, tolerance);
180
min = -20.0; max = 10.0; expected = 0.0;
181
tolerance = Math.max(solver.getAbsoluteAccuracy(),
182
Math.abs(expected * solver.getRelativeAccuracy()));
183
result = solver.solve2(min, max);
184
assertEquals(expected, result, tolerance);
186
min = -50.0; max = 100.0; expected = 0.0;
187
tolerance = Math.max(solver.getAbsoluteAccuracy(),
188
Math.abs(expected * solver.getRelativeAccuracy()));
189
result = solver.solve2(min, max);
190
assertEquals(expected, result, tolerance);
194
* Test of parameters for the solver.
196
public void testParameters() throws Exception {
197
UnivariateRealFunction f = new SinFunction();
198
UnivariateRealSolver solver = new MullerSolver(f);
203
fail("Expecting IllegalArgumentException - bad interval");
204
} catch (IllegalArgumentException ex) {
210
fail("Expecting IllegalArgumentException - no bracketing");
211
} catch (IllegalArgumentException ex) {