Home > Mobile >  Scipy curve fit doesn't perform a fit and raises "Covariance of the parameters could not b
Scipy curve fit doesn't perform a fit and raises "Covariance of the parameters could not b

Time:12-05

I am trying to do a simple linear curve fit with scipy, normally this method works fine for me. This time however for a reason unknown to me it doesn't work.

(I suspect that maybe the numbers are so big that it reaches the limit of what can be stored under a given data type.)

Regardless of the reason, the idea is to make a plot that looks like this: Desired plot with data points and their fits

As you see on the axis here the numbers are of a common order of magnitude. However this time I tried to make a fit to much bigger data points on the order of 1E10, for this I tried to use the following code (here I present only the code for making a scatter plot and then fitting only one data set).

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

ucrt_T = 2/np.sqrt(3)
ucrt_U = 0.1/np.sqrt(3)
T = [314.1, 325.1, 335.1, 345.1, 355.1, 365.1, 374.1, 384.1, 393.1]

T_to_4th = [9733560790.61, 11170378213.80, 12609495509.84, 14183383217.88, 15900203737.92, 17768359469.96, 19586229219.65, 21765930026.49, 23878782252.31]
ucrt_T_lst = [143130823.11, 158701221.00, 173801148.95, 189829733.26, 206814686.75, 224783722.22, 241820148.88, 261735288.93, 280568229.17]

UBlack = [1.9,3.1, 4.4, 5.6, 7.0, 8.7, 10.2, 11.8, 13.4]


def lin_function(x,a,b):
    return a*x   b

def line_fit_2():
    #Dodanie pozostałych punktów na wykresie
    plt.scatter(UBlack, T_to_4th, color='blue')
    plt.errorbar(UBlack, T_to_4th, yerr=ucrt_T, fmt='o')

    #Seria CZARNA
    VltBlack = np.array(UBlack)
    Tt4 = np.array(T_to_4th)
    popt, pcov = curve_fit(lin_function, VltBlack, Tt4, absolute_sigma=False)
    perr = np.sqrt(np.diag(pcov))
    y = lin_function(VltBlack, *popt)
    #Stylistyka i wygląd wykresu
    #plt.plot(Pressure1, y, '--', color = 'g', label="fit with: $a={:.3f}\pm{:.3f}$, $b={:.3f}\pm{:.3f}$" .format(popt[0], perr[0], popt[1], perr[1]))
    plt.plot(VltBlack, y, '--', color='green')


    plt.ylabel(r'$T^4$ w $[K^4]$')
    plt.xlabel(r'Napięcie termometru U w [mV]')
    plt.legend(['Fit', 'Data points'])
    plt.grid()
    plt.show()

line_fit_2()

If you will run it you will find out that the scatter plot is created however the fit isn't executed properly, as only a horizontal line will be added. Additionally an error OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning) is raised.

I would be very happy to know what I am doing wrong or how to resolve this problem. All help is appreciated!

CodePudding user response:

You've pretty much already answered your question, so I'll just confirm your suspicion: the reason the OptimizeWarning is raised is because the underlying optimization algorithm doesn't work properly/diverges due to large parameter numbers.

The solution is very simple, just scale your input parameters before using the fitting tool. Just keep the scaling in mind when you add labels to your x/y axis:

T_to_4th = np.array([9733560790.61, 11170378213.80, 12609495509.84, 14183383217.88, 15900203737.92, 17768359469.96, 19586229219.65, 21765930026.49, 23878782252.31])/10e6
ucrt_T_lst = np.array([143130823.11, 158701221.00, 173801148.95, 189829733.26, 206814686.75, 224783722.22, 241820148.88, 261735288.93, 280568229.17])/10e6

What I did is just divide the lists with big numbers by 10e6. This means that the values are no longer in kPa for example, but in mega kPa (which would be GPa now).

To divide the entire list by the same value, first convert it to a numpy array.

Hope this helps :)

  • Related