# Nested Integrate and NIntegrate: Analytic and Numeric solutions?

Here is a function $F(r)$ which contains double integrations

$$F(r)=\exp\left[ \int_{0}^r dw \,\exp\left(-\int_{0}^w ds \frac{s^2}{s^2+1} \left(1-\exp(- s)\right) \right) \right]$$

I am fine for either/both obtaining

(1) a analytic solutions $F(r)$ (say, in terms of just a function of $r$),

or

(2) a numerical function of $r$ (say, a x-y plot of x=$r$ v.s. y=$f(r)$, even just a selection of points running from $\{r, 0, 100, 1\}$)

I find the most challenge part is that the choices we can make for doing the Integrate and NIntegrate. How to wisely choose NIntegrate and Integrate to obtain the answer.

For example, what will be the plot of Plot[F(r),{r,0,100}] or ListPlot[F(r),{r,0,100,1}] looks like?

The inner integral can be performed symbolically by

si = Integrate[s^2 (1 - Exp[-s])/(s^2 + 1), {s, 0, w}, Assumptions -> w > 0]
(* w - ArcTan[w] + 1/2 (-2 + 2 E^-w - I E^I ExpIntegralEi[-I] +
I E^-I ExpIntegralEi[I] + I E^I ExpIntegralEi[-I - w] - I E^-I ExpIntegralEi[I - w]) *)


and the outer integral numerically by

s = NDSolveValue[{f'[w] == Exp[-si], f == 0}, f, {w, 0, 10}];
F[r_] := Exp[s[r]]
Plot[F[r], {r, 0, 10}, PlotRange -> All, AxesLabel -> {r, "F"},
ImageSize -> Large, LabelStyle -> Directive[Bold, Black, Medium]] Although certainly not necessary, it is possible to simplify si as follows.

FullSimplify[ComplexExpand[si, TargetFunctions -> {Re, Im}], w > 0];
% /. 1/2 I E^-I (E^(2 I) ExpIntegralEi[-I - w] - ExpIntegralEi[I - w])
-> Im[Exp[-I] ExpIntegralEi[I - w]]
% /. CosIntegral Sin - 1/2 Cos (Pi + 2 SinIntegral) - 1
-> N[CosIntegral Sin - 1/2 Cos (Pi + 2 SinIntegral) - 1]
(* -2.07596 + E^-w + w - ArcTan[w] + Im[E^-I ExpIntegralEi[I - w]] *)

• thanks +1, I am puzzled. At $r=0$, should the $F(r)=1$, since the exponent in the exponential is $0$ so $e^0=1$? Why is your $F(r)=0$? – annie marie heart Oct 5 '17 at 16:13
• Yes, it should be 1. I misread your question. But, the fix is easy, and I shall make it in a few minutes. Thanks for catching my error. – bbgodfrey Oct 5 '17 at 16:51
• Other than that, the way I fix it is by also setting the boundary condition, do you think we need f == 1 instead of your f == 0? I think it makes more sense – annie marie heart Oct 5 '17 at 17:41
• No, the value of the outer integral is zero at r == 0. It is the value of the exponential of that integral that is 1 at r == 0. – bbgodfrey Oct 5 '17 at 17:44

Thank you for your correction of the question and revising to replace r with s which changes the problem.

Exp[Integrate[Exp[-Integrate[s^2/(s^2+1)(1-Exp[-s]), {s,0,w}]], {w,0,r}]]


which gives

E^Integrate[ConditionalExpression[
E^(-w + ArcTan[w] + (2 - 2/E^w + I*E^I*ExpIntegralEi[-I] -
(I*ExpIntegralEi[I])/E^I - I*E^I*ExpIntegralEi[-I - w] +
(I*ExpIntegralEi[I - w])/E^I)/2), Re[w] > 0 && Im[w]==0], {w,0,r}]


because MMA can't determine whether all your values are real. Adding assumptions such as bbgodfrey did can then simplify this to probably what you are looking for.

• NO there is a $s$ in the integraand, it is a typo, I am sorry. I vote you up. But please take it into account – annie marie heart Oct 5 '17 at 4:49