Lab09 due Wednesday November 17th
Root finding by bisection and false position methods.
For this lab your mission will consist in completing the functions
1) int bisection(double (*fct)(double x, void *par),void *par,double f0,
double a, double b, double err, double *sol){
2) int false_position(double (*fct)(double x, void *par),void *par,double f0,
double a, double b, double err, double *sol){
In the file rootfind.cpp
1) The function bisection(double (*fct)(double x, void *par),void *par,
double f0,double a, double b, double err, double *sol) evaluates the
value of x such that fct(x,par)==f0 where par is a void pointer used to
pass any parameters necessary to the calculation of the value of fct().
The root of the equation is searched for between a and b and is estimated
with an accuracy specified by err. The root is returned via pointer sol.
The algorithm used is bisection. The function returns -1 if there is an
even number of root between a and b or is err is <=0. It returns 1 when
the algorithm converges succesfully. Boundaries a and b can be specified
in any order a>b or b>a. You just have to implement the bisection
algorithm in this function.
2) The function false_position(double (*fct)(double x, void *par),void *par,
double f0,double a, double b, double err, double *sol) works in the same
way as bisection() except that the algorithm used is the false position
which you need to implement. For this, it is suggested that you paste the
content of bisection and change it a little to implement the false
position.
3) Modify both your functions bisection() and false_position() using
precompiler commands #ifdef and #endif in such a way that when VERBOSE
is defined (-D VERBOSE in the compile command line) the number of iteration
performed by each function is printed out. Observe that when the root is
requested with a fine precision, fals_position() outperforms bisection.
Is it always the case when you degrade the requested precision? Can you
explain what is going on?
To submit your work, use the command:
submit p6720 Lab09 rootfind.cpp