# How to improve the nonlinear fit to a data set [closed]

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

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.

## closed as off-topic by MarcoB, Bob Hanlon, user9660, Jason B., Sjoerd C. de VriesJan 6 '16 at 12:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, Bob Hanlon, Community, Jason B., Sjoerd C. de Vries
If this question can be reworded to fit the rules in the help center, please edit the question.

• "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? – JimB Jan 5 '16 at 17:55
• 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? – glS Jan 5 '16 at 17:59
• 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. – george2079 Jan 5 '16 at 19:22

## 1 Answer

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]}]