curve_fit指令是由一点小问题的。

```from scipy.optimize import curve_fit
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams["font.sans-serif"] = ["SimHei"]
matplotlib.rcParams["axes.unicode_minus"] = False```

```def fun(t,a,b,c):
return a + b*t + c*t**2```

```#bounds
bounds = [[0,0,0],[1,2,3]]
#表示 amin,bmin,cmin ; amax,bmax,cmax```

```x = [0.01 * i for i in range(1000)]
y = [0.7+1.5*i+2.1*i**2 for i in x]
p_fit,pcov = curve_fit(fun,x,y,bounds=bounds)
a,b,c = p_fit.tolist()
print("a "+str(a))
print("b "+str(b))
print("c "+str(c))
>>>a 0.7000000000000016
>>>b 1.4999999999999993
>>>c 2.1```

tips  实际工作的时候，你会发现，这个工作绝对没有我这里展示得这幺简单。

parameters

f
xdata
ydata
bounds

return

popt

pcov

Raises

ValueError

xdata 或 ydata 中含有 Not a Number

RuntimeError

OptimizeWarning

 V0 11 a -0.00049888 b -0.294891 c 0.00474997 d -8.96037e-07

```import xlrd as xl
import matplotlib.pyplot as plt
import matplotlib
import math
from scipy.optimize import curve_fit
matplotlib.rcParams["font.sans-serif"] = ["SimHei"]
matplotlib.rcParams["axes.unicode_minus"] = False
data = xl.open_workbook("附件2 电池放电测试（30A）采样数据.xls")
work_sheet = data.sheets()[0]
all_rows = work_sheet.nrows
time,voltage = [0 for i in range(all_rows-1)],[0 for i in range(all_rows-1)]
for i in range(1,all_rows):
time[i-1] = work_sheet.cell_value(i,0)
voltage[i-1] = work_sheet.cell_value(i,1)
#plt.plot(time,voltage,label="电池放电")
#plt.xlabel("放电时间")
#plt.ylabel("电压",rotation=True)
#plt.legend()
#plt.savefig("电池放电原始记录,jpg")
#plt.cla()
#plt.plot(time[5:],voltage[5:],label="电池放电")
#plt.xlabel("放电时间")
#plt.ylabel("电压",rotation=True)
#plt.legend()
#plt.savefig("电池放电清洗后记录,jpg")
start = 50
time = time[start:]
voltage = voltage[start:]
plt.scatter(time,voltage,marker="o",c="red",s=0.5)
def fun(t,v0,a,b,c,d):
#v0*math.e**(a*t)+
#+(b*t+c*t**2+d*t**3)
#return v0*math.e**(a*t)+(b+c*t+d*t**2)
return v0*math.e**(a*t)+(b+c*t+d*t**2)
p_fit,pcov = curve_fit(fun,time,voltage,bounds=[[9,-2,-2,-2,-2],[11,2,2,2,2]])
v0,a,b,c,d = p_fit.tolist()
time0 = [i for i in range(start,int(time[-1]))]
plt.plot(time0,[fun(i,v0,a,b,c,d) for i in time0],linestyle="-.",c="b",linewidth=2)
SSE = sum([(fun(time[i],v0,a,b,c,d)-voltage[i])**2 for i in range(len(time))])
plt.xlabel("放电时间")
plt.ylabel("电压",rotation=True)
plt.savefig("电池放电拟合图.jpg")
plt.show()
print(SSE)```

 V0 1.6 a -0.0011713 b 0.107615 c 0.00107523 d -4.35313e-07

```import xlrd as xl
import matplotlib.pyplot as plt
import matplotlib
import math
from scipy.optimize import curve_fit
matplotlib.rcParams["font.sans-serif"] = ["SimHei"]
matplotlib.rcParams["axes.unicode_minus"] = False
data = xl.open_workbook("附件2 电池放电测试（30A）采样数据.xls")
work_sheet = data.sheets()[0]
all_rows = work_sheet.nrows
time,voltage = [0 for i in range(all_rows-1)],[0 for i in range(all_rows-1)]
for i in range(1,all_rows):
time[i-1] = work_sheet.cell_value(i,0)
voltage[i-1] = work_sheet.cell_value(i,1)
start = 50
time = time[start:]
voltage = voltage[start:]
voltage = [i-9 for i in voltage]
plt.scatter(time,voltage,marker="o",c="red",s=0.5)
def fun(t,v0,a,b,c,d):
#v0*math.e**(a*t)+
#+(b*t+c*t**2+d*t**3)
#return v0*math.e**(a*t)+(b+c*t+d*t**2)
return v0*math.e**(a*t)+(b+c*t+d*t**2)
p_fit,pcov = curve_fit(fun,time,voltage,bounds=[[1.4,-10,-10,-10,-10],[1.6,10,10,10,10]])
v0,a,b,c,d = p_fit.tolist()
time0 = [i for i in range(start,int(time[-1]))]
plt.plot(time0,[fun(i,v0,a,b,c,d) for i in time0],linestyle="-.",c="b",linewidth=2)
SSE = sum([(fun(time[i],v0,a,b,c,d)-voltage[i])**2 for i in range(len(time))])
plt.xlabel("放电时间")
plt.ylabel("电压",rotation=True)
plt.savefig("电池放电拟合图.jpg")
plt.show()
print(SSE)```

SSE变化不大，还行，说得过去。

 a 8.77533 b 0.0366135 c 2600 SSE 0.908134

```import xlrd as xl
import matplotlib.pyplot as plt
import matplotlib
import math
from scipy.optimize import curve_fit
matplotlib.rcParams["font.sans-serif"] = ["SimHei"]
matplotlib.rcParams["axes.unicode_minus"] = False
data = xl.open_workbook("附件2 电池放电测试（30A）采样数据.xls")
work_sheet = data.sheets()[0]
all_rows = work_sheet.nrows
time,voltage = [0 for i in range(all_rows-1)],[0 for i in range(all_rows-1)]
for i in range(1,all_rows):
time[i-1] = work_sheet.cell_value(i,0)
voltage[i-1] = work_sheet.cell_value(i,1)
start = 50
time = time[start:]
voltage = voltage[start:]
plt.scatter(time,voltage,marker="o",c="red",s=0.5)
def fun(t,v0,a,b,c,d):
return a+b*(c-t)**0.5
p_fit,pcov = curve_fit(fun,time,voltage,bounds=[[9,0,0,2600,0],\
[11,10,10,4600,10]])
v0,a,b,c,d = p_fit.tolist()
time0 = [i for i in range(start,int(time[-1]))]
plt.plot(time0,[fun(i,v0,a,b,c,d) for i in time0],linestyle="-.",c="b",linewidth=2)
SSE = sum([(fun(time[i],v0,a,b,c,d)-voltage[i])**2 for i in range(len(time))])
plt.xlabel("放电时间")
plt.ylabel("电压",rotation=True)
plt.savefig("电池放电拟合图.jpg")
plt.show()
print(SSE)```