0
$\begingroup$

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).

$\endgroup$
  • $\begingroup$ Do you mean to only sample h at integer steps? $\endgroup$ – IPoiler Jan 26 '16 at 16:52
  • $\begingroup$ 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. $\endgroup$ – march Jan 26 '16 at 16:52
  • $\begingroup$ @march and IPoiler, thanks for your comments, please see the updated OP. $\endgroup$ – Alexander Jan 26 '16 at 17:53
  • $\begingroup$ 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. $\endgroup$ – IPoiler Jan 26 '16 at 18:12
0
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Oh, it was that straightforward! Thank you for your help and last comment as well. $\endgroup$ – Alexander Jan 27 '16 at 9:41

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.