Question: An important problem in numerical analysis is to find a solution to the equation f(x) = 0 for some arbitrary f. If the function is
An important problem in numerical analysis is to find a solution to the equation f(x) = 0 for some arbitrary f. If the function is continuous and has two points low and high such that f(low) and f(high) have opposite signs, then a root must exist between low and high and can be found by a binary search. Write a function that takes as parameters f, low, high and solves for a zero. (To implement a generic function as a parameter, pass a function object that implements the Function interface, which you can define to contain a single method f.) What must you do to ensure termination? In this assignment you are writing a method that uses Binary Search to find the zero of a function. Name the class that holds your method NumericalAnalysis and name the method that solves for zero solveForZero(...). I have also posted the JUnit test that I will use to test your program. You can modify it to match whichever function you choose to use to test your program
(Programming Language JAVA)
/* * JUnit test for the exercise 2.18 from * the textbook. */
import static org.junit.Assert.*; // Import the annotations that JUnit tests can use import org.junit.Test; import org.junit.Before; import org.junit.BeforeClass; import org.junit.After; import org.junit.AfterClass;
// @Test flags a method as a test method. // @Before indicates that a method will be run before every // test method is run. // @BeforeClass indicates that a method will be run once before // any of the other methods in the test suite are run. // @After indicates that a method will be run after every // test method is run. // @AfterClass indicates that a method will be run once after // all the other methods in the test suite finish..
public class TestNumericalAnalysis{ private NumericalAnalysis myAnalyzer;
// Methods flagged with the @Before annotation will be @Before // before every test method is run. public void setUp(){ myAnalyzer = new NumericalAnalysis(); }
// Methods flagged with the @Test annotation are the @Test // test methods and will run when the JUnit test is run public void testSolveForZero(){ double ans = myAnalyzer.solveForZero(new SomeFunction(), 0.0, 100.0); assertEquals(1.0, ans, 0.001); // check that the answer returned // is == (1 +- 0.01) }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
