# NSolve and FindRoot failing to evaluate

So I am integrating a Piecewise function and then using NSolve to find out where the limits of integration lie to satisfy an equation.

NSolve[Pi == Integrate[Piecewise[{{(2 Sin[x1])/Sin[θ], x1 < θ < -Pi - x1}}, 1.999], {θ, -5 Pi/6, -Pi/6}], x1, Reals]


This code works perfectly fine, returning the desired result $x_1 \sim -2.49$.

However, when the limits of the integration are slightly more complicated, as in this example:

 NSolve[4.36282952905673 == Integrate[
Piecewise[{{1.999, -255 Pi/256 < θ < x1}, {(2 Sin[x1])/
Sin[θ],
x1 < θ < - π + ArcSin[2 Sin[x1]]}, {1.001, - π +
ArcSin[2 Sin[x1]] < θ < ArcSin[2 Sin[x1]]}, {(
2 Sin[x1])/Sin[θ],
ArcSin[2 Sin[x1]] < θ < -Pi - x1}, {1.999, -Pi -
x1 < θ < -(Pi/256)}},
1.999], {θ, -255 Pi/256, -Pi/256}], x1, Reals]


NSolve runs forever. I have also tried using FindRoot with a sensible guess of $x_1 = -2.71$, which again fails to complete running:

FindRoot[4.36282952905673 ==
Integrate[
Piecewise[{{1.999, -255 Pi/256 < θ < x1}, {(2 Sin[x1])/
Sin[θ],
x1 < θ < - π - ArcSin[2 Sin[x1]]}, {1.001, - π -
ArcSin[2 Sin[x1]] < θ < ArcSin[2 Sin[x1]]}, {(
2 Sin[x1])/Sin[θ],
ArcSin[2 Sin[x1]] < θ < -Pi - x1}, {1.999, -Pi -
x1 < θ < -(Pi/256)}},
1.999], {θ, -255 Pi/256, -Pi/256}], {x1, -2.71}]


I am motivated that the root $x_1$ is close to $-2.71$ as the following:

 ReleaseHold[
Hold[Integrate[
Piecewise[{{1.999, -255 Pi/256 < θ < x1}, {(2 Sin[x1])/
Sin[θ],
x1 < θ < - π - ArcSin[2 Sin[x1]]}, {1.001, - π -
ArcSin[2 Sin[x1]] < θ < ArcSin[2 Sin[x1]]}, {(
2 Sin[x1])/Sin[θ],
ArcSin[2 Sin[x1]] < θ < -Pi - x1}, {1.999, -Pi -
x1 < θ < -(Pi/256)}},
1.999], {θ, -255 Pi/256, -Pi/256}]] /. x1 -> -2.71]


Gives the integral as 4.34719, very close to the desired 4.36282952905673.

Any pointers on why my NSolve or FindRoot code is not working? Could it be something to do with Hold? I had to Hold the Integrate to get ReplaceAll to work.

Edit

I have found a solution that works, though it does require me to have a fairly good guess of where the root is via trial and error with the integral block as posted above.

g[x1_?NumericQ] :=
Integrate[
Piecewise[{{1.999, -255 Pi/256 < θ < x1}, {(2 Sin[x1])/
Sin[θ],
x1 < θ < -π - ArcSin[2 Sin[x1]]}, {1.001, -π -
ArcSin[2 Sin[x1]] < θ <
ArcSin[2 Sin[x1]]}, {(2 Sin[x1])/Sin[θ],
ArcSin[2 Sin[x1]] < θ < -Pi - x1}, {1.999, -Pi -
x1 < θ < -(Pi/256)}}, 1.999], {θ, -255 Pi/256, -Pi/256}]

FindRoot[4.36282952905673 == g[x1], {x1, -2.8, -2.69}, Method -> "Secant"]


This gives $x_1 = -2.70523$ as required.

• Since you are using g only with numerical values, I'd suggest that you use NIntegrate rather than Integrate` for the numerical integration. Apart from that, you might want to move your solution to a self-answer, rather than adding it to your question. – MarcoB Jul 15 '16 at 1:08