1 18 package org.apache.commons.math.analysis; 19 20 import org.apache.commons.math.MathException; 21 22 import junit.framework.TestCase; 23 24 27 public final class NewtonSolverTest extends TestCase { 28 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 assertEquals(result, solver.getResult(), 0); 44 assertTrue(solver.getIterationCount() > 0); 45 } 46 47 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 |