# How to define InterpolatingFunction on a variable set of points?

I would like to define an InterpolatingFunction to which data points are transferred as a parameter, e.g.:

h[i_]:=(i-0.5)*12.5;
data={0.0047,0.021,0.023,0.015,0.00085,0.0041,0.0018,0.00077};
f[data_,x_]:=g[x]/.Interpolation[Table[{h[i],data[[i]]},{i,1,Length[data]}]];
Plot[f[data,x],{x,h[1],h[Length[data]]}]


or

f[data_]:=Interpolation[Table[{h[i],data[[i]]},{i,1,Length[data]}]]


However when I’m trying to evaluate or plot function (Plot[f[data,x],{x,h[1],h[Length[data]]}]) Mathematica shows the same error in both cases ReplaceAll::reps: {InterpolatingFunction[…]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. (By three dots I’ve replaced a set of values and options).
Could you suggest what I’m doing wrong?

In the following I want to use f[data,x] for definition of the objective function and minimize it on various 'data' vectors and at a given set of points 'x' (x1,…, xn).

• Do you mean to only sample h at integer steps? – IPoiler Jan 26 '16 at 16:52
• Can you be more specific about "both do not work"? How don't they work? Does it spit out errors? Can you provide an example h and an example data set? When you say "minimize it on various data vectors and at a given set of points 'x'", do you mean find the data that minimizes f at a particular x or something else? Please provide more details. – march Jan 26 '16 at 16:52
• @march and IPoiler, thanks for your comments, please see the updated OP. – Alexander Jan 26 '16 at 17:53
• The error is related to the fact that what you are attempting to use to the right of the /. is not valid. The right of the /. must be either Rules, Associations, or a Dispatch. Interpolation returns an InterpolatingFunction object, which is none of those types, and thus cannot be used with /.. You may want to omit the g[x]/. in that case and just let f be the InterpolatingFunction. – IPoiler Jan 26 '16 at 18:12

Functionally, this definition of f works the way I think you're trying to describe your needs:
f[data_, x_] := Interpolation[Table[{h[i], data[[i]]}, {i, 1, Length[data]}], x];

Though, I have reservations about this method with large data vectors because it requires interpolating for every x that's plotted. It'd be better to to create the InterpolatingFunction once for a data set then plot it.