7
$\begingroup$

If I construct a vector-valued InterpolatingFunction, say with

f = Interpolation[{{0, {1,1}}, {1, {0,0}}, {2, {0,1}}, {3, {1,0}}}]

plotting the result works just fine. For example,

ParametricPlot[f[t], {t, 0, 3}]

draws a letter "alpha" in the plane. And, as Feyre pointed out, calling Integrate also works fine, with

Integrate[f[t], {t, 0, 3}]

returning {3/4, 3/2}. But Integrate can't deal with more complicated situations, returning unevaluated for even such simple variants as

Integrate[2 f[t], {t, 0, 3}]

More complicated cases can be handled numerically with NIntegrate, as long as the thing being integrated is a scalar. For example,

NIntegrate[f[t].f[t], {t, 0, 3}]

returns 1.78214. But if I try to NIntegrate a vector-valued InterpolatingFunction, even just:

NIntegrate[f[t], {t, 0, 3}]

version 10.0.2 of Mathematica gives me the error message:

NIntegrate::inum: Integrand 
  InterpolatingFunction[{{0,3}},{5,3,0,{4},{4},0,0,0,0,Automatic,
   {},{},False},{{0,1,2,3}},
   {{{0,0}},{{1,1}},{{1,0}},{{0,1}}},{Automatic}][t] 
   is not numerical at {t} = {0.00795732}. >>

In a case too complicated for Integrate, is there some way that I can convince NIntegrate to work, component by component, over such a vector?

$\endgroup$
1
  • $\begingroup$ There's always going to be a technique to find what you want, if everything else fails, just use Table[] and Total[] $\endgroup$
    – Feyre
    Sep 14, 2016 at 22:15

2 Answers 2

7
$\begingroup$

You can use Indexed to access the components separately:

NIntegrate[Indexed[f[t], 1], {t, 0, 3}]
NIntegrate[Indexed[f[t], 2], {t, 0, 3}]
(*
  0.75
  1.5
*)

Integrate will antidifferentiate an InterpolatingFunction. You can then subtract its values at the end points.

af = Head@Integrate[f[t], t];
af[3] - af[0]
(*  {3/4, 3/2}  *)

You can also write your own integration rule to plug into NIntegrate, but that takes a little work.

$\endgroup$
4
  • $\begingroup$ Thanks for your answer. NIntegrate can only recognize number value function but not a function with list output? Seems it not looks like Integrate. $\endgroup$
    – swish47
    Jul 5, 2022 at 14:30
  • 1
    $\begingroup$ @swish47 Both Integrate and NIntegrate will integrate func[x_] := {x^2, x^3};. Neither will integrate gunk[x_?NumericQ] := {x^2, x^3};. Integrate has a special rule built in to integrate vector valued interpolating functions like the OP's f[t], but it won't integrate other expressions with interpolating functions as the OP shows (not even scalar valued ones). NIntegrate does not have this special case. In my first case, NIntegrate maps itself to two 1-dim integrals, NIntegrate[x^2,...] and NIntegrate[x^3,...]. Basically, the NIntegrate rules handle only scalar functions. $\endgroup$
    – Michael E2
    Jul 5, 2022 at 14:55
  • $\begingroup$ Thank you so much. I finally understand some weird error from NIntergrate before. And is it possible to modify the definition of NIntegrate to intergrate a list value function directly but not map to 1d integral? $\endgroup$
    – swish47
    Jul 5, 2022 at 22:38
  • 1
    $\begingroup$ @swish47 It might be, since you can plug in your own methods (see here and here). I have not tried implementing a vector method. I'm not familiar with any integration rule for integrating vectors. I could only do it by applying rules for scalar functions to each coordinate. $\endgroup$
    – Michael E2
    Jul 5, 2022 at 23:07
5
$\begingroup$

You can use NDSolve to integrate your interpolating function:

f = Interpolation[{{0,{1,1}},{1,{0,0}},{2,{0,1}},{3,{1,0}}}];

NDSolveValue[{y'[x] == f[x], y[0] == {0, 0}}, y[3], {x, 0, 3}]

{0.75, 1.5}

Here is a more complicated integrand:

NDSolveValue[{y'[x] == f[x]^2 Sin[x], y[0] == {0, 0}}, y[3], {x, 0, 3}]

{0.148276, 0.887794}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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