I'm creating a script for interactive model fitting where the user selects a 'window' of the data to exclude from the fitting.
I can come up with a MWE like the following:
testdata = {{1, 2.9}, {2, 3.2}, {3, 3.2}, {4, 5.1}, {5, 7.1}, {6, 9.1}};
DynamicModule[{data = testdata, fitdata, i1 = {2, 4}},
Panel[
Column[{
IntervalSlider[Dynamic[i1], {1, Length[data], 1},
Enabled -> True], "Min: " Dynamic[i1[[1]]],
" Max: " Dynamic[i1[[2]]]
,
Dynamic[
Show[
ListPlot[{data,
fitdata =
Select[data, First[#] < i1[[1]] || First[#] > i1[[2]] &]},
PlotStyle -> {Blue, Red}],
Plot[
Evaluate[
nlm = a + b*x + c*x^2 /.
NonlinearModelFit[fitdata, a + b*x + c*x^2, {a, b, c}, x][
"BestFitParameters"]], {x, 1, 6}]
]
]
}]
]
]
However, I want to plot the fit curve on top of the data, like above, and then have a separate plot of the residuals and a table of the fit parameters. This is where I run into trouble.
I've tried using With, with no luck:
testdata = {{1, 2.9}, {2, 3.2}, {3, 3.2}, {4, 5.1}, {5, 7.1}, {6, 9.1}};
DynamicModule[{data = testdata, fitdata, i1 = {2, 4}, nlm},
Panel[
Column[{
IntervalSlider[Dynamic[i1], {1, Length[data], 1},
Enabled -> True], "Min: " Dynamic[i1[[1]]],
" Max: " Dynamic[i1[[2]]]
,
Dynamic[
With[
nlm = NonlinearModelFit[
fitdata =
Select[data, First[#] < i1[[1]] || First[#] > i1[[2]] &],
a + b*x + c*x^2, {a, b, c}, x]],
Column[
Show[
ListPlot[{data, fitdata}, PlotStyle -> {Blue, Red}],
Plot[
Evaluate[ a + b*x + c*x^2 /. nlm["BestFitParameters"]], {x,
1, 6}]
],
ListPlot[nlm["FitResiduals"]]
]
]
}]
]
]
Could anyone direct me to how to make nlm 'available' to multiple Plot instances within DynamicModule?
