Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 applydouble x;
Find a root for a continuous, real value function f over a closed
interval ab in which fa fb
@author
public class RootFinder
Used to determine if doubles are equal
public static final double EPSILON e;
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 fa fb
@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 fa fb
@throws Execption if end points of interval are out of order
@throws Exception if fafb
public RootFinderdouble 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 runBisectionMethoddouble 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 Methoda b f e N
i
fa fa fapplya
while i N
p a ba
fp fp
if fp or ba e
return p
i i
if fa fp
a p
fa fp
else
b p
return exception "method failed after n iterations"
return ;
Display the intermediate values of a b p and ba for each iteration of the
Bisection Method
public void printBisectionMethodRunDetails
public static void mainString args throws Exception
Func f double xx ;
double zero Double.NaN;
RootFinder rf new RootFinder f;
zero rfrunBisectionMethode;
rfprintBisectionMethodRunDetails;
rf new RootFinder Math.PI double x Math.cosx;
zero rfrunBisectionMethode;
rfprintBisectionMethodRunDetails;
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 xx ;
Assertions.assertThrowsExceptionclass,
new RootFinder f;;
@Test
void negativeProductForFaFb
Func f double xx ;
Assertions.assertThrowsExceptionclass,
new RootFinder f;;
@Test
void negativeToleranceForRunBisectionMethod throws Exception
Func f double xx ;
RootFinder rf new RootFinder f;
Assertions.assertThrowsExceptionclass,
rfrunBisectionMethode;
;
@Test
void negativeNForRunBisectionMethod throws Exception
Func f double xx ;
RootFinder rf new RootFinder f;
Assertions.assertThrowsExceptionclass,
rfrunBisectionMethode;
;
@Test
void runBisectionMethodFX throws Exception
Func f double xMathpowx;
RootFinder rf new RootFinder f;
double zero rfrunBisectionMethode;
assertTrueMathabszero e;
@Test
void runBisectionMethodSmallN throws Exception
Func f double xx ;
RootFinder rf new RootFinder f;
Assertions.assertThrowsExceptionclass,
rfrunBisectionMethode;
;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started