Answered step by step
Verified Expert Solution
Link Copied!

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 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);
});
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 28th British National Conference On Databases Bncod 28 Manchester Uk July 2011 Revised Selected Papers Lncs 7051

Authors: Alvaro A.A. Fernandes ,Alasdair J.G. Gray ,Khalid Belhajjame

2011th Edition

3642245765, 978-3642245763

More Books

Students also viewed these Databases questions

Question

Which of the following is Not input device Keyboard Mouse Scanner

Answered: 1 week ago