Trying to numerically solve an equation involving an integral, either the error Recursion depth exceeded appears (when AccuracyGoal->10 or smaller) or the error Numerical integration converging too slowly appears. In the MWE below, the functions involved are smooth, bounded and the solution for the equation is 1 (as confirmed by Plot[{hdif[z], 0}, {z, smin, 2}]), so there should not be difficulty numerically integrating or solving.

How to find a solution to similar equations which cannot be solved by hand analytically?


Clear[smin, smax, h, hdif]
smin = -1;
h[z_] := Exp[-z/2]
hdif[zmax_?NumericQ] := 
 NIntegrate[(Exp[z] - 1)*h[z], {z, smin, zmax}, AccuracyGoal -> 5]
smax = FindRoot[{hdif[smax] == 0}, {smax, 0, smin, Max[-smin, 1000]}]

Other solutions recommend increasing AccuracyGoal or WorkingPrecision (both of which I tried), cancelling periodic oscillations, integrating piecewise around points where the derivative is discontinuous (these do not apply to the function in question). Replacing NIntegrate with Integrate works for h[z_] := Exp[-z/2], but not for functions without a closed form integral.


1 Answer 1


You could use NDSolveValue for something like this:

smin = -1;
h[z_] := Exp[-z/2]
Last @ Reap[
        int'[z] == (Exp[z] - 1) h[z], int[smin] == 0, 
        WhenEvent[int[z]==0, {"StopIntegration", Sow[z, "zmax"]}]
        {z, smin, Infinity}
    #1 -> First@#2&

{"zmax" -> 1.}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.