用双自变量曲线拟合函数拟合实验数据

2024-06-01 04:07:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把曲线拟合到我的实验数据集中,但我真的不知道怎么做。我一直在寻找各种可能性,我遇到了curve_fit(还有least_suqares),这似乎是为了完成这项任务,但我仍然非常不熟悉它的工作原理,因为我很难让它进入我的厚脑袋。我首先定义了初始值:

import numpy as np
import math
from scipy.optimize import curve_fit, least_squares

f_exp = np.array([1, 1.6, 2.7, 4.4, 7.3, 12, 20, 32, 56, 88, 144, 250000])
e_exp = np.array([7.15, 7.30, 7.20, 7.25, 7.26, 7.28, 7.32, 7.25, 7.35, 7.34, 7.37, 13.55])

n_e_exp = len(e_exp)

ezero     = 7.15
einf      = 13.55
fc        = np.arange(1,11000,1000)
alpha     = np.arange(0,1.1,0.1)

log_f_mod = np.arange(-3, 6.5, 0.5)
f_mod     = 10 ** log_f_mod

n_f_mod   = len(f_mod)
n_fc      = len(fc)
n_alpha   = len(alpha)  

x         = np.zeros((n_f_mod, n_fc))
for j in range(n_f_mod):
        for k in range(n_fc):
            x[j,k] = np.log(f_mod[j] / fc[k])

注意,xfc的函数。现在,我使用curve_fitleast_squares或其他更合适的函数来定义要运行的函数:

def c_c_eRI(einf, ezero, alpha, x):
    eR    = einf + 1/2 * (ezero - einf) * (1 - np.sinh((1 - alpha) * x) / (np.cosh((1 - alpha) * x) + np.cos(alpha * math.pi / 2))) 
    eI    = np.abs(1/2 * (ezero - einf) * np.cos(alpha * math.pi / 2) / (np.cosh((1 - alpha) * x) + np.sin(alpha * math.pi / 2)))
    eRI   = np.sqrt(eR ** 2 + eI ** 2)
    return eRI

在这一点上,我试图让它工作没有任何运气,通过:

fit = curve_fit(c_c_eRI, f_exp, e_exp)
  • 有没有一种方法可以使用一个函数(例如curve_fitleast_squares或其他一些)来将曲线拟合到实验数据,同时提供独立变量的值,即alphafc(这些x的一个函数,用于实现拟合本身?你知道吗

换言之,目的是找到alphafc(这x是的函数)的值,该值提供了f_expe_exp的最佳拟合,方式与EXCEL解算器通过改变alphafc找到最小平方残差的方式类似。你知道吗

最终目标是绘制f_expe_exp以及使用matplotlib拟合的曲线——我也有点不知道怎么做。你知道吗

我很抱歉没有一个更普遍的例子。你知道吗


Tags: 函数importalphamodlennpmathfit
1条回答
网友
1楼 · 发布于 2024-06-01 04:07:54

如果我正确理解了你的例子,我想你只需要把你的函数定义改为

def c_c_eRI(x, einf, ezero, alpha):
    ...

curve_fit docsThe model function, f(x, …). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.

相关问题 更多 >