I'm quite new to python and algorithms, I'm struggling with the following function. Can anyone assist?:
For this question, you will be required to use the binary search to find the root of some function () on the domain ∈[,] by continuously bisecting the domain. In our case, the root of the function can be defined as the x-values where the function will return 0, i.e. ()=0 For example, for the function: ()=2()2−2 on the domain [0,2] , the root can be found at ≈1.43 .
Constraints
Stopping criteria: ||()||<0.0001 or you reach a maximum of 1000 iterations. Round your answer to two decimal places. Function specifications
Argument(s):
f (function) → mathematical expression in the form of a lambda function. domain (tuple) → the domain of the function given a set of two integers. MAX (int) → the maximum number of iterations that will be performed by the function.
My current solution is not quite there:
### START FUNCTION
def binary_search(f, domain, MAX = 1000):
### END FUNCTION```
---------------------------------
Required input:
f = lambda x:(np.sin(x)**2)*(x**2)-2
domain = (0,2)
x = binary_search(f,domain)
x
----------------------------------
Expected output:
1.43