# Problem with NMinimize and NonlinearModelFit

I have data points of some experiment and system of ODE, that approximate this data.

data = {{0, 0}, {2.5, 0}, {20, 455.295}, {27, 693.52}, {48,
1521.57}, {72, 1557.94}};
k4 = 1;
k5 = 0.0395;
OD = {m'[t] == -a*m[t] - b*v[t]*m[t]  ,
v'[t] == c*i[t] - k4*v[t]*m[t],
i'[t] == b*v[t]*m[t] - k5*i[t],
m[0] == 10^5, v[0] == 0, i[0] == 10^3};


Then i have three cells:

1

sol = ParametricNDSolve[OD, {m, v, i}, {t, 0, 80}, {a, b, c}];
model[aa_, bb_, cc_] := v[aa, bb, cc] /. sol;


2

fitted = NonlinearModelFit[
data, {model[a, b, c][t], 0.4 < a < 0.51, 0.8 < b < 1,
0 < c < 0.1}, {a, b, c},
t, {Method -> "NMinimize", Method -> "DifferentialEvaluation"}];
Show[ListPlot[data], Plot[fitted[t], {t, 0, 80}]]


3

Clear[Fi, cp]
Fi[aa_, bb_,
cc_] := (model[aa, bb, cc][data[[1]][[1]]] -
data[[1]][[2]])^2 + (model[aa, bb, cc][data[[2]][[1]]] -
data[[2]][[2]])^2 + (model[aa, bb, cc][data[[3]][[1]]] -
data[[3]][[2]])^2 + (model[aa, bb, cc][data[[4]][[1]]] -
data[[4]][[2]])^2 + (model[aa, bb, cc][data[[5]][[1]]] -
data[[5]][[2]])^2 + (model[aa, bb, cc][data[[6]][[1]]] -
data[[6]][[2]])^2;
cp = Table[0.01 + j, {j, 0, 2, 0.1}];
Table[{cp[[i]],
Part[NMinimize[{Fi[a1, b1, cp[[i]]], a1 > 0 , b1 > 0 }, {a1, b1}],
1]}, {i, 1, 2}]


So, when i evaluate first cell and then third, Wolframs output is:

{{0.01, 5.42636*10^6}, {0.11, 5.38385*10^6}}


And this result is incorrect. But if i evaluate simultaneously first and second cell, third cell give me correct result

{{0.01, 38702.}, {0.11, 38702.}}


So, i do not understand what happened. I noticed this problem, when i tried to use ParallelTable, i do not know why, but when i use it, wolfram gives me incorrect results.

• The constraints in second and third cell are different, that's why the results differ I think! Oct 25, 2022 at 9:55
• @Ulrich Neumann, I am not comparing the outputs of the second and third cell, so it does not matter what constraints are .I have different output data of the third cell when executing the second, and it changes in the right direction and I don't understand why it works like this. Oct 25, 2022 at 10:34
• MMA version 13.1 Windows 10. I always get {{0.01, 5.42636*10^6}, {0.11, 5.38385*10^6}} from the third cell with and without cell 2. Oct 25, 2022 at 10:42
• @Daniel Huber, if you evaluate all cells simultaneously after quieting kernel, you will get the same result? Oct 25, 2022 at 10:52
• Also, if you run first and third cells, second cell cant Minimize in a right way :)) Oct 25, 2022 at 11:07

If you simplify first cell to

(* cell 1*)
model = ParametricNDSolveValue[OD, v , {t, 0, 80}, {a, b, c}];


and change third cell to

(* cell 3*)
Clear[min]
min[cc_?NumericQ] := {cc,NMinimize[{# . # &[Map[ model[a, b, c ][ #[[1]] ] - #[[2]]   &, data ]],cc == c}, {a , b, c }][[1]] }
cp = Table[.01 + j, {j, 0, 2, .1}];
Map[min , cp[[1 ;; 2]]]


evaluation of cell 1&3 and cell 1&2&3 give the same result

{{0.01, 38702.}, {0.11, 38702.}}

• Thanks! It really works, even with ParallelMap, but i do not understand, what is the problem? Oct 25, 2022 at 13:41
• @DaniilUdalov You're welcome! Me too I don't know the reason, I' ll think about it. Oct 25, 2022 at 14:15