[Python] Scipy OptimizeWarning: Covariance of the parameters could not be estimated when...

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to plot some data with a non-linear fit using the function: [​IMG]

    kB and Bv being a constant while J'' is the independent variable and T is a parameter.

    I tried to do this in Python:

    #Constants
    kb = 1.380649e-23
    B = 0.3808

    #Fit function
    def func(J, T):
    return (B*(2*J+1) / kb*T * np.exp(-B*J*(J+1)/kb*T))
    popt, pcov = optimize.curve_fit(func, x, y)
    print(popt)

    plt.plot(x, y, "o")
    plt.plot(x, func(j, popt[0]))


    But this results in the warning "OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn('Covariance of the parameters could not be estimated'" and the parameter turns out to be 1.

    This is the data:

    x = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48]
    y = [0.185,0.375,0.548,0.695,0.849,0.931,0.996,0.992,1.0,0.977,0.927,0.834,0.691,0.68,0.575,0.479,0.421,0.351,0.259,0.208,0.162,0.135,0.093,0.066]


    [​IMG]

    Continue reading...

Compartilhe esta Página