Question

5. (10 pts) Consider the problem to find a root of the equation fx) 0 within [xl, x2]. Assume that the function method eval) and f(x1)<0 and f(x2)>0. Complete the following binarySearch. method using recursion, which returns a solution Xe(x1, x2], satisfying If(刈< tol. fc) is implemented in the Class MyFunction [ public: virtual double eval (double x) ) double binarysearch (double x1, double x2, double tol) // assumption eval (x1) <0 and eval (x2)>0
Thank you for your helpful answer!(It's necessary for you to pay attention to the given information)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

double binarySearch(double x1, double x2, double tol){

    double x = (x1+x2)/2;

    if (abs(eval(x))<tol){

        return x;

    }

    else{

        if(eval(x)<0){

            binarySearch(x, x2, tol);

        }

        else{

            binarySearch(x1,x,tol);

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Thank you for your helpful answer!(It's necessary for you to pay attention to the given information)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • For this homework assignment, you will create two classes that will work with each other. The...

    For this homework assignment, you will create two classes that will work with each other. The first is a class you will write is called CQuadratic, which represents a second-order quadratic equation (e.g., 3x2 + 2x + 9).  CQuadratic will only have one constructor (type constructor), which will take the coefficients of the quadratic equation (e.g., 3, 2, 9) and assign them to it’s three private data members (the three coefficient variables). The class also has an Evaluate function that passes...

  • Code to be written in C++: Initially, you will be given symbols printed in a preorder...

    Code to be written in C++: Initially, you will be given symbols printed in a preorder traversal of a boolean expression tree. The internal nodes of the tree will contain one of the following operators: & (and), | (or), ^ (exclusive-or), or ! (not). The nodes containing the first three operators will have two children, while the nodes containing the ! operator will contain only a left child. The leaves of the tree will contain an operand - either f...

  • In this assignment you’ll implement a data structure called a trie, which is used to answer...

    In this assignment you’ll implement a data structure called a trie, which is used to answer queries regarding the characteristics of a text file (e.g., frequency of a given word). This write-up introduces the concept of a trie, specifies the API you’re expected to implement, and outlines submission instructions as well as the grading rubric. Please carefully read the entire write-up before you begin coding your submission. Tries A trie is an example of a tree data structure that compactly...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT