KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > analysis > NewtonSolverTest


1 /*
2  *
3  * Copyright (c) 2003-2004 The Apache Software Foundation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  *
17  */

18 package org.apache.commons.math.analysis;
19
20 import org.apache.commons.math.MathException;
21
22 import junit.framework.TestCase;
23
24 /**
25  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
26  */

27 public final class NewtonSolverTest extends TestCase {
28     /**
29      *
30      */

31     public void testSinZero() throws MathException {
32         DifferentiableUnivariateRealFunction f = new SinFunction();
33         double result;
34         
35         UnivariateRealSolver solver = new NewtonSolver(f);
36         result = solver.solve(3, 4);
37         assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
38
39         result = solver.solve(1, 4);
40         assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
41         
42         //TODO: create abstract solver test class, move these there
43
assertEquals(result, solver.getResult(), 0);
44         assertTrue(solver.getIterationCount() > 0);
45     }
46
47     /**
48      *
49      */

50     public void testQuinticZero() throws MathException {
51         DifferentiableUnivariateRealFunction f = new QuinticFunction();
52         double result;
53
54         UnivariateRealSolver solver = new BisectionSolver(f);
55         result = solver.solve(-0.2, 0.2);
56         assertEquals(result, 0, solver.getAbsoluteAccuracy());
57
58         result = solver.solve(-0.1, 0.3);
59         assertEquals(result, 0, solver.getAbsoluteAccuracy());
60
61         result = solver.solve(-0.3, 0.45);
62         assertEquals(result, 0, solver.getAbsoluteAccuracy());
63
64         result = solver.solve(0.3, 0.7);
65         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
66
67         result = solver.solve(0.2, 0.6);
68         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
69
70         result = solver.solve(0.05, 0.95);
71         assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
72
73         result = solver.solve(0.85, 1.25);
74         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
75
76         result = solver.solve(0.8, 1.2);
77         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
78
79         result = solver.solve(0.85, 1.75);
80         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
81
82         result = solver.solve(0.55, 1.45);
83         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
84
85         result = solver.solve(0.85, 5);
86         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
87     }
88 }
89
Popular Tags