# Use output of NDSolve as an integration limit of NIntegrate

I've been working on a ODE, for a function y[t], that, due to it's complexity, has to be solved numerically. I then need to use the results of NDSolve, which I store in the variable $s$, in the following manner:

Plot[
NIntegrate[
f[r],{r,b,Evaluate[Sqrt[{(y[t])^2+b^2}/.s]]}
],
{t,0,5}
]


which returns the following errors:

NIntegrate::nlim: r = {20.025} is not a valid limit of integration. >>
NIntegrate::nlim: r = {20.025} is not a valid limit of integration. >>
NIntegrate::nlim: r = {19.6024} is not a valid limit of integration. >>
General::stop: Further output of NIntegrate::nlim will be suppressed during this calculation. >>


It seems that the problem is due to $\text{Evaluate}$ returning the number in parentheses...I've tried the function $\text{N}$ but it has the same problem. Maybe I'm doing something else wrong. Can anyone help me?

Edit: I shall post the complete code as suggested in the comments:

Module[{b = 1., k = 1., Vt0 = 2., L0 = 20.},
s = NDSolve[
{y''[t] - (y'[t])^2 (b^2/((b^2 + (y[t])^2) y[t]) - y[t]/(
b^2 (Exp[(y[t])^2/b^2] - 1))) - (z'[t])^2 (-1/(
y[t] Exp[(y[t])^2/b^2]) (Exp[(y[t])^2/b^2] -
1) (b^2 + (y[t])^2)) + k*(b^2 y[t])/(b^2 + (y[t])^2) == 0,
y[0] == L0, y'[0] == 0,
z''[t] + 2*y'[t]*z'[t]*(y[t]/(b^2 + (y[t])^2)) == 0, z'[0] == Vt0,
z[0] == 0},
{z, y}, {t, 0, 5}
];
f[r_] := Sqrt[E^(r^2/b^2)/(-E + E^(r^2/b^2)) - 1];

Plot[
NIntegrate[f[r], {r, b, Evaluate[Sqrt[(y[t])^2 + b^2] /. s]}],
{t, 0, 5}
]
]

-
you shall put info on f[],s,r..!! –  Rorschach Aug 25 '13 at 12:26
@Nasser ups. Yes \sqrt is Sqrt[]. I copied as Latex before used the 'code' command and left that artifact. –  PML Aug 25 '13 at 12:29
@Blackbird Just edited to include the complete code. The function $\text{z[t]}$ shall not matter for the error. –  PML Aug 25 '13 at 12:33
M is saying that you are specifying an interpolating function where it needs a constant.You need to look into this.probably your approach is wrong.Upper limit of NIntegrate cannot be a function returning function. –  Rorschach Aug 25 '13 at 12:44
@Blackbird Based on your suggestion I figured out what was wrong with my approach. Thank you very much for your time. –  PML Aug 25 '13 at 14:03

I do not know if this plot makes sense of not. But you have complex values showing up from your NIntegrate. Any way, may be you can look at this and see if it look ok.

Let us spread things out a little. Once it is working ok, then you can make a Module and collapse back into one nice long expression.

b = 1.;
k = 1.;
Vt0 = 2.;
L0 = 20.;

sol = NDSolve[{y''[
t] - (y'[t])^2 (b^2/((b^2 + (y[t])^2) y[t]) -
y[t]/(b^2 (Exp[(y[t])^2/b^2] - 1))) - (z'[
t])^2 (-1/(y[t] Exp[(y[t])^2/b^2]) (Exp[(y[t])^2/b^2] -
1) (b^2 + (y[t])^2)) + k*(b^2 y[t])/(b^2 + (y[t])^2) == 0,
y[0] == L0, y'[0] == 0,
z''[t] + 2*y'[t]*z'[t]*(y[t]/(b^2 + (y[t])^2)) == 0, z'[0] == Vt0,
z[0] == 0}, {z, y}, {t, 0, 5}];
ysol = y /. First@sol;


make sure solution looks ok before the next step

Plot[ysol[t], {t, 0, 5}]


f[r_?NumericQ] := Sqrt[E^(r^2/b^2)/(-E + E^(r^2/b^2)) - 1];


check

f[r]


make the data. I used Abs since you have complex numbers otherwise.

data = Table[{t,Abs @ NIntegrate[f[r], {r, b, Sqrt[ysol[t]^2 + b^2]}]}, {t, 0,5, .05}];


check some points:

data[[1 ;; 3]]


plot

ListLinePlot[data, Joined -> True, Mesh -> True, MeshStyle -> Red,
PlotRange -> All, AxesLabel -> {"t", "NIntegrate"}]


Another way to do the above, is to first calculate the upper limit in a vector, make sure it is ok, then use that next for the integration. Same result is found.

upperLimit = Table[Sqrt[ysol[t]^2 + b^2], {t, 0, 5, .01}];
ListLinePlot[upperLimit, Mesh -> True, MeshStyle -> Red]


data = Table[{i, Abs @ NIntegrate[f[r], {r, b, upperLimit[[i]]}]}, {i,
Length[upperLimit]}];

ListLinePlot[data, Joined -> True, Mesh -> True, MeshStyle -> Red,
PlotRange -> All, AxesLabel -> {"i", "NIntegrate"}]


I do not see anything interesting here. Making t steps smaller, one get straight line.

-
I believe that your approach is correct. I've figured out what was wrong with mine. I just had to equal the upper limit expression to a variable, say $\text{x}$. And then in NIntegrate use $\text{x[[1]]}$ for the upper limit. But certainly your approach is very interesting and I foresee that I'll check it out oftentimes. Thank You very much –  PML Aug 25 '13 at 14:01