Thank you for your helpful answerIts necessary for you to pa

Thank you for your helpful answer!(It\'s necessary for you to pay attention to the given required information)

Consider the problem to find a root of the equation f(x) = 0 within [x1, x2]. Assume that the function f(.) is implemented in the method eval() and f(x1) 0. Complete the following binarySearch(...) method using recursion, which returns a solution x e[x1, x2], satisfying |f(x)|

Solution

double binarySearch(double x1, double x2, double tol)
{
//Assumption: eval(x1) < 0 and eval(x2) > 0.
double mid = (x1 + x2) / 2;
if(abs(eval(mid)) <= tol)
return mid;
else if(eval(mid) < tol)
binarySearch(x1, mid, tol);
else
binarySearch(mid, x2, tol);   
}

Thank you for your helpful answer!(It\'s necessary for you to pay attention to the given required information) Consider the problem to find a root of the equati

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site