Search code examples
pythonscipyleast-squares

least squares curve fitting


I have a set of distances x=c*r/rs

 array([ 0.09317335,  0.1863467 ,  0.27952006,  0.37269341,  0.46586676,
        0.55904011,  0.65221346,  0.74538682,  0.83856017,  0.93173352,
        1.02490687,  1.11808022,  1.21125357,  1.30442693,  1.39760028,
        1.49077363,  1.58394698,  1.67712033,  1.77029369,  1.86346704])

and number density (sigma) array([ 9.56085037e+14, 5.13431506e+14, 3.26960286e+14, 2.27865084e+14, 1.68325130e+14, 1.29590176e+14, 1.02918831e+14, 8.37487042e+13, 6.94971037e+13, 5.86086377e+13, 5.00994710e+13, 4.33218850e+13, 3.78349864e+13, 3.33300619e+13, 2.95856349e+13, 2.64394232e+13, 2.37702922e+13, 2.14863249e+13, 1.95167455e+13, 1.78063354e+13])

which I have plotted to get the following graph. It is a log log plot.

I have a function enter image description here

which should fit my graph according to theory. I don't how to use scipy.opt.leastsquare to use the function and fit my graph. The parameters to fit are c and rs


Solution

  • Option 1: Using scipy.optimize.curve_fit

    Option 2: write your own function to output R2 or sse, then minimize this function using scipy.optimize. I've always used this method for complicated problems and would recommend the algorithms of SLSQP and L-BFGS-B.