Demo entry 6729275

11

   

Submitted by anonymous on Apr 02, 2018 at 11:37
Language: Python 3. Code size: 708 Bytes.

# 导入物性计算模块
from CoolProp.CoolProp import PropSI as psI
# 导入科学计算模块
from scipy import optimize
import numpy as np

# 选取0~120℃的等压比热值进行拟合(10000个数据点)
t1 = np.linspace(0,120, 10000) 

# 计算得到相应饱和温度的液态水等容比热值
p1 = [psI('O', 'T', i+273.15, 'Q', 0, 'water') for i in t1] 

# 拟合函数原型
fitfunc = lambda p, x:   p[0] + p[1]*x + p[2]*x**2+p[3]*x**3\
                                   +p[4]*x**4+p[5]*x**5+p[6]*x**6+p[7]*x**7
# 误差函数
errfunc = lambda p, x, y:np.abs(fitfunc(p, x)  y)

# 系数初始值[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01]
p0 = [0.01]*8
# 使用最小二乘拟合,p即为拟合的系数值
p, suc = optimize.leastsq(errfunc, p0[:], args=(np.array(t1), np.array(p1)))

# 计算误差
errs = errfunc(p,  np.array(t1),  np.array(p1))

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).