-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsq_curve_fit.py
44 lines (35 loc) · 1.16 KB
/
lsq_curve_fit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
"""
this is a script to curve fit pres , vol and strain
v0=5.2232010e+01 unitchange=1/0.13332236842105
shapes as :
norm_pres=a*norm_vol^b
norm_pres=a*strain^b
norm_vol=(v-v0)/v0
norm_pres=unitchange*pres : kpa->mmHg
"""
from scipy.optimize import curve_fit #使用非线性最小二乘法拟合
import numpy as np
#拟合函数
def func(x,a,b):
return a*(x**b)
# fit norm_pres & norm_vol
def pres_vol_fit(pres,vol):
unitchange=1/0.13332236842105
norm_pres=unitchange*pres
norm_vol=(vol-min(vol))/min(vol)
popt, _=curve_fit(func,norm_vol[1:],norm_pres[1:],maxfev=5000,gtol=1e-8)
return popt#popt里面是拟合系数
# norm_pres & strain
def pres_strain_fit(pres,strain):
unitchange=1/0.13332236842105
norm_pres=unitchange*pres
popt, _=curve_fit(func,strain[1:],norm_pres[1:],maxfev=5000,gtol=1e-8)
return popt
# norm_pres-norm_vol/norm_strain
def norm_fit(pres,arg):
unitchange=1/0.13332236842105
norm_pres=unitchange*pres
norm_arg=(arg-min(arg))/(max(arg)-min(arg))
popt, _=curve_fit(func,norm_arg[1:],norm_pres[1:],maxfev=50000)
return popt