python nonlinear least squares fitting

前端 未结 2 1268
灰色年华
灰色年华 2020-12-13 03:11

I am a little out of my depth in terms of the math involved in my problem, so I apologise for any incorrect nomenclature.

I was looking at using the scipy function l

2条回答
  •  孤城傲影
    2020-12-13 03:40

    Another option is to use lmfit.

    They provide a great example to get you started:.

    #!/usr/bin/env python
    #
    from lmfit import minimize, Minimizer, Parameters, Parameter, report_fit
    import numpy as np
    
    # create data to be fitted
    x = np.linspace(0, 15, 301)
    data = (5. * np.sin(2 * x - 0.1) * np.exp(-x*x*0.025) +
            np.random.normal(size=len(x), scale=0.2) )
    
    # define objective function: returns the array to be minimized
    def fcn2min(params, x, data):
        """ model decaying sine wave, subtract data"""
        amp = params['amp']
        shift = params['shift']
        omega = params['omega']
        decay = params['decay']
        model = amp * np.sin(x * omega + shift) * np.exp(-x*x*decay)
        return model - data
    
    # create a set of Parameters
    params = Parameters()
    params.add('amp',   value= 10,  min=0)
    params.add('decay', value= 0.1)
    params.add('shift', value= 0.0, min=-np.pi/2., max=np.pi/2)
    params.add('omega', value= 3.0)
    
    
    # do fit, here with leastsq model
    minner = Minimizer(fcn2min, params, fcn_args=(x, data))
    kws  = {'options': {'maxiter':10}}
    result = minner.minimize()
    
    
    # calculate final result
    final = data + result.residual
    
    # write error report
    report_fit(result)
    
    # try to plot results
    try:
        import pylab
        pylab.plot(x, data, 'k+')
        pylab.plot(x, final, 'r')
        pylab.show()
    except:
        pass
    
    #
    

提交回复
热议问题