3
$\begingroup$

I would like to fit a function to a set of data:

data = {
  {8, 0.138177}, {16, 0.0789974},
  {32, 0.0479225}, {64, 0.0351548},
  {128, 0.0299134}, {1024, 0.0277813}}

I tried something like:

nlm = 
  NonlinearModelFit[
    data,
    {a + b Log[c x], c > 0},
    {a, b, c}, x]

But I still can't get an ideal result. The plot looks like this:

Plot[nlm["BestFit"], {x, 0, 1024}, PlotRange -> {{0, 1030}, {0, 0.2}}, BaseStyle -> {FontFamily -> "Times", 16}, Frame -> True, FrameStyle -> Black, FrameLabel -> {{"Relative error", ""}, {"Time increments", ""}}, Epilog -> {{Red, PointSize[Large], Point@data}}]

enter image description here

Maybe I'm not using the correct fitting model here, so I got stuck for a while. Any help or suggestion will be appreciated!

$\endgroup$
6
  • 3
    $\begingroup$ Please include a Plot of your result (curve and data superimposed) and describe what would constitute an ideal result. $\endgroup$
    – bbgodfrey
    Commented Dec 28, 2021 at 3:05
  • $\begingroup$ Fitting 4 parameters (a, b, c, and error variance) with just 6 data points is likely (even with a "bad looking" fit) to be overly optimistic. Please consider including single prediction confidence bands to get an idea of the associated error in fit: Plot[ nlm["SinglePredictionBands"], {x,8,1024}, PlotRange->All]. $\endgroup$
    – JimB
    Commented Dec 28, 2021 at 3:38
  • 2
    $\begingroup$ An alternative function to try is a + b/x^c. $\endgroup$
    – JimB
    Commented Dec 28, 2021 at 3:39
  • $\begingroup$ Thanks! @JimB. It worked as following your suggestion now. I think I focused on the wrong function too long previously. $\endgroup$
    – J. W
    Commented Dec 28, 2021 at 3:47
  • 3
    $\begingroup$ Note that your model was also not great from a fitting perspective because your parameters were correlated: $a+b\log{c x}=a+b(\log{c}+\log{x})=a+b\log{c}+b\log{x}$ which would essentially be the same as using $a+b\log{x}$ directly. $\endgroup$
    – MarcoB
    Commented Dec 28, 2021 at 3:49

1 Answer 1

8
$\begingroup$
Clear["Global`*"]

data = {{8, 0.138177}, {16, 0.0789974}, {32, 0.0479225},
   {64, 0.0351548}, {128, 0.0299134}, {1024, 0.0277813}};

Using a different model

(nlm = NonlinearModelFit[data, {a + b*x^c, c < 0},
    {a, b, c}, x]) // Normal

(* 0.0257578 + 1.20405/x^1.13815 *)

Plotting,

Plot[nlm[x], {x, 8, 1024},
 Epilog -> {Red, AbsolutePointSize[4],
   Tooltip[Point[#], #] & /@ data},
 PlotRange -> {0.02, 0.14}]

enter image description here

On a log scale,

LogLinearPlot[nlm[x], {x, 8, 1024},
 Epilog -> {Red, AbsolutePointSize[4],
   Tooltip[Point[{Log[#[[1]]], #[[2]]}], #] & /@ data},
 PlotRange -> {0.02, 0.14}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks a lot! I was just addicted to the wrong function too long to get the correct result $\endgroup$
    – J. W
    Commented Dec 28, 2021 at 3:49

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.