Это мой подход не с линейными, а с квадратичными c функциями.
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
def soft_step(x, s): ### not my usual np.tanh() ...OMG
return 1+ 0.5 * s * x / np.sqrt( 1 + ( s * x )**2 )
### for the looks of the data I decided to go for two parabolas with
### one discontinuity
def fit_func( x, a0, b0, c0, a1, b1, c1, x0, s ):
out = ( a0 * x**2 + b0 * x + c0 ) * ( 1 - soft_step( x - x0, s ) )
out += ( a1 * x**2 + b1 * x + c1 ) * soft_step( x - x0, s )
return out
### with global parameter for iterative fit
### if using least_squares one could avoid globals
def fit_short( x, a0, b0, c0, a1, b1, c1, x0 ):
global stepwidth
return fit_func( x, a0, b0, c0, a1, b1, c1, x0, stepwidth )
### getting data
xl = np.loadtxt( "binf11.dat" )
yl = np.loadtxt( "binp11.dat" )
el = np.loadtxt( "bine11.dat" )
### check for initial values
p0 = [ 0, -2,-11, 0, -2, -9, -3, 10 ]
xth = np.linspace( -5.5, -1.5, 250 )
yth = np.fromiter( ( fit_func(x, *p0 ) for x in xth ), np.float )
### initial fit
sol, pcov = curve_fit( fit_func, xl, yl, sigma=el, p0=p0, absolute_sigma=True )
yft = np.fromiter( ( fit_func( x, *sol ) for x in xth ), np.float )
sol=sol[: -1]
###iterating with fixed and decreasing softness in the step
for stepwidth in range(10,55,5):
sol, pcov = curve_fit( fit_short, xl, yl, sigma=el, p0=sol, absolute_sigma=True )
### printing the step position
print sol[-1]
yiter = np.fromiter( ( fit_short(x, *sol ) for x in xth ), np.float )
print sol
###plotting
fig = plt.figure()
ax = fig.add_subplot( 1, 1, 1 )
# ~ax.plot( xth, yth ) ### no need to show start parameters
ax.plot( xth, yft ) ### first fit with variable softness
ax.plot( xth, yiter ) ### last fit with fixed softness of 50
ax.errorbar( xl, yl, el, marker='o', ls='' ) ### data
plt.show()
Это дает:
-3.1762721614559712
-3.1804393481217477
-3.1822672190583603
-3.183493292415725
-3.1846976088390333
-3.185974760198917
-3.1872472903175266
-3.188427041827035
-3.1894705102541843
[ -0.78797351 -5.33255174 -12.48258537 0.53024954 1.14252783 -4.44589397 -3.18947051]
и

установка скачка на -3,189