I need to find a root of an equation in a given interval. The Problem is that there are more than one roots. But only within [0,$\pi$] I'm interested in the solution.
So I implemented a function find_root in my C++ code:
double find_root(std::function<double(double)> func, std::function<double(double)> deriv, const double start_value, const int it_max)
{
// Implementation of Newton Raphson Method
double a = start_value;
double z;
int it = 0;
do
{
z = a - (func(a) / deriv(a)); // z is root
a = z; // a is guess
it++;
} while (abs(func(z))>0.000001 && it <= it_max);
if (it == 1000) {
// doesn't matter...
}
return a;
}
Of course this works. But for some examples I receive roots I'm not interested in and not the root I'm looking for because it's outside my interval. So I asked myself is there a general way to use the Newton Method within a certain interval.
I would be thankful for help.
BG mk3