How can I reduce the domain of an InterpolatingFunction?

For example, consider if = FunctionInterpolation[x^2, {x, 0, 3}]. Is it possible to define if2 as the restriction of if to {x,1,2}? Not only should the domain change, but if2 should not contain the information of if on $[0,1]$ and $[2,3]$. I've noticed the package InterpolatingFunctionAnatomy but I don't know what methods would be useful here.


you can do:

FunctionInterpolation[if[x], {x, 1, 2}]

which will actually sample the interpolation function and generate a new one.

Alternately you can extract the data and use Interpolation

   Select[ Transpose[{#[[3, 1]], Flatten[#[[4]]]}] , 
    1 <= #[[1]] <= 2 &]] &@if

This second method may not exactly match your desired domain boundaries unless they happened to be sample points on the original interpolation.

In both cases the result will not be precisely the same as the original interpolation.

yet another thing you can do, which seems a bit of a hack. Looking at the FullForm of InterpolatingFunction you see the first argument is the domain, so if you do:

if[[1]] = {{1, 2}}

you will get a warning if you go out of the new bounds (but get the same result as the original)

Edit: this will use the derivatives from the first interpolation in the second:

data = Select[Transpose[{#[[3, 1]], Flatten[#[[4]]]}], 
                 1 <= #[[1]] <= 2 &] &@if;
Interpolation[{{#[[1]]}, #[[2]], D[if[x], x] /. x -> #[[1]]} & /@ data]
  • $\begingroup$ Do you know how to control the number of points with FunctionInterpolation? When I try it on actual interpolating functions, it clearly reduces the quality of the original if. $\endgroup$
    – anderstood
    Oct 31 '16 at 17:40
  • $\begingroup$ You can do Interpolation[Table[f[x],..]] to sample as many points as you like (you do loose adaptive refinement that way though) $\endgroup$
    – george2079
    Oct 31 '16 at 17:48
  • $\begingroup$ Just for illustration purpose of my previous comment: imgur.com/a/9trUF. $\endgroup$
    – anderstood
    Oct 31 '16 at 17:48
  • 1
    $\begingroup$ see edit for a refinement, but really if you need it to stay precisely the same you should just use the original. $\endgroup$
    – george2079
    Oct 31 '16 at 18:25
  • 2
    $\begingroup$ @anderstood Note that FunctionInterpolation produces a different method interpolation than NDSolve. $\endgroup$
    – Michael E2
    Nov 1 '16 at 0:22

Maybe the following meets your desiderata:

if2[x_ /; 1 <= x <= 2] = if[x];

(* if2[0.9] *)

(* 1.21 *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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