001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math.analysis.solvers; 018 019 import org.apache.commons.math.MathException; 020 import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; 021 import org.apache.commons.math.analysis.QuinticFunction; 022 import org.apache.commons.math.analysis.SinFunction; 023 024 025 import junit.framework.TestCase; 026 027 /** 028 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $ 029 */ 030 public final class NewtonSolverTest extends TestCase { 031 032 @Deprecated 033 public void testDeprecated() throws MathException { 034 DifferentiableUnivariateRealFunction f = new SinFunction(); 035 double result; 036 037 UnivariateRealSolver solver = new NewtonSolver(f); 038 result = solver.solve(3, 4); 039 assertEquals(result, Math.PI, solver.getAbsoluteAccuracy()); 040 041 result = solver.solve(1, 4); 042 assertEquals(result, Math.PI, solver.getAbsoluteAccuracy()); 043 044 assertEquals(result, solver.getResult(), 0); 045 assertTrue(solver.getIterationCount() > 0); 046 } 047 048 /** 049 * 050 */ 051 public void testSinZero() throws MathException { 052 DifferentiableUnivariateRealFunction f = new SinFunction(); 053 double result; 054 055 UnivariateRealSolver solver = new NewtonSolver(); 056 result = solver.solve(f, 3, 4); 057 assertEquals(result, Math.PI, solver.getAbsoluteAccuracy()); 058 059 result = solver.solve(f, 1, 4); 060 assertEquals(result, Math.PI, solver.getAbsoluteAccuracy()); 061 062 assertEquals(result, solver.getResult(), 0); 063 assertTrue(solver.getIterationCount() > 0); 064 } 065 066 /** 067 * 068 */ 069 public void testQuinticZero() throws MathException { 070 DifferentiableUnivariateRealFunction f = new QuinticFunction(); 071 double result; 072 073 UnivariateRealSolver solver = new NewtonSolver(); 074 result = solver.solve(f, -0.2, 0.2); 075 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 076 077 result = solver.solve(f, -0.1, 0.3); 078 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 079 080 result = solver.solve(f, -0.3, 0.45); 081 assertEquals(result, 0, solver.getAbsoluteAccuracy()); 082 083 result = solver.solve(f, 0.3, 0.7); 084 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 085 086 result = solver.solve(f, 0.2, 0.6); 087 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 088 089 result = solver.solve(f, 0.05, 0.95); 090 assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); 091 092 result = solver.solve(f, 0.85, 1.25); 093 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 094 095 result = solver.solve(f, 0.8, 1.2); 096 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 097 098 result = solver.solve(f, 0.85, 1.75); 099 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 100 101 result = solver.solve(f, 0.55, 1.45); 102 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 103 104 result = solver.solve(f, 0.85, 5); 105 assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); 106 } 107 108 }