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.

  • $\begingroup$ 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 $\endgroup$ – george2079 Aug 2 '16 at 11:51
  • $\begingroup$ Just to confirm: you want to solve $$1=x^2+\int_{1.97}^x \frac{0.37037\exp u-u}{u-1}\mathrm du$$? $\endgroup$ – J. M.'s torpor Aug 2 '16 at 11:57
  • $\begingroup$ @ J.M. : yes. Please see the comment to Kiro's answer. And please tell how to format the Latex like equation as you did. $\endgroup$ – Sluth Aug 2 '16 at 12:16
  • $\begingroup$ See this for formatting help. $\endgroup$ – J. M.'s torpor Aug 2 '16 at 12:26
  • $\begingroup$ Also a dup. this earlier answer: mathematica.stackexchange.com/a/2428 $\endgroup$ – 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}*)
  • $\begingroup$ +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. $\endgroup$ – Sluth Aug 2 '16 at 12:12
  • 2
    $\begingroup$ 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. $\endgroup$ – george2079 Aug 2 '16 at 12:39
  • $\begingroup$ @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. >> $\endgroup$ – 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]]


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:

  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}

enter image description here

  • $\begingroup$ +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 ! $\endgroup$ – Sluth Aug 3 '16 at 5:39
  • $\begingroup$ 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. $\endgroup$ – Sluth Aug 3 '16 at 5:46
  • $\begingroup$ good thought, added a plot. $\endgroup$ – george2079 Aug 3 '16 at 13:42

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