SciPi Optimize Curve Fit returns error: OptimizeWarning: Covariance of the parameters could not be estimated

251 views Asked by At

I am trying to apply a model based on this formula: Formula used as function and so I defined it as a function

def Amp(angular_freq,N,w0,y,c):
        return ((N)/(((((angular_freq**2)-(w0)**2)**2)+((y*angular_freq)**2))**0.5))+c

This function is applied to a set of data which I have imported as follows:

#reading in csv data
two_ohm_df = pd.read_csv(r'C:/Users/Sidha/Documents/Computing Labs/LCR Experiment/2ohm LCR csv.csv', engine='python') #2ohm
print(two_ohm_df)
two_ohm_angular_freq=2*np.pi*two_ohm_df['Frequency (kHz)']*1000        #x-axis data
print(two_ohm_angular_freq)
two_ohm_peak_to_peak_V=two_ohm_df['Peak-to-Peak Voltage(mV)']/1000     #y-axis data

So first I plot this data, and then I apply the function Amp to the data like this:

#Plotting Angular Frequency against Peak-To-Peak Voltage
plt.plot(two_ohm_angular_freq,two_ohm_peak_to_peak_V,color='orange', marker='*',label='2 Ohm Resistor')
initial_guess=[1050,15915,0.00000322,-0.000000002]
po,po_cov=sp.optimize.curve_fit(Amp,two_ohm_angular_freq,two_ohm_peak_to_peak_V,initial_guess)

plt.plot(two_ohm_angular_freq,Amp(two_ohm_angular_freq,po[0],po[1],po[2],po[3]),color='green',label='Fit results')
plt.xlabel('Angular Frequency (Hz)')
plt.ylabel('Peak-to-Peak Voltage (V)')
plt.legend()
plt.show()

The plots are correct, although I would like to be able to also get the covariance matrix so that I know the uncertainty due to fitting. However, I get this error with regards to the cov matrix.

C:\Program Files\Spyder\pkgs\scipy\optimize\minpack.py:829: OptimizeWarning: Covariance of the parameters could not be estimated
  category=OptimizeWarning)

Everything else is working fine, like I said, my plot looks how I want it to look. Note that in the full code, I reapply this formula to 2 other data sets in exactly the same way (with the only difference being the data used)

Plot of data and plot of the curve-fit

Packages I have imported:

#importing packages
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import pandas as pd
from scipy.optimize import curve_fit

Thank you, Sid

0

There are 0 answers