# NIntegrate the result of NDSolve

My code involves integrating the a function, which requires the end value of the NDSolve. My differential equations and integrating functions are very complex (not related to complex numbers) so I need to use NDSolve and NIntegrate. I was able to get to the point of getting the result of NDSolve. Now I'm unable to get any further. Here is a toy problem which is similar to my big code.

ClearAll[Evaluate[StringJoin[Context[], "*"]]]
Needs["UtilitiesCleanSlate"];
CleanSlate[];
ClearInOut[];

c[r_] := E^(-r);

func1[r_, t_] := r + t;

x1x2[r2_, c2_, t2_] := Module[{r = r2, c = c2, t0 = t2},
Reap[
NDSolve[{Derivative[1][x11][t3] == x11[t3]^2 +c func1[r, t3],
WhenEvent[t3 == t0, Sow[x11[t3]]],
x11[0] == 0},
{},
{t3, t0}]][[-1,1,1]]];

x1[r_, t_] := x1x2[r, c[r], t];
x1[0, 1/30]

l[r_, t_] := func1[r, t]*x1[r, t];

finalF[(t_)?NumericQ] := NIntegrate[l[rr, t], {rr, 0, 1}, MaxRecursion -> 50,
AccuracyGoal -> 10, Method -> {"LocalAdaptive", "SymbolicProcessing" -> 0},
PrecisionGoal -> 10];

finalF[1/30]

Plot[final[tt],{tt,0,1}]


I used the solution method described in this answer to obtain the final result of NDSolve.

Any one kindly suggest me how to proceed. Thank you

• If it's any help, you can compute the antiderivative of an NDSolve solution with Integrate. For instance, yFN = y /. First@NDSolve[{y''[x] == x y[x] - y[x]^2, y'[0] == 0, y[0] == 1}, y, {x, 0, 10}]; Integrate[yFN[x], x] Commented Apr 15, 2020 at 12:37
• What is a role parameter c=c2 plays in a module? Commented Apr 15, 2020 at 12:54
• @Alex Trounev, hi, sorry for missing "c" in my code .i have edited my code .Kindly suggest me approach .thank-you Commented Apr 15, 2020 at 13:37
• @ Michael E2 . Hi thanks for the response .Since my function complex I won't be able to use Integrate function. Commented Apr 15, 2020 at 14:04

Perhaps it's easier to use ParametricNDSolveValue instead of Sow/Reap, because you can require only the last point of the interpolation (evaluated by NDSolve) !

Try

X11 = ParametricNDSolveValue[{Derivative[1][x11][t3] ==x11[t3]^2 + c[r] func1[r, t3], x11[0] == 0},x11 [t0], {t3, 0, t0}, {r, t0}]
(* returns x11[t0] !*)


for example

X11[0, 1/30]
(* 0.000555558 *)


further integration

l[r_, t_] := func1[r, t]*X11[r, t] ;
finalF[ t_ ?NumericQ] := NIntegrate[l[rr, t], {rr, 0, 1}]
Plot[finalF[tt], {tt, 0, 1}]


• awesome .thanks alot Commented Apr 15, 2020 at 14:02
• You're welcome! Commented Apr 15, 2020 at 14:20