Integrating Squared of Interpolating Function with respect to one variable

I am interested in evaluating a two dimensional interpolating function produced by solving the wave equation. Here is the code that includes the resulting interpolating function.

pde = D[y[x, t], t, t] == D[y[x, t], x, x]

solnDerivative = NDSolve[{pde, y[x, 0] == Exp[-(x)^2], Derivative[0, 1][y][x, 0] == 0,
Derivative[1, 0][y][-50, t] == Derivative[0, 1][y][-50, t],
Derivative[1, 0][y][50, t] == - Derivative[0, 1][y][50, t]},
Derivative[0, 1][y][x, t], {x, -50, 50}, {t, 0, 130}]

ifunDerivative  = First[Derivative[0, 1][y][x, t] /. solnDerivative]


Here, I wanted to first integrate squared of ifunDerivative with respect to t from {0,130} then get a new interpolating function as a function of x only.

The first problem I ran into is that it won't evaluate this integral and spits back out the input.

integratedFunction = Integrate[(ifunDerivative[x, t])^2, {t, 0, 130}, x]


How can I go about numerically integrating interpolating function squared with respect to t? Then, obtain a solution that depends only on x? Please help me! Thank You!

• I'm not sure about the code integratedFunction = Integrate[(ifunDerivative[x, t])^2, {t, 0, 130}, x]. Do you want indefinite integral with respect to x followed by the definite integral with respect to t? – Michael E2 Sep 14 '14 at 2:20
• Hello, I wanted to evaluate definite integral with respect to t from {t,0,130} and then express the resulting interpolating function in terms of x, then graph it. – soccerboyz9341 Sep 14 '14 at 16:55

One can integrate Derivative[0, 1][y][x, t]^2 along with the pde:

pde = D[y[x, t], t, t] == D[y[x, t], x, x];

solnDerivative =
NDSolve[{pde, y[x, 0] == Exp[-(x)^2], Derivative[0, 1][y][x, 0] == 0,
Derivative[1, 0][y][-50, t] == Derivative[0, 1][y][-50, t],
Derivative[1, 0][y][50, t] == -Derivative[0, 1][y][50, t],
(**)
Derivative[0, 1][int][x, t] == Derivative[0, 1][y][x, t]^2,
int[x, 0] == 0},
{Derivative[0, 1][y][x, t], int[x, 130]}, {x, -50, 50}, {t, 0, 130}]


The integral may be obtained and a value for x substituted with

int[x, 130] /. First@solnDerivative /. x -> 10


Here is a plot (on the grid generated by NDSolve), which seems a little wobbly around x == 0:

ListLinePlot[{x, int[x, 130] /. First@solnDerivative} /. x -> # & /@
Head[Derivative[0, 1][y][x, t] /. First@solnDerivative]["Grid"][[All, 1, 1]],
PlotRange -> All]


Here are some values of int[x, 130] compared with the result of integrating the derivative from the computed derivative Derivative[0, 1][y][x, t], with values of x taken among the grid computed by NDSolve:

Table[{
int[x, 130] /. First[solnDerivative],
(int[x, 130] /. First[solnDerivative]) -
NIntegrate[Derivative[0, 1][y][x, t]^2 /. First[solnDerivative],
Evaluate@
Prepend[Head[Derivative[0, 1][y][x, t] /. First@solnDerivative]["Grid"][[1, All, 2]], t]]},
{x, Head[Derivative[0, 1][y][x, t] /. First@solnDerivative]["Grid"][[All, 1, 1]][[;; ;; 37]]}]
(*
{{0.317956, -0.00806953},    {0.324704,  0.0000186443}, {0.326885,  2.02759*10^-6},
{0.334907,  0.000021252},   {0.340367,  0.0000134228}, {0.342387,  0.00011331},
{0.341913,  0.000136063},   {0.657123, -0.000169644},  {0.344552,  0.000135694},
{0.348381,  0.000113308},   {0.356022,  0.0000133392}, {0.374857,  0.0000212584},
{0.400694,  1.89176*10^-6}, {0.405668,  0.0000179112}, {0.423675, -0.00942803}}
*)


V8 Code

The code above does not run on V8.0.4. The return expression int[x, 130] with the value 130 for t. My preference is to return the InterpolatingFunction without the arguments. Another issue is that the code complains about a spatial error estimate being too great (with a grid of 933 points). Setting "MinPoints" to 1000 solves that problem.

solnDerivative =
NDSolve[{pde, y[x, 0] == Exp[-(x)^2], Derivative[0, 1][y][x, 0] == 0,
Derivative[1, 0][y][-50, t] == Derivative[0, 1][y][-50, t],
Derivative[1, 0][y][50, t] == -Derivative[0, 1][y][50, t],
(**)
Derivative[0, 1][int][x, t] == Derivative[0, 1][y][x, t]^2, int[x, 0] == 0},
{Derivative[0, 1][y], int},
{x, -50, 50}, {t, 0, 130},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "MinPoints" -> 1000}}]


The rest of the code above may be used as is. One might want to change the 37 to something like 100 in the Table example.

• Hello, thank you very much for the answer. I have a question regarding your NDSolve. What exactly is happening at "" (**) Derivative[0, 1][int][x, t] == Derivative[0, 1][y][x, t]^2, int[x, 0] == 0},{Derivative[0, 1][y][x, t], int[x, 130]}"" – soccerboyz9341 Sep 14 '14 at 17:01
• Why is Derivative[0, 1][int][x, t] == Derivative[0, 1][y][x, t]^2? And what is the purpose of this int[x,t] function? Thank you so much for your input. It is helping me alot! – soccerboyz9341 Sep 14 '14 at 17:08
• @soccerboyz9341 You're welcome. The extra equations form an IVP that represents the t integral of the square of the derivative, Derivative[0, 1][y][x, t]^2. The expression int[x, t] represents the integral from 0 to t, so int[x, 130] is the integral you seek. The second argument {Derivative[0, 1][y][x, t], int[x, 130]} tells NDSolve what functions (and in what form) to return. The return value solnDerivative will consist of two rules, one for each function. – Michael E2 Sep 14 '14 at 17:08
• thank you again. Is there a reason why there is an additional condition int[x, 0] == 0? – soccerboyz9341 Sep 14 '14 at 17:14
• @soccerboyz9341 In TeX, Derivative[0, 1][int][x, t] == Derivative[0, 1][y][x, t]^2 is $\partial (\hbox{int(x,t)})/\partial t = (\partial y /\partial t)^2$ so that $\hbox{int}(x,t_0) = \int_0^{t_0} (\partial y /\partial t)^2\;dt$. The condition int[x, 0] == 0 is what defines the starting value of the integral -- it corresponds to $\int_0^{0} (\partial y /\partial t)^2\;dt = 0$. – Michael E2 Sep 14 '14 at 17:16

I'm sure this isn't the most beatuiful thing, but it is what I'd do:

integratedFunction[xx_?NumericQ] := NIntegrate[(ifunDerivative /. {x -> xx, t -> tt})^2, {tt, 0, 130}]

• Also, you should probably add //Quiet at the end. – Ivan Sep 12 '14 at 23:37