7

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.

anderstood
  • 14,301
  • 2
  • 29
  • 80

2 Answers2

6

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

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]
george2079
  • 38,913
  • 1
  • 43
  • 110
  • 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. – anderstood Oct 31 '16 at 17:40
  • You can do Interpolation[Table[f[x],..]] to sample as many points as you like (you do loose adaptive refinement that way though) – george2079 Oct 31 '16 at 17:48
  • Just for illustration purpose of my previous comment: http://imgur.com/a/9trUF. – anderstood Oct 31 '16 at 17:48
  • 1
    see edit for a refinement, but really if you need it to stay precisely the same you should just use the original. – george2079 Oct 31 '16 at 18:25
  • 2
    @anderstood Note that FunctionInterpolation produces a different method interpolation than NDSolve. – Michael E2 Nov 01 '16 at 00:22
  • I still have an issue with this: I'd like to keep the exact same interpolating function, so I use something of the type if[[1]] = {{1, 2}}. However I store the whole function (typically defined on a 20 times larger interval) which is really suboptimal. Do you think it's possible to chop oup the points out of something like {{0.9,2.1}}, i.e. keep the same interpolation points in the interval of interest, and crop most of the unused parts? – anderstood Nov 12 '16 at 23:54
4

Maybe the following meets your desiderata:

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

if2[0.9]
(* if2[0.9] *)

if2[1.1]
(* 1.21 *)
Chris K
  • 20,207
  • 3
  • 39
  • 74