# Numerically solving an equation in which variable is the upper limit of an integral [duplicate]

With lower limit set to 1.97, how to find the value of upper limit of an integral where integrand is

(0.37037 E^( x) - x)/(-1 + x)

such that f[xu] + the integral equals a constant, where constant is 1.0 and f[xu] is xu^2 (say for concreteness)? I wrote f[xu] to emphasize that integral will become function of upper limit, say xu, as x will be integrated out. I know how NSolve and FindRoot work, and that f[xu] + the integral can be plotted on same graph (xu on x-axis) where the constant will represent a horizontal line, and the solution will be the crossing point visually.

• You might want to try analytic Integrate, also dont litter mathematica with superflous real literals. 1. X does nothing and will bite you if you ever need extended precision – george2079 Aug 2 '16 at 11:51
• Just to confirm: you want to solve $$1=x^2+\int_{1.97}^x \frac{0.37037\exp u-u}{u-1}\mathrm du$$? – J. M.'s torpor Aug 2 '16 at 11:57
• @ J.M. : yes. Please see the comment to Kiro's answer. And please tell how to format the Latex like equation as you did. – Sluth Aug 2 '16 at 12:16
• See this for formatting help. – J. M.'s torpor Aug 2 '16 at 12:26
• Also a dup. this earlier answer: mathematica.stackexchange.com/a/2428 – Michael E2 Aug 3 '16 at 15:30

You can do this using analytical integration like already suggested in the comments. Below is one way to do it.

expr = Integrate[(0.37037 E^(x) - x)/(-1 + x), x]
(*-1. x + 1.00677 ExpIntegralEi[-1. + x] - 1. Log[-1. + x]*)

FindRoot[expr - With[{x = 1.97}, Evaluate@expr] + x^2 - 1.0, {x, 1.1}]
(*{x -> 1.14532}*)


EDIT: to make the solution a bit more generic and also work in cases where analytic integration is not possible, I noticed that the code below does give a result (along with a warning message) for some functions that I tried, such as $Sin[Sin[x]]$.

fun = (0.37037 E^(x) - x)/(-1 + x);

FindRoot[NIntegrate[fun, {x, 1.97, y}] + y^2 - 1.0, {y, 1.1}]
(*{y -> 1.14532}*)

• +1 for the question asked. But I want to get the idea for the case where integrand is complicated and can't be symbolically integrated. – Sluth Aug 2 '16 at 12:12
• for the numerical case its actually more efficient to work with NDSolve ( and use WhenEvent ) see: mathematica.stackexchange.com/q/101784/2079. If the FindRoot..NIntegrate approach is fast enough for you then its maybe not worth the trouble though. – george2079 Aug 2 '16 at 12:39
• @Kiro. What is not correctly followed in Mathematica due to which it is throwing a warning message: NIntegrate::nlim: x = y is not a valid limit of integration. >> – Sluth Aug 3 '16 at 6:22

For completeness here is the NDSolve solution, this should be much faster if the function evaluation is slow.

fun = (0.37037 E^(x) - x)/(-1 + x);
s = y /. NDSolve[{
y'[x] == fun,
y[1.97] == 0,
WhenEvent[y[x] == 1 - x^2, "StopIntegration"]
}, y, {x, 1.97, -Infinity}][[1, 1]]
s["Domain"][[1, 1]]


1.14532

the integral value is s["ValuesOnGrid"][[1]] -> -0.311754 which is of course 1-1.14532^2

note we had to know to look for x smaller than 1.97..

here is what the solution looks like:

Show[{
Plot[1 - x^2, {x, 1, 2.5}, PlotStyle -> Red,
PlotLegends -> {1 - x^2}],
Plot[s2[x], {x, 1.97, 3},
PlotLegends -> {"Integral[... ,{1.97,x}]"}],
Plot[s[x], {x, 1.14532, 1.97}]}, PlotRange -> {{1, 3}, All}]


s2 is the result with NDSolve range from {1.97,3}

• +1. you have translated my exact thought in mathematica syntax for how to tackle this problem---ie gradually increasing the upper limit and stopping the integration when the integral reaches the constant value which is 1.0 in the instant case. Fresh and novel ! – Sluth Aug 3 '16 at 5:39
• It would have been instructive to the mathematica beginners if one can generate visualization of this solution process showing the integral area approaching some fixed (area) value where the integration stops, and the end point on the x-axis is selected as the answer. – Sluth Aug 3 '16 at 5:46
• good thought, added a plot. – george2079 Aug 3 '16 at 13:42