[英]Fit experimental data and obtain 2 parameters
I am trying to fit a messy expression to some experimental data. 我正在尝试使一些实验数据变得混乱。 The formula has 2 free parameters (in the code denoted by "a" and "b") and I need to find the values for a and b. 该公式有2个自由参数(在用“ a”和“ b”表示的代码中),我需要找到a和b的值。 I tried using the scipy module but I keep getting an error when compiling the curve_fit procedure. 我尝试使用scipy模块,但是在编译curve_fit过程时始终出现错误。 I tried to find something regarding the error but I couldn't find anything that solved the issue. 我试图找到有关该错误的信息,但找不到解决该问题的信息。
Screenshot with the error: 出现错误的屏幕截图:
Please keep in mind that my experience with python is not that great (basically I just started to learn it for this fitting procedure). 请记住,我在python方面的经验不是很好(基本上我刚刚开始学习此拟合过程)。 Segment of the code is here if it would make it easier: 如果可以简化代码,请参见以下代码段:
def fun1(x,a,b):
result=tsd1(x,17,6.5,a,b)
return result
params, extras = curve_fit(fun1,mydata.spin,mydata.energy)
print(params)
tsd1 is another function which depends on some parameter (the full expression for the tsd1 function can be seen here ) and mydata are 2 arrays with the spin and energy, represented by the first and second column in the data file. tsd1是另一个依赖于某些参数的函数(可以在此处看到tsd1函数的完整表达式),并且mydata是具有自旋和能量的2个数组,由数据文件中的第一列和第二列表示。 Full input data here 完整的输入数据在这里
I would like to know what is wrong with my fitting procedure and how I can solve this issue. 我想知道我的验配程序出了什么问题以及如何解决这个问题。
The problem you experience is that you use the functions of the math
module (they work on scalars) instead of the numpy
functions that are designed to work on arrays. 您遇到的问题是您使用的是math
模块的功能(它们在标量上运行),而不是设计用于数组的numpy
函数。 A simplified version of your problem (I am too lazy to go through all the functions you declared) would be: 您的问题的简化版本(我太懒了,无法遍历您声明的所有函数)将是:
from math import cos
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
import pandas as pd
#works perfectly fine with the numpy arrays from the pandas data frame
def fun1(x, a, b, c, d):
return a * np.cos(b * x / 1000 + c) + d
#this version doesn't work because math works only on scalars
#def fun1(x, a, b, c, d):
# return a * cos(b * x / 1000 + c) + d
#read data from file you provided
mydata = pd.read_csv("test.txt", names = ["spin", "energy"])
#curve fit
params, extras = curve_fit(fun1,mydata.spin,mydata.energy)
print(params)
#generation of the fitting curve
x_fit = np.linspace(np.min(mydata.spin), np.max(mydata.spin), 1000)
y_fit = fun1(x_fit, *params)
#plotting of raw data and fit function
plt.plot(mydata.spin, mydata.energy, "ro", label = "data")
plt.plot(x_fit, y_fit, "b", label = "fit")
plt.legend()
plt.show()
So, the solution is to find all math functions in your script like sin
, cos
, sqrt
and substitute them with their numpy equivalents. 因此,解决方案是在脚本中查找所有数学函数,例如sin
, cos
, sqrt
,并用其numpy等效项替换它们。 Luckily, they are usually simply np.sin
etc. 幸运的是,它们通常只是np.sin
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.