Python 做曲线拟合和求积分的方法
这是一个由加油站油罐传感器测量的油罐高度数据和出油体积,根据体积和高度的倒数,用截面积来描述油罐形状,求出拟合曲线,再用标准数据,求积分来验证拟合曲线效果和误差的一个小项目。主要的就是首先要安装Anaconda python库,然后来运用这些数学工具。
###最小二乘法试验###
importnumpyasnp
importpymysql
fromscipy.optimizeimportleastsq
fromscipyimportintegrate
###绘图,看拟合效果###
importmatplotlib.pyplotasplt
fromsympyimport*
path='E:\PythonProgram\oildata.txt'
lieh0=[]#初始第一列,油管高度
liev1=[]#初始第二列,油枪记录的体积
h_median=[]#高度相邻中位数
h_subtract=[]#相邻高度作差
v_subtract=[]#相邻体积作差
select_h_subtr=[]#筛选的高度作差########
select_v_subtr=[]#筛选的体积作差
VdtH=[]#筛选的V和t的倒数。
defloadData(Spath,lie0,lie1):
withopen(Spath,'r')asf0:
foriinf0:
tmp=i.split()
lie0.append(float(tmp[0]))
lie1.append(float(tmp[2]))
print("sourcelie0",len(lie0))
defconnectMySQL():
db=pymysql.connect(host='10.**.**.**',user='root',passwd='***',db="zabbix",charset="utf8")#校罐
cur=db.cursor()
try:
#查询
cur.execute("SELECT*FROMstation_snapshotlimit10")
forrowincur.fetchall():
#print(row)
id=row[0]
snapshot_id=row[1]
DateTime=row[13]
attr1V=row[5]
attr2H=row[6]
print("id=%d,snapshot_id=%s,DateTime=%s,attr1V=%s,attr2H=%s",
(id,snapshot_id,DateTime,attr1V,attr2H))
except:
print("Error:unabletofecthdataofstation_stock")
try:
cur.execute("SELECT*FROMcan_stocklimit5");
forrowincur.fetchall():
#print(row)
stockid=row[0]
stationid=row[1]
DateTime=row[4]
Volume=row[5]
Height=row[8]
print("stockid=%d,stationid=%s,DateTime=%s,Volume=%f,Height=%f",
(stockid,stationid,DateTime,Volume,Height))
except:
print("Error:unabletofecthdataofcan_snapshot")
cur.close()
db.close()
defformatData(h_med,h_subtr,v_subtr):
lh0=lieh0[:]
dellh0[0]
print("lh0size():",len(lh0))
lh1=lieh0[:]
dellh1[len(lh1)-1]
print("lh1size():",len(lh1))
lv0=liev1[:]
dellv0[0]
#print(liev1)
print("Souceliev1size():",len(liev1))
print("lv1size():",len(lv0))
"""
lv1=liev1[:]
dellv1[len(lv1)-1]
print("lv1size():",len(lv1))
"""
h_med[:]=[(x+y)/2forx,yinzip(lh0,lh1)]###采样点(Xi,Yi)###
print("h_medsize():",len(h_med))
h_subtr[:]=[(y-x)forx,yinzip(lh0,lh1)]
print("h_subtrsize():",len(h_subtr))
#v_subtr[:]=[(y-x)forx,yinzip(lv0,lv1)]
v_subtr[:]=lv0
print("v_subtrsize():",len(v_subtr))
defremoveBadPoint(h_med,h_sub,v_sub):
forvalinh_sub:
position=h_sub.index(val)
if0.01>val>-0.01:
delh_sub[position]
delh_med[position]
delv_sub[position]
v_dt_h_ay=[(y/x)forx,yinzip(h_sub,v_sub)]
returnv_dt_h_ay
defselectRightPoint(h_med,h_subtr,v_dt_h_ay):
forvalinv_dt_h_ay:
pos=v_dt_h_ay.index(val)
ifval>20:
delv_dt_h_ay[pos]
delh_med[pos]
delh_subtr[pos]
forvalinv_dt_h_ay:
ptr=v_dt_h_ay.index(val)
ifval<14:
delv_dt_h_ay[ptr]
delh_med[ptr]
delh_subtr[ptr]
defwriteFile(h_mp,v_dt_h):
s='\n'.join(str(num)[1:-1]fornuminh_mp)
v='\n'.join(str(vdt)[1:-1]forvdtinv_dt_h)
open(r'h_2.txt','w').write(s)
open(r'v_dt_h.txt','w').write(v)
print("writeh_median:",len(h_mp))
#print("V_dtalsois(y-x):",v_dt_h,end="\n")
print("WriteV_dt_h:",len(v_dt_h))
#file=open('data.txt','w')
#file.write(str(h_mp));
#file.close
defintegralCalculate(coeff,xspace):
vCalcute=[]
x=Symbol('x')
a,b,c,d=coeff[0]
y=a*x**3+b*x**2+c*x+d
i=0
while(i
以上这篇Python做曲线拟合和求积分的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
 