to fit function y = Alpha * POW (x, Beta)
Input: x array, y array
Output: Alpha,beta, correlation coefficient R2
From scipy.optimize import leastsqfrom pylab import *import numpy as Npxdata = Np.array ([4.79616, 11.63, 37.5534, 105.414] ) Ydata = Np.array ([1.33921, 0.755319, 0.34085, 0.0554339]) # Define function for calculating a power Lawpowerlaw = lambda x , Alpha, Beta:alpha * (x * * Beta) LOGX = log10 (xdata) logy = log10 (ydata) # define our (line) fitting functionfitfunc = Lamb Da P, x:p[0] + p[1] * x errfunc = lambda p, x, Y: (Y-fitfunc (P, x)) Pinit = [1.0, -1.0]out, CoV, infodict, MESG, ier = LEASTSQ (Errfunc, pinit, args = (LOGX, logy), full_output=1) beta = Out[1]alpha = 10.0 * * Out[0]ss_err = (infodict[' F VEC '] * * 2). SUM () Ss_tot = ((Ydata-ydata.mean ()) * * 2). SUM () r2 = 1-(ss_err/ss_tot) print (' Alpha:%f, Beta:%f '% (Alph A, beta)) print (' R2:%f '% r2)
Python hyperbola Fitting