简体   繁体   English

拟合实验数据并获得2个参数

[英]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: 出现错误的屏幕截图:

here 这里

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. 因此,解决方案是在脚本中查找所有数学函数,例如sincossqrt ,并用其numpy等效项替换它们。 Luckily, they are usually simply np.sin etc. 幸运的是,它们通常只是np.sin等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自实验数据的正态拟合 - Normal Fit from experimental data 如何使用 python 中的最小化来将数据与方程拟合以获得 model 参数 - how to fit data with equations using minimize in python to obtain model parameters 如何使用 scipy 将 2 个实验数据拟合到导数方程? - How to fit 2 experimental data to derivative equations with scipy? Python Pseudo-Voigt拟合实验数据 - Python Pseudo-Voigt fit experimental data 使用Python拟合模拟和实验数据点 - Fit simulated and experimental data points with Python 如何使用 lmfit Python 绘制多个实验数据的高斯拟合 - How to plot the Gaussian fit for multiple experimental data using lmfit Python 使用带有两个自变量的curve_fit将函数拟合到实验数据 - Fit function to experimental data using curve_fit with two independent variables 如何使用scipy.curve_fit将Python中的实验数据拟合为有限定义区域的反三角函数? - How to fit experimental data in Python to inverse trigonometric function with limited definition area using scipy.curve_fit? 如何通过拟合实验数据来估计一组复微分方程的参数? - How to estimate parameters of set of complex differential equations by fitting experimental data? 如何将曲面拟合到一组数据点并获得曲面的方程 - How to fit a curved surface to a set of data points and obtain the equation for the surface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM