I have two sets of data that are not regularly sampled, and I want to produce a quantitative measurement of how similar they are while fitting them with an $x$ shift and $x$ and $y$ scaling.
Following the Mathematica interpolation documentation, f = Interpolation[data]
gives me nice graphs and allows me to access one interpolated point at a time via f[x]
.
What I'd like to do is something along the lines of:
experiment = Interpolation[expdata]
FindFit[theorydata, a*experiment[b*x + c], {a, b, c}, x]
where theorydata
and expdata
are non-regularly sampled lists of $x$ and $y$ values.
This successfully inputs $x$ values from the theorydata
list, but just prints them out as FindFit[{theorydata}, {a InterpolatingFunction[{{0., 0.32878}}, <>][0.499755 b + c] ...]
Alternatively, if something like that is not an option, it would be useful to know how to generally work with interpolating functions. For example, I cannot seem to subtract two interpolating functions from each other:
experiment = Interpolation[expdata]
theory = Interpolation[theorydata]
f = experiment - theory
This outputs:
InterpolatingFunction[{{0., 0.32758}}, <>] - InterpolatingFunction[{{0., 0.32878}}, <>]
and, f[0.1]
, for example outputs:
InterpolatingFunction[{{0., 0.32758}}, <>] - InterpolatingFunction[{{0., 0.32878}}, <>][0.1]
I am, however, able to re-sample the interpolated data with:
result = Table[{x, experiment[x]}, {x, 0, 0.3, 0.01}]
This then gives me a regularly sampled list.
In summary, I suppose the biggest question here is how to work with InterpolatingFunction
objects. Can they be added/subtracted? Can they be treated as a function and used in fitting algorithms? How exactly is an InterpolatingFunction
stored in Mathematica?
EDIT: Fixed a typo in the output
Sample of data (small sample of a large number of points):
expdata={{0, 0.032174}, {0.00497, 0.032446}, {0.010701, 0.032923}, {0.015671,
0.033402}, {0.021403, 0.034199}, {0.028477, 0.035362}, {0.033447,
0.036444}, {0.039179, 0.038074}, {0.044149, 0.039642}, {0.049119,
0.041422}}
theorydata={{0, 0.033955}, {0.00497, 0.034502}, {0.010701, 0.035312}, {0.015671,
0.036135}, {0.020641, 0.037109}, {0.026373, 0.038481}, {0.031343,
0.039824}, {0.036313, 0.041344}, {0.041283, 0.043037}, {0.047014,
0.045256}}
Note that that the datasets are very similar, but they are not sampled for all of the same x values.
FindFit
does, i.e. you need to have an analytical model and experimental data points. In your case, you could generate a function that expresses the squared differences between your experimental and corresponding theoretical data points as a function of the shift parameters, and then minimize that function numerically to get the best-fit shift parameters for this the theoretical calculations fit the experimental findings. $\endgroup$