从两边都有Y的方程生成数据和曲线拟合

时间:2019-05-24 13:50:07

标签: python scipy curve-fitting

我想拟合由等式(1)生成的曲线:

(1) ID=K*(W/L)*[(VG-VT*(ID*RD/2))**(alpha-1) * (VD-ID*RD)-(1-1/alpha)*(VD-ID*RD)**alpha]

但是变量“ ID”在两侧。我想找到一种方法来从scipy修改本示例中给出的代码:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

#the function for ID, with ID in the arguments. 
def func(K, W, L, VG, VT, ID, RD, alpha, VD ):
    return K*(W/L)*[(VG-VT*(ID*RD/2))**(alpha-1) * (VD-ID*RD)-(1-1/alpha)*(VD-ID*RD)**alpha]

#x_data
VG_data = np.linspace(-6, 16, 300)

#y_data: ID is also an argument of the function here
ID = func(VG=VG_data, K=5.2*1e-7, W=320*1e-6, L=20*1e-6, VT=0.68, VD=5e-3, alpha=2.2, RD=5.79)



popt, pcov = curve_fit(func, VG_data, ID)
popt

plt.plot(VG_data, func(VG_data, *popt), 'r-')


当我运行它时,尝试生成ID时显然会出现错误:

---> 27 ID = func(VG=VG_data, K=5.2*1e-7, W=320*1e-6, L=20*1e-6, VT=0.68, VD=5e-3, alpha=2.2, RD=5.79)
     28 
     29 

TypeError: func() missing 1 required positional argument: 'ID'

当两边都带有'y'变量时,如何生成和拟合数据?

编辑

我正在添加指向示例数据(VG,ID)的链接以及用于绘制示例代码的代码:

将熊猫作为pd导入 导入matplotlib.pyplot作为plt

url='https://raw.githubusercontent.com/leoUninova/Transistor-altair-plots/master/df1.csv'
df=pd.read_csv(url)
#a single transistor data
df=df.loc[df.Name=='300-250-10.0-10.0']
VG=df.VG
ID=df.absID
plt.plot(VG,ID)
plt.show()

enter image description here

0 个答案:

没有答案