# Confusion over constructing interpolation functions from tables

Suppose I have a set of functions:

f[x_] := x^2;

g[x_] := (f[x])^2 +2*f[x];

h[x_] := 2*f[x]+5*g[x];


I tabulate the first two functions over x such that

ftab1 = Table[{x,f[x]},{x,0,5,0.5}];

gtab1 = Table[{x,g[x]},{x,0,5,0.5}];


then use them to construct a table for h[x], i.e.

ftotal1 = Table[{x,2*ftab1[[j,2]]+5*gtab1[[j,2]]},{j,1,Length[ftab1]}];


and finally interpolate this table to obtain a function,

func1 = Interpolation[ftotal1];


I then construct tables again as above, but replacing x with func1[x], such that

ftab2 = Table[{x,f[func1[x]]},{x,0,5,0.5}];

gtab2 = Table[{x,g[func1[x]]},{x,0,5,0.5}];

ftotal2 = Table[{x,2*ftab2[[j,2]]+5*gtab2[[j,2]]},{Length[ftab2]}];


and then finally interpolate the result again to obtain a new function

func2 = Interpolation[ftotal2];


Alternatively, I simply tabulate h[x] straight away, i.e.

htab1 = Table[{x,h[x]},{x,0,5,0.5}];


then interpolate this

hfunc1 = Interpolation[htab1];


Following the same procedure, I then tabulate h again, replacing x with hfunc1,

htab2 = Table[{x,h[hfunc1[x]]},{x,0,5,0.5}];


an then finally interpolate this to get

hfunc2 = Interpolation[htab2];


What I find is that, if I plot a graph of the interpolation functions, hfunc1 = func1, however hfunc2 ≠ func2. Why is this the case? Is it something to do with the precision errors that are accumulated in the two different methods and how Mathematica handles them?

• Try adding InterpolationOrder -> 4 to Interpolation[]. You are in effect trying to approximate a quartic polynomial with piecewise cubics, so no surprise that there is some error. – J. M. is away Aug 4 '17 at 12:04
• @J.M. Thanks, that seems to have worked. Will this kind of thing work in a more general case – user35305 Aug 4 '17 at 12:25
• As a rather famous book on numerical computing once put it, "higher order does not mean higher accuracy". In this case, increasing the interpolation order worked because your actual function is a polynomial, but if the function were transcendental... – J. M. is away Aug 4 '17 at 12:29
• @J.M. Ah, that's frustrating as I have a Runge-Kutta routine that I'm struggling to get to work precisely because of this problem. – user35305 Aug 4 '17 at 12:31
• ...and that's why one should always try to come up with a minimal example that still has most of the features of the original problem. ;) – J. M. is away Aug 4 '17 at 12:33