Message Boards Message Boards

0
|
6601 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

NonlinearModelFit with Interpolated Function

Posted 10 years ago

Hi all,

To do some data analyzation, I would like to fit my data with an interpolated function which I get from theoretical calculations.

That's the code: I import the theoretical data (attached) and make an interpolation. Additionally I import my data that is to be fitted (attached). I already try to move the data into the range of the fitting function:

datenFunction = Import[".../r_ 5um__G _ 0.06kPa_Int _Phase.txt", "Table", "NumberPoint" -> "."];  (*Theoretical data*)
datenExperiment = Import["...../matlab.txt", "Table", "NumberPoint" -> ","]; (*Data to be fitted*)
E0 = datenFunction[[All, 1]];
phase = datenFunction[[All, 2]];
iPhase = Interpolation[phase];
iE = Interpolation[E0];
time = ({datenExperiment[[All, 1]]} - Mean[datenExperiment[[All, 1]]])*10000 + 5;
data = datenExperiment[[All, 2]];
dataExperiment2 = MapThread[List, {time[[1]], data}];
iData = Interpolation[dataExperiment2];

Show[ListPlot[ E0], Plot[iE[t], {t, 1, length}]]
Show[ListPlot[ phase], Plot[iPhase[t], {t, 1, length}]]
Show[Plot[iData[t], {t, 2, 6}, PlotRange -> All], ListPlot[dataExperiment2]]

I define and plot the interpolated theory function using maipulate:

tFirst = dataExperiment2[[1, 1]]; (*First time value*)
tLast = dataExperiment2[[-1, 1]]; (*Last time value*)
mitte = (tFirst + tLast)/ 2 ;(*Mid point in time*)
IntFirst = Mean[dataExperiment2[[1 ;; 15, 2]]]; (*Mean intensity before cell*)
IntLast = Mean[dataExperiment2[[-15 ;; -1,  2]]] ;(*Mean intensity after cell*)
IntMean = Mean[{IntFirst, IntLast}]; (*Total mean intensity*)
IntMax = Max[dataExperiment2[[;; , 2]]] ;(*Maximal intensity*)
IntMin = Min[dataExperiment2[[;; , 2]]]; 

interference2[dPhase_, ERef_, t_, I0_, a_, b_, dt_, phase0_] := I0 + a*(ERef^2*Cos[phase0 + iPhase[b (t - dt)] + dPhase]^2 +  iE[b (t - dt)]^2*Cos[phase0 + iPhase[b (t - dt)]]^2 + 2*ERef*iE[b (t - dt)]*Cos[phase0 + iPhase[b (t - dt)] + dPhase]* Cos[phase0 + iPhase[b (t - dt)]]);

Manipulate[Show[Plot[iData[t], {t, 2, 6}, PlotRange -> All], Plot[interference2[dPhase, ERef, t, I0, a, b, dt, phase0], {t, tFirst, tLast}, PlotRange -> All]], {{dPhase, 1.2}, 0,  2*Pi}, {{phase0, 2}, 0, 2*Pi}, {{ERef, IntMean}, 0,  1.5 IntMean}, {{I0, 0.03}, -2, 2}, {{a, -0.3}, -10, 10}, {{b, 70}, -1, 100}, {{dt, -mitte + 7.8}, tFirst, tLast}]

From this I can see, that interpolated theory function and data approximately overlap. However, I already get some errors:

InterpolatingFunction::dmval: Input value {-110.239} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {-110.239} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {-110.239} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation. >>

If I move the sliders, the function changes correctly, but further similar erros are produced.

What I actually want to do, is to fit the data with the interpolated function:

fit = NonlinearModelFit[dataExperiment2, interference2[ERef, t, IntMean - 2 a (1 + Cos[dPhase]), dPhase, a,  b, dt, phase0], {{ERef, IntMean}, {dt, -mitte + 7.8}, {dPhase, 1.2}, {a, -0.3}, {I0, 0.03}, {b, 70}, {phase0, 2}}, t];
Show[ListPlot[dataExperiment2], Plot[fit[t], {t, tFirst, tLast}, PlotRange -> All]]
fit["ParameterTable"]

InterpolatingFunction::dmval: Input value {-140.148} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {-140.148} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {-140.148} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation. >>
InterpolatingFunction::dmval: Input value {-199.877} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {-199.877} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {-199.877} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation. >>
InterpolatingFunction::dmval: Input value {-199.877} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {-199.877} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {-199.877} lies outside the range of data in the interpolating function. Extrapolation will be used.             
General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation. >>

Additionally, the "fit" (only some curve going somewheare through the data) and the parameter table are shown.

It seems, as if mathematica has a problem to shift the interpolated function into the range of the data to be fitted.

Does anyone has an idea what I can do in order to match the interpolated function with the data? I would be pleased for any hint.

Thanks a lot!

Attachments:
POSTED BY: Ka Me
2 Replies
Posted 10 years ago

Sean, Thanks for your reply! In the meantime, also following your hints, I am able to make the fit:

  1. I ignore the error messages.

  2. I had already applied appropriate start values. But now I have further confined them using Exp[] for positive values and 2ArcTan[] for angles between -pi and pi.

  3. Possible. In my case, I additionally had done some mistake while calling the varibles in the interpolated function using NonLinearModelFit

  4. You are certainly right about the "not good" practise of this method. This program is just a test. Later on, I will hopefully parametrize everything. But as this is rather complicated, I first would like to test everything with "a fiew" interpolated points.

Thanks again!

POSTED BY: Ka Me
  1. The error messages are saying that the interpolation is being sampled outside of the range it was defined in. I guess for your purposes you can ignore them. The "Off" function allows you to turn off certain error messages do that they don't bother you.

  2. To help NonlinearModelFit find the values you are looking for, try giving it starting values. Look at this example for an example that's like yours: http://mathematica.stackexchange.com/questions/9601/nonlinearmodelfit-problem

  3. There are many examples where "eyeing" a solution with Manipulate doesn't guarantee a good result. It's possible that the result you see with your eyes isn't actually the optimal solution.

  4. As a last note, using an Interpolation as a parametric model is generally not considered good practice. Or at least it's not good practice in any statistics I've done or seen. A forumula built with an Interpolation of some data just can't qualify as a parametric statistical model and wouldn't be generated by any theoretical calculations. Interpolations aren't theoretical in nature. You should consult a statistician about this.

POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract