def Amp(angular_freq,N,w0,y,c):
return ((N)/(((((angular_freq**2)-(w0)**2)**2)+((y*angular_freq)**2))**0.5))+c
此功能适用于我导入的一组数据,如下所示:
#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
所以首先我绘制了这些数据,然后我将函数 Amp 应用于数据,如下所示:
#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()
这些图是正确的,尽管我希望能够获得协方差矩阵,以便我知道由于拟合造成的不确定性。但是,关于 cov 矩阵,我得到了这个错误。
C:\Program Files\Spyder\pkgs\scipy\optimize\minpack.py:829: OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)
其他一切都运行良好,就像我说的,我的情节看起来是我想要的样子。请注意,在完整代码中,我以完全相同的方式将此公式重新应用于其他 2 个数据集(唯一的区别是使用的数据)
我已经导入的包:
#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
谢谢你,席德