Search
Suppose we have a real function of one variable $f: \mathbb{R} \rightarrow \mathbb{R}$ continuous on the interval $[a, b] \in \mathbb{R}$, for which the function values in the extreme points of this interval have the opposite sign. The bisection method is an iterative algorithm for finding the root of such a function on a specified interval. In each iteration, the bisection method performs the following two steps:
The above two steps are repeated until the desired accuracy is reached or the maximum number of iterations is reached. As a stopping criterion, we can use the functional value at point $c$, i.e. the algorithm will terminate if $|f(c)| < \varepsilon$ for the specified tolerance $\varepsilon \in \mathbb{R}$.
The regula falsi method differs from the bisection method only in how it selects the $c$ point. The following formula is used for this method
$$ c = \frac{a\cdot f(b) - b\cdot f(a)}{f(b) - f(a)}. $$
Implement a findroot function that finds the root of a given function on a given interval. The findroot function must have the following input arguments (in the order listed):
findroot
method
f
a
b
Choose the appropriate types for all input parameters of the findroot function. Use the following type hierarchy to differentiate the root search method.
abstract type BracketingMethod end struct Bisection <: BracketingMethod end struct RegulaFalsi <: BracketingMethod end
Additionally, the findroot function must accept the following keyword arguments (values after = are values):
=
atol = 1e-8
maxiter = 1000
When implementing, note that the bisection method and the regula falsi method differ only in the selection of a new point. Write a general findroot function for both methods. Use multiple-dispatch and write a midpoint function that will return a new point based on the method used. This function must have the following input arguments (in the order listed):
midpoint
The findroot function must also meet the following properties:
a < b