【发布时间】:2016-04-22 22:01:57
【问题描述】:
我上周才开始编程,所以请温柔;)
我尝试使用 curve_fit 进行线性拟合,以确定对斜率的两个贡献。我试过了:
import os
from os import listdir
from os.path import isfile, join
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.axes as ax
from scipy import asarray as ar,exp
freq_lw_tuple = [('10.0', 61.32542701946727), ('10.5', 39.367147015318501), ('11.0', 58.432147581817077), ('11.5', 68.819336676144317), ('12.0', 71.372078906193025), ('12.5', 73.113662907539336), ('13.0', 75.855316075062603), ('13.5', 76.798724771266322), ('14.0', 79.065657225891329), ('14.5', 81.637345805693897), ('15.0', 82.407248034320034)]
def func_lw(x_lw,alpha,g):
return (alpha/g)*x_lw
#define constants
m_e = 9.109383*(10**(-31)) #mass of electron in [kg]
q_e = 1.602176*(10**(-19)) #value of electron charge
pi = 3.14159 #pi
#unzip the list of tuples
unzipped = list(zip(*freq_lw_tuple))
xval_list = []
yval_list = []
for k in range(0,len(unzipped[0])):
x_value = (8/np.sqrt(3))*((2*pi*m_e*float(unzipped[0][k])*10**9)/q_e) #calculate x values
xval_list.append(x_value)
y_value = unzipped[1][k]*10**(-4)*4*pi*10**(-7) #transform unit of y values
yval_list.append(y_value)
start_params2 = [0.01,2]
fitted_params, pcov = curve_fit(func_lw, xval_list, yval_list, start_params2)
它实际上给出了一些结果,但是当我想要时
print(func_lw(xval_list,*fitted_params))
我只是得到一个空列表,可能这就是我不能的原因
plt.plot(xval_list, func_lw(xval_list, *fitted_params))
(这会产生一个错误,例如:x 和 y 必须具有相同的第一维)
[编辑:为 freq_lw_tuple 和导入添加了一些数据]
【问题讨论】:
-
你的“线性”函数有一个斜率(由两个参数组成!)但没有偏移
-
在代码中写入你的导入和一些值,以便其他人可以复制粘贴代码并尝试运行它
-
感谢您的回复!我知道,但它仍然是线性的,对吧?斜率的两个参数是我感兴趣的。如果这就是你的意思,我添加了一些数据。哦,还有进口……对不起
标签: python curve-fitting