Some data to begin with

data = {{0, 0}, {0.05, 0.5786}, {0.5, 0.7202}, {1, 0.7902}, {2, 0.8978},
        {3, 0.9246}, {4, 0.9620}, {5, 0.993}, {6, 1}, {7, 1}, {8, 1}, 
        {9, 1}, {10, 1}};

I know from theory that the best fist to such data is the formula $0.5(1 + tanh(a + bx))$.

So I use the NonlinearModelFit

fit = NonlinearModelFit[data, 0.5*(1 + Tanh[a*x + b]), {a, b, c}, x]

which gives

enter image description here

where the dots correspond to the data, while the solid line to the fit. As we can see the result is not so good.

So, is there a way to obtain a better fit to my data? When I say better I mean that the solid fit line should pass very close from all given data points.

  • 1
    $\begingroup$ "In theory there is no difference between theory and practice; in practice there is." What are the consequences of your data not matching the theory? Instrument problems? Theory is wrong? And if you just need a fit with no interpretation of the coefficients, why not just perform a linear interpolation between points? $\endgroup$ – JimB Jan 5 '16 at 17:55
  • 4
    $\begingroup$ are you sure about your data? The curve does not seem to match well a Tanh. If you remove the {0,0} point it gets much better. Is it possible that that point is wrong? $\endgroup$ – glS Jan 5 '16 at 17:59
  • $\begingroup$ Actually just dropping the first point and fixing c=1/2 is all you need. Note the given form can not pass through (0,0) and the data clearly asymptotes to 1 so you can fix c. $\endgroup$ – george2079 Jan 5 '16 at 19:22

The suggestion give by george2079 works quite well.

workingData = Rest @ data;
fit = NonlinearModelFit[workingData, c (1 + Tanh[a x + b]), {a, b, c}, x]
Plot[fit[x], {x, 0, 10},
  PlotRange -> {0, 1.03},
  Epilog -> {PointSize[Medium], Point[workingData]}]



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