3
$\begingroup$

The following fit runs forever on my laptop; how to speed up?

Bdata = {{1.17, 400}, {2, 800}, {4, 1100}, {6, 1350}, {8, 1500}};

B = ParametricNDSolveValue[{b'[t] == kappa (1 - b[t]/1500), b[1.17] == b0}, b, {t, 0, 11}, {b0, kappa}]

J[b0_?NumericQ, kappa_?NumericQ] := Total@Map[(B[b0, kappa][#[[1]]] - #[[2]])^2 &, Bdata]

mini = NMinimize[{J[b0, kappa], 350 < b0 < 450, 0 < kappa < 1}, {b0, kappa}]
$\endgroup$
1
  • 1
    $\begingroup$ The final calculation for mini takes about 51 seconds on my computer. {2.46*10^6,{b0->450.,kappa->-2.10153*10^-30}}. Version 14 on Windows 11. Intel I9 13900 24 core processor with 64 GB ram. $\endgroup$
    – Bill Watts
    Feb 22 at 8:26

3 Answers 3

5
$\begingroup$

Remove the parameter constraints and you'll get a good fit

mini = NMinimize[{J[b0, kappa] }, {b0, kappa}]
(*{13054.2, {b0 -> 426.03, kappa -> 621.01}}*)

Show[{ListPlot[Bdata], Plot[B[b0, kappa][x] /. mini[[2]], {x, 0, 10}]}]

enter image description here

$\endgroup$
4
$\begingroup$
(* nlf = NonlinearModelFit[Bdata,  
  B[b0, kappa]@t, {{b0, 350, 450}, {kappa, 0, 1}}, t] *)

nlf = NonlinearModelFit[Bdata, B[b0, kappa]@t, {b0, kappa}, t]

Show[ListPlot[Bdata, PlotStyle -> Red], Plot[nlf@t, {t, 0, 11}]]

enter image description here

nlf["BestFitParameters"]

{b0 -> 426.03, kappa -> 621.009}.

nlf["ParameterConfidenceIntervals"]

{{237.826, 614.234}, {364.814, 877.205}}.

$\endgroup$
0
1
$\begingroup$
$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

Bdata = {{117/100, 400}, {2, 800}, {4, 1100}, {6, 1350}, {8, 1500}};

The differential equation can be solved exactly.

B = DSolveValue[{b'[t] == kappa  (1 - b[t]/1500), b[117/100] == b0}, b[t], 
   t] // Simplify

(* 1500 + (-1500 + b0) E^((kappa (117 - 100 t))/150000) *)

(nlm = NonlinearModelFit[Bdata, B, {kappa, b0}, t])["BestFitParameters"]

(* {kappa -> 621.01, b0 -> 426.03} *)

Show[ListPlot[Bdata], Plot[nlm[x], {x, 0, 10}]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.