2
$\begingroup$

So I'm trying to fit data, for all my data, the same fitting function can be used. Sometimes the fits actually FIT really well. Sometimes they just look completely random. Here's an example of it working well. I have two lists "p365UIe" and "p365GitterUIe" where the first part is a list of the x-values, the second part is the measured y-values and the last part is the measurement errors.

p365UIe = {{-1.502, -2.002, -2.500, -3.001}, {67, -12, -23, -25}, {1, 
    0.5, 0.5, 0.5}};
p365GitterUIe = {p365UIe[[1]], {34, -6.1, -11.5, -12.5}, {1, 0.1, 0.5,
     0.5}};
Needs["ErrorBarPlots`"];
p365 = Table[{{p365UIe[[1]][[i]], p365UIe[[2]][[i]]}, 
   ErrorBar[p365UIe[[3]][[i]]]}, {i, 1, 4}]
p365Gitter = 
 Table[{{p365GitterUIe[[1]][[i]], p365GitterUIe[[2]][[i]]}, 
   ErrorBar[p365GitterUIe[[3]][[i]]]}, {i, 1, 4}]

So I've created two lists that can be plotted with ErrorListPlot. Now I come to the fitting and showing everything.

f365 = NonlinearModelFit[
   Table[{p365UIe[[1]][[i]], p365UIe[[2]][[i]]}, {i, 1, 4}], 
   a*(Exp[(x - b)/c] - 1), {a, b, c}, x];
f365Gitter = 
  NonlinearModelFit[
   Table[{p365GitterUIe[[1]][[i]], p365GitterUIe[[2]][[i]]}, {i, 1, 
     4}], a*(Exp[(x - b)/c] - 1), {a, b, c}, x];
Show[ErrorListPlot[p365, PlotStyle -> PointSize[0.008]], 
 ErrorListPlot[p365Gitter, PlotStyle -> PointSize[0.008]], 
 Plot[Normal[f365], {x, -3, -1}], 
 Plot[Normal[f365Gitter], {x, -3, -1}], ImageSize -> Large]

The result is this beautiful fit:

Working fit

Now for the example that doesn't work. I've done exactly the same thing, just with different measurement data, as far as I know.

p436UIe = {{-1.253, -1.500, -1.749, -1.994}, {45, -27, -52, -58}, {1, 
    0.5, 1, 1}};
p436GitterUIe = {p436UIe[[1]], {25.5, -14, -26, -30}, {0.5, 0.5, 0.5, 
    1}};
p436 = Table[{{p436UIe[[1]][[i]], p436UIe[[2]][[i]]}, 
   ErrorBar[p436UIe[[3]][[i]]]}, {i, 1, 4}]
p436Gitter = 
 Table[{{p436GitterUIe[[1]][[i]], p436GitterUIe[[2]][[i]]}, 
   ErrorBar[p436GitterUIe[[3]][[i]]]}, {i, 1, 4}]
f436 = NonlinearModelFit[
   Table[{p436UIe[[1]][[i]], p436UIe[[2]][[i]]}, {i, 1, 4}], 
   a*(Exp[(x - b)/c] - 1), {a, b, c}, x];
f436Gitter = 
  NonlinearModelFit[
   Table[{p436GitterUIe[[1]][[i]], p436GitterUIe[[2]][[i]]}, {i, 1, 
     4}], a*(Exp[(x - b)/c] - 1), {a, b, c}, x];
Show[ErrorListPlot[p436, PlotStyle -> PointSize[0.008]], 
 ErrorListPlot[p436Gitter, PlotStyle -> PointSize[0.008]], 
 Plot[Normal[f436], {x, -3, -1}], 
 Plot[Normal[f436Gitter], {x, -3, -1}], ImageSize -> Large]

Now, for some reason, the fit "f436Gitter" doesn't fit at all:

Fit not working

Can someone tell me where I did something wrong? Thank you!

$\endgroup$
  • $\begingroup$ Attempting to fit 4 parameters (a, b, c, and $\sigma^2$) with just 4 data points is a recipe for disaster and just plain silly. Over-parameterized models (generally not enough data points relative to the number of parameters not to mention a simple curve with too many parameters) have estimation problems in all estimation packages not just Mathematica. $\endgroup$ – JimB Jun 18 '16 at 17:05
3
$\begingroup$

In such cases you can provide an initial guess. You can get an idea of the value from the other set. For example

FindFit[Table[{p436UIe[[1]][[i]], p436UIe[[2]][[i]]}, {i, 1, 4}], 
               a*(Exp[(x - b)/c] - 1), {a, b, c}, x]

{a -> 62.2442, b -> -1.3725, c -> 0.219432}

Which you know gives correct result.

For the other set it is

FindFit[Table[{p436GitterUIe[[1]][[i]], p436GitterUIe[[2]][[i]]}, {i, 1, 4}],
               a*(Exp[(x - b)/c] - 1), {a, b, c}, x]

{a -> 11.125, b -> -49.4742, c -> -0.370934}

which gives you wrong fit. So you can use the first set of values as your initial guess.

FindFit[Table[{p436GitterUIe[[1]][[i]], p436GitterUIe[[2]][[i]]}, {i, 1, 4}],
               a*(Exp[(x - b)/c] - 1), {{a, 62.}, {b, -1.}, {c, 0.21}}, x]

{a -> 31.5837, b -> -1.37758, c -> 0.210551}

f436Gitter=NonlinearModelFit[Table[{p436GitterUIe[[1]][[i]],p436GitterUIe[[2]][[i]]}
,{i, 1, 4}], a*(Exp[(x - b)/c] - 1), {{a, 62.}, {b, -1.}, {c, 0.21}}, x];
Show[ErrorListPlot[p436, PlotStyle -> PointSize[0.008]], 
ErrorListPlot[p436Gitter, PlotStyle -> PointSize[0.008]], 
Plot[Normal[f436], {x, -3, -1}], 
Plot[Normal[f436Gitter], {x, -3, -1}], ImageSize -> Large]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you! Works now. $\endgroup$ – Keno Goertz Jun 18 '16 at 11:51
3
$\begingroup$

As often when things go wrong, this is a question of initial values. Plugging in the approximate parameter values from f436 gives us:

f436["BestFitParameters"]

{a -> 62.2442, b -> -1.3725, c -> 0.219432}

f436Gitter = 
  NonlinearModelFit[
   Table[{p436GitterUIe[[1]][[i]], p436GitterUIe[[2]][[i]]}, {i, 1, 
     4}], a*(Exp[(x - b)/c] - 1), {{a, 60}, {b, -.1}, {c, 0.2}}, x];

And the result:

enter image description here

Where:f436Gitter["BestFitParameters"] gives us:

{a -> 31.5837, b -> -1.37758, c -> 0.210551}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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