# Find a fitting curve to data points

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}}] Maybe I'm not using the correct fitting model here, so I got stuck for a while. Any help or suggestion will be appreciated!

• Please include a Plot of your result (curve and data superimposed) and describe what would constitute an ideal result. Dec 28, 2021 at 3:05
• 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].
– JimB
Dec 28, 2021 at 3:38
• An alternative function to try is a + b/x^c.
– JimB
Dec 28, 2021 at 3:39
• Thanks! @JimB. It worked as following your suggestion now. I think I focused on the wrong function too long previously.
– J. W
Dec 28, 2021 at 3:47
• 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. Dec 28, 2021 at 3:49

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,
Tooltip[Point[#], #] & /@ data},
PlotRange -> {0.02, 0.14}] On a log scale,

LogLinearPlot[nlm[x], {x, 8, 1024},
Epilog -> {Red, AbsolutePointSize,
Tooltip[Point[{Log[#[]], #[]}], #] & /@ data},
PlotRange -> {0.02, 0.14}]
` • Thanks a lot! I was just addicted to the wrong function too long to get the correct result
– J. W
Dec 28, 2021 at 3:49