Question: import java.util.function.Function; @FunctionalInterface interface Func { double apply ( double x ) ; } / * * * Find a root for a continuous, real

import java.util.function.Function;
@FunctionalInterface
interface Func {
double apply(double x);
}
/**
* Find a root for a continuous, real value function f over a closed
* interval [a,b] in which f(a)* f(b)0.
*
* @author
*
*/
public class RootFinder {
// Used to determine if doubles are equal
public static final double EPSILON =1e-15;
Func f;
double a;
double b;
double[] ai;
double[] bi;
double[] pi;
int iterationsExecuted;
/**
* Initialize a RootFinder with the interval and function. It is assumed that
* a b and f is a continuous function on a, b with f(a)* f(b)0.
*
* @param a the left endpoint of a closed interval of the domain of f
* @param b the right endpoint of a closed interval of the domain of f
* @param f a continuous function such that f(a)* f(b)0
* @throws Execption if end points of interval are out of order
* @throws Exception if f(a)*f(b)>=0
*/
public RootFinder(double a, double b, Func f) throws Exception {
this.a = a;
this.b = b;
this.f = f;
// Check order of a and b
}
/**
* Run the Bisection Method algorithm to determine a root of f.
* Both tolerance and N must be positive.
*
* @param tolerance absolute error bound on the zero and p
* @param N the maximum number of iterations of the bisection method
* @return a zero of f
* @throws Exception thrown if a root is not found in N iterations
* @throws Execption thrown if N is not positive
* @throws Exception thrown if tolerance is not positive
*/
public double runBisectionMethod(double tolerance, int N) throws Exception{
/* First implement the method without saving the intermediate values
* and then store ai, bi and pi in the arrays.
*/
/*
* ALG Bisection Method(a, b, f, e, N)
* i =0
* fa = f(a)//.... f.apply(a)*
*
* while i N
* p = a +(b-a)/2
* fp = f(p)
*
* if fp =0 or (b-a)/2 e
* return p
*
* i = i +1
* if fa * fp >0
* a = p
* fa = fp
* else
* b = p
*
* return exception "method failed after n iterations"
*
*/
return 0;
}
/**
* Display the intermediate values of a, b, p and (b-a) for each iteration of the
* Bisection Method
*
*/
public void printBisectionMethodRunDetails(){
}
public static void main(String[] args) throws Exception{
Func f =(double x)->(x -0.5);
//double zero = Double.NaN;
//RootFinder rf = new RootFinder(0,1.1, f);
//zero = rf.runBisectionMethod(1e-5,20);
//rf.printBisectionMethodRunDetails();
//rf = new RootFinder(0,3* Math.PI /2,(double x)-> Math.cos(x));
//zero = rf.runBisectionMethod(1e-5,-2);
//rf.printBisectionMethodRunDetails();
}
}
--------------------------------------------
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class RootFinderSpec {
@Test
void intervalBoundsOutOfOrder(){
Func f =(double x)->(x -0.5);
Assertions.assertThrows(Exception.class, ()->{
new RootFinder(1,0, f);});
}
@Test
void negativeProductForFaFb(){
Func f =(double x)->(x -0.5);
Assertions.assertThrows(Exception.class, ()->{
new RootFinder(1,2, f);});
}
@Test
void negativeToleranceForRunBisectionMethod() throws Exception {
Func f =(double x)->(x -0.5);
RootFinder rf = new RootFinder(0,1.1, f);
Assertions.assertThrows(Exception.class, ()->{
rf.runBisectionMethod(-1e-5,1);
});
}
@Test
void negativeNForRunBisectionMethod() throws Exception {
Func f =(double x)->(x -0.5);
RootFinder rf = new RootFinder(0,1.1, f);
Assertions.assertThrows(Exception.class, ()->{
rf.runBisectionMethod(1e-5,-1);
});
}
@Test
void runBisectionMethodFX2_1() throws Exception {
Func f =(double x)->(Math.pow(x,2)-1);
RootFinder rf = new RootFinder(0,2.5, f);
double zero = rf.runBisectionMethod(1e-5,50);
assertTrue(Math.abs(zero -1)1e-5);
}
@Test
void runBisectionMethodSmallN() throws Exception {
Func f =(double x)->(x -0.5);
RootFinder rf = new RootFinder(0,1.1, f);
Assertions.assertThrows(Exception.class, ()->{
rf.runBisectionMethod(1e-5,1);
});
}
}
 import java.util.function.Function; @FunctionalInterface interface Func { double apply(double x); } /**

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!