我正在尝试使用幂律分布将幂律拟合到我的数据。这是我从中提取值然后尝试曲线拟合到提取的数据中的样本数据。 Sampledata。样本数据包含单个文本文件&从这个文件我提取两列数据。我想将幂律拟合到该数据中并从Y = A *(X ^ B)获得系数A和B.我期望B的负值,因为它是一个递减的曲线。
解答:我使用了Scipy Optimize曲线拟合。这是有效的代码。希望它对某人有所帮助。
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from scipy.optimize import curve_fit
###############################################################################
def centerline1(V1,Y1):
fig = plt.figure(1)
ax = plt.subplot()
ax.plot(Y1,V1)
plt.hold(True)
ax.set_xlabel('Y (m)')
ax.set_ylabel('V_center/V_nozzle')
ax.set_title('Plane Jet')
ax.legend(['ddn110A'],loc =0)
ax.grid(True)
plt.tight_layout()
plt.figtext(0.6,0.40,"Flow Time = 50s",style= 'normal',alpha=0.5)
flowtime1 = input('Enter the flowtime for ddn110A: ')
flowtime2 = input('Enter the flowtime for ddn110B: ')
FT_init1 = 3.61
delt = 0.15
TS_init1 = 150
V_initial =0.236
b_O = 0.6096
timestep1 = (flowtime1-FT_init1)/delt
timestep1 = int(round(timestep1 + TS_init1))
timestepstring1=str(timestep1).zfill(4)
path1 = "path"
fname1 = path1+"c1-"+timestepstring1+".txt"
f1 = open(fname1,'r')
data1 = np.loadtxt(f1,skiprows=1)
V1 = data1[:,4]
V1 = V1/(V_initial)
firstIdx = np.nonzero(V1==V1.min())[0][0]
Y1 = data1[:,2] # Assigning Y to column 5 from the text file
Y1 = Y1/(b_O) # Dividing Y by bo which is the half width
s = (2*1.2192)
Y1 = Y1[s:firstIdx]
V1= V1[s:firstIdx]
centerline1(V1,Y1)
#################################################################
def func1(x,C,m):
return C*np.array((x**m))
xdata1 = Y1
ydata1 = V1
popt1,pcov1 =curve_fit(func1,xdata1,ydata1,p0=[5,-1]) ## (Optimize Curve Fit from Scipy)
perr1 = np.sqrt(np.diag(pcov1))