I'm fitting data inside Manipulate and it works good
model = Evaluate[H1*complex[r, k1, 1]];
fitRes = FindFit[data, model, {H1, k1}, r, Method -> "NMinimize"];
but when I'm trying to create the function with found parameters fitted[r_] := model /. fitRes;
to plot found fit the Manipulate starts to recalculate infinitely. Why it happens and how to avoid it?
Here is the working example:
n = 2;
Quiet[solution = Solve[{K*P*M == PM, P0 == P + PM, r*P0 == M + PM}, {P, M, PM}]];
vala = PM /. Take[solution, {n, n}];
complex[rVal_, KVal_, P0Val_, DifInd_: 0] := Re@With[{r = rVal, K = KVal, P0 = P0Val,índex = DifInd}, (Evaluate@D[vala, {r, índex}])];
rmin = 0.1;
Manipulate[
data = Table[Evaluate[{r, H*complex[r, k, 10^p0, 1][[1]]}], {r, rmin, rmax, 0.1}];
rnd = Table[{0, RandomReal[{-ee, ee}]}, {r, rmin, rmax, 0.1}];
model = Evaluate[H1*complex[r, k1, 10^p0, 1]];
fitRes = FindFit[data + rnd, model, {H1, k1}, r, Method -> "NMinimize"];
fitted[r_] = model /. fitRes;
Row[{Show[{ListPlot[data + rnd, PlotMarkers ->
Graphics[{Red, Thick, Circle[]}, ImageSize -> 10]]}]}],
{{rmax,2, "r"}, 1, 10},
{{k, 1000001, "k"}, 10001, 10000001},
{{p0, -3.5, "10^P0"}, -1, -10}, Delimiter,
{{H, -100,"H"}, -300, 300},
{{ee, 0.0015, "Error"}, -0.1, 0.1},
ControlPlacement -> Left]