Find root of a function with $\dfrac{\sin x}{\cos x}$ in a given interval

functionsnumerical methodsroots

I have defined a function with $\sin x$ and $\cos x$ expressions in it. So since this is a periodic function I know I have more than one root. The target is to find a solution for the second (or more general) the highest root in the given interval with my C++ code.

The function looks as follows:

enter image description here

As you can see in the picture in a given interval of [0,$\pi$] I have two roots. Only this interval is relevant for me.

Since I failed to receive the right solution with the Newton Method I choosed the Bisection Method because I than can be sure to receive a solution within the given interval. Newton Method also gave me solutions outside the interval which is not allowed here.

But here comes the problem. Since I have more than one root in this given interval there is no guarantee that I receive the right solution. I'm only interested in the highest root value. Currently I repeat the call of the function which calculates the root according to the bisection method several times until I don't receive a solution anymore. Everytime I define as lower bound the already found root (+very small increment) and the upper bound $\pi$.

It works of course but I was wondering if there is a more efficient way or a more common way to make sure to always receive the highest value of the root.

Has anybody an idea or a hint for a better solution?

Best regards
mk3

Best Answer

If you are able to bracket the solution, I suggest you use subroutine RTSAFE from Numerical Recipes (this is the $C$ version) (look at page $366$). Quoting the book

"The hybrid algorithm takes a bisection step whenever Newton-Raphson would take the solution out of bounds, or whenever Newton-Raphson is not reducing the size of the brackets rapidly enough".

Looking at your plots, in order to bracket the solution, what I should to is to find the maximum value of the function (solving $f'(x)=0$ - hoping that there is only one root in the interval). So, the largest root is between this point and the right bound.

If it is possible, I would enjoy playing with your function.