如何在 python 曲线拟合阻尼余弦波中有效地使用 "LMFIT"
How to efficiently use "LMFIT" in python curve fitting to a damped cosine wave
我有数据集 (x & y),我想将其拟合为阻尼余弦,例如 (1-A+Acos(Kx))exp(- B*x) 通过使用 LMFIT(“非线性最小二乘法”)作为 link (https://lmfit.github.io/lmfit-py/intro.html)。为此,我尝试使用以下代码,但无法正确安装。我的代码出了什么问题?任何帮助或建议将不胜感激。
x = [0, 1.3, 1.7, 1.72, 1.84, 1.98, 2.02, 2.16, 2.2, 2.2, 2.3, 2.38, 2.5, 2.55, 2.75, 2.8, 2.82, 2.84, 2.9, 2.92, 3.1, 3.13, 3.18, 3.19, 3.22, 3.3, 3.38, 3.44, 3.49, 3.62, 3.64, 3.72, 3.72, 3.75, 3.8, 3.82, 3.86, 3.92, 4.0, 4.07, 4.1, 4.1, 4.13, 4.14, 4.14, 4.17, 4.21, 4.24, 4.24, 4.24, 4.28, 4.3, 4.38, 4.49, 4.62, 4.62, 4.67, 4.72, 4.73, 4.74, 4.76, 4.76, 4.81, 4.81, 4.88, 4.89, 4.9, 4.9, 4.94, 4.96, 5.03, 5.05, 5.06, 5.1, 5.1, 5.15, 5.16, 5.16, 5.19, 5.22, 5.22, 5.3, 5.37, 5.41, 5.46, 5.56, 5.63, 5.65, 5.65, 5.73, 5.76, 5.81, 5.86, 5.91, 5.98, 6.03, 6.05, 6.05, 6.06, 6.11, 6.14, 6.22, 6.25, 6.27, 6.27, 6.3, 6.3, 6.31, 6.36, 6.42, 6.42, 6.47, 6.48, 6.5, 6.51, 6.58, 6.59, 6.62, 6.65, 6.66, 6.67, 6.69, 6.72, 6.77, 6.8, 6.84, 6.87, 6.91, 6.94, 6.94, 6.94, 7.05, 7.14, 7.17, 7.22, 7.23, 7.24, 7.32, 7.32, 7.35, 7.38, 7.4, 7.41, 7.42, 7.44, 7.45, 7.49, 7.5, 7.52, 7.54, 7.6, 7.72, 7.75, 7.81, 7.9, 7.92, 7.95, 7.97, 7.98, 7.99, 8.02, 8.03, 8.03, 8.05, 8.06, 8.07, 8.1, 8.12, 8.14, 8.19, 8.2, 8.21, 8.24, 8.25, 8.28, 8.28, 8.29, 8.32, 8.38, 8.38, 8.43, 8.49, 8.52, 8.54, 8.54, 8.57, 8.7, 8.75, 8.75, 8.78, 8.79, 8.88, 8.88, 8.93, 8.95, 9.0, 9.01, 9.02, 9.03, 9.06, 9.07, 9.11, 9.14, 9.16, 9.17, 9.18, 9.19, 9.2, 9.3, 9.33, 9.44, 9.46, 9.59, 9.62, 9.62, 9.64, 9.66, 9.71, 9.73, 9.73, 9.75, 9.76, 9.76, 9.79, 9.88, 9.9, 9.93, 9.93, 9.95, 9.99, 10.01, 10.03, 10.04, 10.05, 10.07, 10.11, 10.13, 10.18, 10.22, 10.22, 10.31, 10.37, 10.38, 10.41, 10.42, 10.44, 10.5, 10.52, 10.55, 10.56, 10.56, 10.58, 10.6, 10.66, 10.68, 10.68, 10.69, 10.7, 10.73, 10.75, 10.81, 10.93, 10.96, 10.98, 10.98, 11.02, 11.04, 11.1, 11.14, 11.15, 11.15, 11.17, 11.19, 11.21, 11.23, 11.24, 11.28, 11.3, 11.31, 11.32, 11.33, 11.4, 11.42, 11.48, 11.5, 11.51, 11.6, 11.62, 11.62, 11.63, 11.65, 11.72, 11.74, 11.74, 11.94, 11.95, 11.98, 12.02, 12.02, 12.03, 12.04, 12.09, 12.11, 12.17, 12.2, 12.23, 12.26, 12.3, 12.31, 12.33, 12.33, 12.37, 12.38, 12.61, 12.63, 12.69, 12.7, 12.74, 12.79, 12.8, 12.84, 12.87, 12.9, 12.91, 12.92, 12.94, 13.0, 13.19, 13.2, 13.26, 13.29, 13.3, 13.31, 13.31, 13.34, 13.35, 13.36, 13.44, 13.48, 13.52, 13.59, 13.78, 13.83, 13.88, 13.98, 14.02, 14.05, 14.07, 14.1, 14.14, 14.19, 14.25, 14.33, 14.36, 14.38, 14.41, 14.46, 14.47, 14.53, 14.54, 14.57, 14.69, 14.72, 14.77, 14.78, 14.78, 14.8, 14.82, 14.82, 14.91, 14.92, 14.96, 14.96, 15.05, 15.09, 15.17, 15.2, 15.21, 15.25, 15.26, 15.31, 15.32, 15.36, 15.36, 15.4, 15.4, 15.4, 15.41, 15.47, 15.52, 15.6, 15.61, 15.61, 15.63, 15.65, 15.71, 15.77, 15.8, 15.84, 15.86, 15.88, 15.94, 15.94, 15.97, 15.98, 16.02, 16.03, 16.27, 16.43, 16.56, 16.64, 16.64, 16.64, 16.68, 16.88, 16.91, 16.92, 16.93, 16.97, 16.99, 17.0, 17.01, 17.02, 17.05, 17.13, 17.21, 17.32, 17.45, 17.59, 17.79, 17.8, 17.81, 17.87, 17.9, 17.92, 17.93, 17.93, 17.97, 17.98, 18.02, 18.05, 18.08, 18.11, 18.2, 18.24, 18.4, 18.48, 18.5, 18.51, 18.59, 18.65, 18.76, 18.76, 18.86, 18.86, 18.86, 18.87, 18.9, 18.92, 18.93, 19.05, 19.06, 19.17, 19.26, 19.27, 19.41, 19.47, 19.48, 19.54, 19.6, 19.66, 19.67, 19.68, 19.8, 19.9, 20.01, 20.04, 20.1, 20.49, 20.49, 20.5, 20.56, 20.65, 20.65, 20.7, 20.71, 20.78, 20.91, 21.11, 21.19, 21.2, 21.28, 21.29, 21.58, 21.62, 21.7, 21.7, 21.76, 21.76, 21.84, 21.85, 21.87, 21.9, 21.94, 22.0, 22.02, 22.09, 22.16, 22.3, 22.3, 22.41, 22.51, 22.53, 22.71, 22.77, 22.94, 23.17, 23.25, 23.33, 23.72, 23.87, 24.12, 24.14, 24.19, 24.34, 24.4, 24.6, 24.62, 24.62, 24.8, 25.01, 25.13, 25.4, 25.42, 25.81, 25.85, 25.89, 26.03, 26.17, 26.22, 26.41, 26.98, 27.01, 27.02, 27.06, 27.17, 27.49, 27.73, 28.14, 28.23, 28.37, 28.56, 28.83, 28.84, 30.32, 30.57, 31.95, 33.23, 33.46, 33.81, 33.85, 34.44]
y = [1, 0.96826171875, 0.9541015625, 0.99658203125, 0.98828125, 0.98046875, 0.9931640625, 0.99365234375, 0.96435546875, 0.98388671875, 0.984375, 0.98486328125, 0.97412109375, 0.986328125, 0.900390625, 0.662109375, 0.994140625, 0.59716796875, 0.9833984375, 0.97802734375, 0.94873046875, 0.99169921875, 0.9755859375, 0.88330078125, 0.98583984375, 0.96630859375, 0.974609375, 0.9677734375, 0.9345703125, 0.7783203125, 0.95556640625, 0.90576171875, 0.9765625, 0.9794921875, 0.896484375, 0.984375, 0.97314453125, 0.96240234375, 0.96044921875, 0.95361328125, 0.6123046875, 0.919921875, 0.9833984375, 0.90869140625, 0.98095703125, 0.890625, 0.96923828125, 0.425048828125, 0.611328125, 0.96484375, 0.9560546875, 0.464111328125, 0.97705078125, 0.50634765625, 0.841796875, 0.93896484375, 0.96240234375, 0.9501953125, 0.8857421875, 0.806640625, 0.884765625, 0.96533203125, 0.677734375, 0.9169921875, 0.88427734375, 0.90966796875, 0.91259765625, 0.97021484375, 0.9736328125, 0.9619140625, 0.841796875, 0.9736328125, 0.87353515625, 0.79931640625, 0.92626953125, 0.966796875, 0.95263671875, 0.966796875, 0.96240234375, 0.552734375, 0.9443359375, 0.9248046875, 0.91748046875, 0.97021484375, 0.85400390625, 0.70751953125, 0.9658203125, 0.90185546875, 0.916015625, 0.91650390625, 0.95654296875, 0.953125, 0.77294921875, 0.814453125, 0.40673828125, 0.8583984375, 0.8779296875, 0.89892578125, 0.31005859375, 0.495849609375, 0.9189453125, 0.94921875, 0.89990234375, 0.6923828125, 0.7294921875, 0.85302734375, 0.95947265625, 0.890625, 0.9384765625, 0.8779296875, 0.93359375, 0.391357421875, 0.779296875, 0.93505859375, 0.9423828125, 0.93359375, 0.841796875, 0.9345703125, 0.58154296875, 0.92919921875, 0.947265625, 0.94580078125, 0.84375, 0.93359375, 0.921875, 0.9189453125, 0.8955078125, 0.61474609375, 0.8369140625, 0.87060546875, 0.8740234375, 0.356201171875, 0.892578125, 0.73876953125, 0.93310546875, 0.88330078125, 0.826171875, 0.80810546875, 0.984375, 0.919921875, 0.1849365234375, 0.89404296875, 0.83984375, 0.91455078125, 0.2366943359375, 0.73193359375, 0.65380859375, 0.779296875, 0.94140625, 0.8564453125, 0.79150390625, 0.67041015625, 0.266357421875, 0.290771484375, 0.677734375, 0.89501953125, 0.4091796875, 0.87255859375, 0.87255859375, 0.389892578125, 0.90478515625, 0.403076171875, 0.68896484375, 0.83642578125, 0.8486328125, 0.5771484375, 0.8642578125, 0.93359375, 0.7734375, 0.72900390625, 0.853515625, 0.87890625, 0.7587890625, 0.2391357421875, 0.8671875, 0.9501953125, 0.92041015625, 0.869140625, 0.90576171875, 0.95947265625, 0.77734375, 0.2017822265625, 0.82568359375, 0.6015625, 0.8935546875, 0.12481689453125, 0.89697265625, 0.6240234375, 0.86865234375, 0.9130859375, 0.470947265625, 0.75439453125, 0.8486328125, 0.67529296875, 0.87451171875, 0.9755859375, 0.71826171875, 0.91796875, 0.70751953125, 0.80908203125, 0.89208984375, 0.79541015625, 0.7236328125, 0.81103515625, 0.84814453125, 0.59765625, 0.7529296875, 0.234375, 0.68896484375, 0.849609375, 0.69970703125, 0.82861328125, 0.87939453125, 0.322509765625, 0.84326171875, 0.4404296875, 0.78369140625, 0.58154296875, 0.796875, 0.84814453125, 0.28759765625, 0.5263671875, 0.71044921875, 0.8388671875, 0.94580078125, 0.3125, 0.1378173828125, 0.81591796875, 0.85009765625, 0.84521484375, 0.779296875, 0.45751953125, 0.84033203125, 0.58447265625, 0.921875, 0.51123046875, 0.82421875, 0.80224609375, 0.2000732421875, 0.90771484375, 0.4267578125, 0.58154296875, 0.78173828125, 0.736328125, 0.8173828125, 0.65087890625, 0.66357421875, 0.900390625, 0.923828125, 0.66162109375, 0.76513671875, 0.73974609375, 0.6943359375, 0.96044921875, 0.4091796875, 0.529296875, 0.82861328125, 0.7333984375, 0.81689453125, 0.73583984375, 0.66259765625, 0.72607421875, 0.8125, 0.47802734375, 0.646484375, 0.1658935546875, 0.80615234375, 0.59716796875, 0.724609375, 0.2113037109375, 0.301025390625, 0.45947265625, 0.22216796875, 0.62744140625, 0.61865234375, 0.8662109375, 0.7998046875, 0.7333984375, 0.771484375, 0.57373046875, -0.135009765625, 0.6455078125, 0.39404296875, 0.76513671875, 0.603515625, 0.76025390625, 0.630859375, 0.114013671875, 0.63671875, 0.75537109375, 0.1390380859375, 0.27001953125, 0.73681640625, 0.78271484375, 0.7646484375, -0.005977630615234375, 0.402099609375, 0.7119140625, 0.75244140625, 0.71484375, 0.64990234375, 0.3974609375, 0.333251953125, -0.004974365234375, 0.94873046875, 0.348876953125, 0.27880859375, 0.77783203125, 0.67529296875, 0.421875, 0.66357421875, 0.73388671875, 0.6640625, 0.70263671875, 0.48486328125, 0.25537109375, 0.6337890625, 0.4765625, 0.560546875, 0.75439453125, 0.1290283203125, 0.75390625, 0.67138671875, -0.06591796875, 0.7421875, 0.671875, 0.62109375, 0.487548828125, 0.61181640625, 0.56787109375, -0.158447265625, 0.767578125, 0.2235107421875, 0.701171875, 0.05010986328125, 0.60986328125, 0.7255859375, 0.646484375, 0.368896484375, 0.623046875, 0.7177734375, -0.1337890625, 0.69677734375, 0.68408203125, 0.64794921875, 0.433349609375, 0.5302734375, 0.8076171875, 0.39306640625, 0.70361328125, 0.55810546875, 0.7392578125, 0.59765625, 0.91748046875, 0.2086181640625, -0.00395965576171875, 0.11541748046875, 0.4501953125, 0.9111328125, 0.5673828125, 0.278076171875, 0.55029296875, 0.53271484375, 0.6103515625, 0.2529296875, 0.374755859375, 0.6416015625, -0.0623779296875, 0.6845703125, 0.75537109375, 0.853515625, -0.1517333984375, 0.77294921875, 0.55517578125, 0.51318359375, 0.62353515625, 0.5771484375, 0.53515625, 0.324951171875, 0.1741943359375, 0.619140625, -0.174560546875, 0.1729736328125, 0.33642578125, 0.58544921875, 0.59033203125, 0.66845703125, 0.50244140625, 0.01486968994140625, 0.3740234375, 0.442138671875, 0.249755859375, 0.52685546875, 0.17822265625, 0.036163330078125, 0.0174560546875, -0.272705078125, 0.072998046875, 0.14208984375, 0.81298828125, 0.28955078125, 0.371337890625, 0.499755859375, 0.311767578125, 0.63330078125, 0.7138671875, 0.358154296875, -0.04681396484375, -0.1251220703125, 0.32763671875, 0.049652099609375, 0.449951171875, 0.469970703125, 0.1090087890625, 0.34326171875, 0.040740966796875, 0.43017578125, 0.311767578125, 0.043548583984375, -0.2294921875, 0.2281494140625, -0.26611328125, 0.5087890625, 0.76904296875, 0.058685302734375, 0.6572265625, 0.51318359375, 0.7578125, -0.036712646484375, 0.311767578125, 0.025726318359375, 0.041351318359375, 0.0687255859375, 0.342529296875, 0.375732421875, 0.316162109375, 0.466796875, 0.371337890625, 0.07318115234375, 0.6201171875, 0.63623046875, 0.425048828125, 0.38134765625, -0.263916015625, -0.26513671875, 0.364990234375, 0.454833984375, -0.27001953125, 0.43017578125, 0.2164306640625, 0.335693359375, 0.310302734375, 0.447998046875, -0.1202392578125, 0.33740234375, 0.383544921875, -0.447509765625, -0.076904296875, 0.0176544189453125, 0.52587890625, 0.6865234375, 0.59375, -0.2413330078125, 0.38671875, 0.355224609375, 0.403564453125, 0.58154296875, 0.426025390625, 0.0280609130859375, -0.342041015625, -0.06524658203125, 0.2734375, 0.01151275634765625, -0.00833892822265625, 0.2269287109375, -0.1380615234375, 0.55712890625, -0.01837158203125, 0.36474609375, 0.52099609375, -0.353759765625, 0.356689453125, -0.11865234375, 0.2734375, 0.07598876953125, -0.207763671875, 0.1864013671875, 0.11090087890625, 0.08856201171875, -0.441162109375, 0.197998046875, -0.11285400390625, 0.1878662109375, 0.50537109375, -0.40576171875, -0.2386474609375, 0.0860595703125, -0.38720703125, -0.192626953125, -0.313232421875, 0.478759765625, -0.4306640625, -0.0682373046875, 0.01105499267578125, -0.14013671875, 0.23583984375, 0.26025390625, -0.255859375, -0.37548828125, 0.0772705078125, -0.21728515625, 0.279296875, 0.153564453125, -0.389892578125, 0.45361328125, -0.11083984375, 0.466064453125, 0.44287109375, -0.3984375, -0.16552734375, 0.3984375, -0.1669921875, -0.11944580078125, -0.1561279296875, 0.441650390625, -0.194091796875, -0.429443359375, 0.00981903076171875, 0.2122802734375, -0.151123046875, 0.019500732421875, -0.192138671875, -0.1365966796875, 0.1016845703125, -0.10931396484375, 0.139404296875, -0.454833984375, -0.353515625, 0.26953125, -0.41650390625, -0.41162109375, 0.303955078125, 0.0335693359375, -0.1527099609375, -0.396728515625, 0.1541748046875, 0.302001953125, 0.461181640625, -0.26953125, -0.244873046875, -0.128173828125, -0.1346435546875, -0.2939453125, 0.1932373046875, 0.0312042236328125, 0.04608154296875, -0.351806640625, -0.11907958984375, -0.005191802978515625, -0.29296875, -0.1134033203125, -0.09002685546875]
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import least_squares
from lmfit import minimize, Parameters, Parameter, report_fit
x = np.asarray(x); y = np.asarray(y)
def fit_fc(params, t, data):
A = params['A'].value
K = params['K'].value
B = params['B'].value
model = (1-A+A*np.cos(K * t))*np.exp(-1 * B * t)
return model - data
params = Parameters()
params.add('A', value=.9, min =0, max =1)
params.add('K', value=0.42, min=-0.2, max=0.8)
params.add('B', value=0.1, min=.01, max=.1)
result = minimize(fit_fc, params, args=(x, y), method='leastsq')
report_fit(params) # write error report
y_lsq = (1-result.params['A'] + (result.params['A'] *np.cos(result.params['K']*x))*np.exp(result.params['B'])*x*(-1))
#plot results
plt.plot(x, y, 'o', label='Data')
plt.plot(x, y_lsq, label='Least_Square_Method')
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.legend()
你试过画图吗?与您尝试的拟合相比,您的数据确实“嘈杂”。
我将您的脚本更改为使用 lmfit.Model
,这样更容易进行曲线拟合。看起来像(为简单起见删除数据):
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model, Parameters
x = np.asarray(x)
y = np.asarray(y)
def damped_cosine(t, a, k, b):
return (1-a+a*np.cos(k*t))*np.exp(-b*t)
params = Parameters()
params.add('a', value=0.9, min=0)
params.add('k', value=0.42)
params.add('b', value=0.1)
dmodel = Model(damped_cosine)
result = dmodel.fit(y, params, t=x)
print(result.fit_report())
result.plot_fit(show_init=True)
plt.show()
我已经删除了大部分参数边界——不要使用任意和严格的边界,除非你知道你需要这样做,如果你甚至没有绘制你的数据,那么你肯定不知道.
为此配合打印的报告是
[[Model]]
Model(damped_cosine)
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 42
# data points = 562
# variables = 3
chi-square = 42.2473566
reduced chi-square = 0.07557667
Akaike info crit = -1448.43356
Bayesian info crit = -1435.43905
[[Variables]]
a: 1.1104e-10 +/- 0.00205742 (1852835584.87%) (init = 0.9)
k: 0.45910709 +/- 170843.547 (37212134.66%) (init = 0.42)
b: 0.05936966 +/- 0.00309385 (5.21%) (init = 0.1)
[[Correlations]] (unreported correlations are < 0.100)
C(a, b) = -0.707
数据图和拟合(以及初始猜测)是
我认为最明显的结论是阻尼振荡函数不能非常令人信服地表示您的数据。没有任何明显的完整振荡。
我有数据集 (x & y),我想将其拟合为阻尼余弦,例如 (1-A+Acos(Kx))exp(- B*x) 通过使用 LMFIT(“非线性最小二乘法”)作为 link (https://lmfit.github.io/lmfit-py/intro.html)。为此,我尝试使用以下代码,但无法正确安装。我的代码出了什么问题?任何帮助或建议将不胜感激。
x = [0, 1.3, 1.7, 1.72, 1.84, 1.98, 2.02, 2.16, 2.2, 2.2, 2.3, 2.38, 2.5, 2.55, 2.75, 2.8, 2.82, 2.84, 2.9, 2.92, 3.1, 3.13, 3.18, 3.19, 3.22, 3.3, 3.38, 3.44, 3.49, 3.62, 3.64, 3.72, 3.72, 3.75, 3.8, 3.82, 3.86, 3.92, 4.0, 4.07, 4.1, 4.1, 4.13, 4.14, 4.14, 4.17, 4.21, 4.24, 4.24, 4.24, 4.28, 4.3, 4.38, 4.49, 4.62, 4.62, 4.67, 4.72, 4.73, 4.74, 4.76, 4.76, 4.81, 4.81, 4.88, 4.89, 4.9, 4.9, 4.94, 4.96, 5.03, 5.05, 5.06, 5.1, 5.1, 5.15, 5.16, 5.16, 5.19, 5.22, 5.22, 5.3, 5.37, 5.41, 5.46, 5.56, 5.63, 5.65, 5.65, 5.73, 5.76, 5.81, 5.86, 5.91, 5.98, 6.03, 6.05, 6.05, 6.06, 6.11, 6.14, 6.22, 6.25, 6.27, 6.27, 6.3, 6.3, 6.31, 6.36, 6.42, 6.42, 6.47, 6.48, 6.5, 6.51, 6.58, 6.59, 6.62, 6.65, 6.66, 6.67, 6.69, 6.72, 6.77, 6.8, 6.84, 6.87, 6.91, 6.94, 6.94, 6.94, 7.05, 7.14, 7.17, 7.22, 7.23, 7.24, 7.32, 7.32, 7.35, 7.38, 7.4, 7.41, 7.42, 7.44, 7.45, 7.49, 7.5, 7.52, 7.54, 7.6, 7.72, 7.75, 7.81, 7.9, 7.92, 7.95, 7.97, 7.98, 7.99, 8.02, 8.03, 8.03, 8.05, 8.06, 8.07, 8.1, 8.12, 8.14, 8.19, 8.2, 8.21, 8.24, 8.25, 8.28, 8.28, 8.29, 8.32, 8.38, 8.38, 8.43, 8.49, 8.52, 8.54, 8.54, 8.57, 8.7, 8.75, 8.75, 8.78, 8.79, 8.88, 8.88, 8.93, 8.95, 9.0, 9.01, 9.02, 9.03, 9.06, 9.07, 9.11, 9.14, 9.16, 9.17, 9.18, 9.19, 9.2, 9.3, 9.33, 9.44, 9.46, 9.59, 9.62, 9.62, 9.64, 9.66, 9.71, 9.73, 9.73, 9.75, 9.76, 9.76, 9.79, 9.88, 9.9, 9.93, 9.93, 9.95, 9.99, 10.01, 10.03, 10.04, 10.05, 10.07, 10.11, 10.13, 10.18, 10.22, 10.22, 10.31, 10.37, 10.38, 10.41, 10.42, 10.44, 10.5, 10.52, 10.55, 10.56, 10.56, 10.58, 10.6, 10.66, 10.68, 10.68, 10.69, 10.7, 10.73, 10.75, 10.81, 10.93, 10.96, 10.98, 10.98, 11.02, 11.04, 11.1, 11.14, 11.15, 11.15, 11.17, 11.19, 11.21, 11.23, 11.24, 11.28, 11.3, 11.31, 11.32, 11.33, 11.4, 11.42, 11.48, 11.5, 11.51, 11.6, 11.62, 11.62, 11.63, 11.65, 11.72, 11.74, 11.74, 11.94, 11.95, 11.98, 12.02, 12.02, 12.03, 12.04, 12.09, 12.11, 12.17, 12.2, 12.23, 12.26, 12.3, 12.31, 12.33, 12.33, 12.37, 12.38, 12.61, 12.63, 12.69, 12.7, 12.74, 12.79, 12.8, 12.84, 12.87, 12.9, 12.91, 12.92, 12.94, 13.0, 13.19, 13.2, 13.26, 13.29, 13.3, 13.31, 13.31, 13.34, 13.35, 13.36, 13.44, 13.48, 13.52, 13.59, 13.78, 13.83, 13.88, 13.98, 14.02, 14.05, 14.07, 14.1, 14.14, 14.19, 14.25, 14.33, 14.36, 14.38, 14.41, 14.46, 14.47, 14.53, 14.54, 14.57, 14.69, 14.72, 14.77, 14.78, 14.78, 14.8, 14.82, 14.82, 14.91, 14.92, 14.96, 14.96, 15.05, 15.09, 15.17, 15.2, 15.21, 15.25, 15.26, 15.31, 15.32, 15.36, 15.36, 15.4, 15.4, 15.4, 15.41, 15.47, 15.52, 15.6, 15.61, 15.61, 15.63, 15.65, 15.71, 15.77, 15.8, 15.84, 15.86, 15.88, 15.94, 15.94, 15.97, 15.98, 16.02, 16.03, 16.27, 16.43, 16.56, 16.64, 16.64, 16.64, 16.68, 16.88, 16.91, 16.92, 16.93, 16.97, 16.99, 17.0, 17.01, 17.02, 17.05, 17.13, 17.21, 17.32, 17.45, 17.59, 17.79, 17.8, 17.81, 17.87, 17.9, 17.92, 17.93, 17.93, 17.97, 17.98, 18.02, 18.05, 18.08, 18.11, 18.2, 18.24, 18.4, 18.48, 18.5, 18.51, 18.59, 18.65, 18.76, 18.76, 18.86, 18.86, 18.86, 18.87, 18.9, 18.92, 18.93, 19.05, 19.06, 19.17, 19.26, 19.27, 19.41, 19.47, 19.48, 19.54, 19.6, 19.66, 19.67, 19.68, 19.8, 19.9, 20.01, 20.04, 20.1, 20.49, 20.49, 20.5, 20.56, 20.65, 20.65, 20.7, 20.71, 20.78, 20.91, 21.11, 21.19, 21.2, 21.28, 21.29, 21.58, 21.62, 21.7, 21.7, 21.76, 21.76, 21.84, 21.85, 21.87, 21.9, 21.94, 22.0, 22.02, 22.09, 22.16, 22.3, 22.3, 22.41, 22.51, 22.53, 22.71, 22.77, 22.94, 23.17, 23.25, 23.33, 23.72, 23.87, 24.12, 24.14, 24.19, 24.34, 24.4, 24.6, 24.62, 24.62, 24.8, 25.01, 25.13, 25.4, 25.42, 25.81, 25.85, 25.89, 26.03, 26.17, 26.22, 26.41, 26.98, 27.01, 27.02, 27.06, 27.17, 27.49, 27.73, 28.14, 28.23, 28.37, 28.56, 28.83, 28.84, 30.32, 30.57, 31.95, 33.23, 33.46, 33.81, 33.85, 34.44]
y = [1, 0.96826171875, 0.9541015625, 0.99658203125, 0.98828125, 0.98046875, 0.9931640625, 0.99365234375, 0.96435546875, 0.98388671875, 0.984375, 0.98486328125, 0.97412109375, 0.986328125, 0.900390625, 0.662109375, 0.994140625, 0.59716796875, 0.9833984375, 0.97802734375, 0.94873046875, 0.99169921875, 0.9755859375, 0.88330078125, 0.98583984375, 0.96630859375, 0.974609375, 0.9677734375, 0.9345703125, 0.7783203125, 0.95556640625, 0.90576171875, 0.9765625, 0.9794921875, 0.896484375, 0.984375, 0.97314453125, 0.96240234375, 0.96044921875, 0.95361328125, 0.6123046875, 0.919921875, 0.9833984375, 0.90869140625, 0.98095703125, 0.890625, 0.96923828125, 0.425048828125, 0.611328125, 0.96484375, 0.9560546875, 0.464111328125, 0.97705078125, 0.50634765625, 0.841796875, 0.93896484375, 0.96240234375, 0.9501953125, 0.8857421875, 0.806640625, 0.884765625, 0.96533203125, 0.677734375, 0.9169921875, 0.88427734375, 0.90966796875, 0.91259765625, 0.97021484375, 0.9736328125, 0.9619140625, 0.841796875, 0.9736328125, 0.87353515625, 0.79931640625, 0.92626953125, 0.966796875, 0.95263671875, 0.966796875, 0.96240234375, 0.552734375, 0.9443359375, 0.9248046875, 0.91748046875, 0.97021484375, 0.85400390625, 0.70751953125, 0.9658203125, 0.90185546875, 0.916015625, 0.91650390625, 0.95654296875, 0.953125, 0.77294921875, 0.814453125, 0.40673828125, 0.8583984375, 0.8779296875, 0.89892578125, 0.31005859375, 0.495849609375, 0.9189453125, 0.94921875, 0.89990234375, 0.6923828125, 0.7294921875, 0.85302734375, 0.95947265625, 0.890625, 0.9384765625, 0.8779296875, 0.93359375, 0.391357421875, 0.779296875, 0.93505859375, 0.9423828125, 0.93359375, 0.841796875, 0.9345703125, 0.58154296875, 0.92919921875, 0.947265625, 0.94580078125, 0.84375, 0.93359375, 0.921875, 0.9189453125, 0.8955078125, 0.61474609375, 0.8369140625, 0.87060546875, 0.8740234375, 0.356201171875, 0.892578125, 0.73876953125, 0.93310546875, 0.88330078125, 0.826171875, 0.80810546875, 0.984375, 0.919921875, 0.1849365234375, 0.89404296875, 0.83984375, 0.91455078125, 0.2366943359375, 0.73193359375, 0.65380859375, 0.779296875, 0.94140625, 0.8564453125, 0.79150390625, 0.67041015625, 0.266357421875, 0.290771484375, 0.677734375, 0.89501953125, 0.4091796875, 0.87255859375, 0.87255859375, 0.389892578125, 0.90478515625, 0.403076171875, 0.68896484375, 0.83642578125, 0.8486328125, 0.5771484375, 0.8642578125, 0.93359375, 0.7734375, 0.72900390625, 0.853515625, 0.87890625, 0.7587890625, 0.2391357421875, 0.8671875, 0.9501953125, 0.92041015625, 0.869140625, 0.90576171875, 0.95947265625, 0.77734375, 0.2017822265625, 0.82568359375, 0.6015625, 0.8935546875, 0.12481689453125, 0.89697265625, 0.6240234375, 0.86865234375, 0.9130859375, 0.470947265625, 0.75439453125, 0.8486328125, 0.67529296875, 0.87451171875, 0.9755859375, 0.71826171875, 0.91796875, 0.70751953125, 0.80908203125, 0.89208984375, 0.79541015625, 0.7236328125, 0.81103515625, 0.84814453125, 0.59765625, 0.7529296875, 0.234375, 0.68896484375, 0.849609375, 0.69970703125, 0.82861328125, 0.87939453125, 0.322509765625, 0.84326171875, 0.4404296875, 0.78369140625, 0.58154296875, 0.796875, 0.84814453125, 0.28759765625, 0.5263671875, 0.71044921875, 0.8388671875, 0.94580078125, 0.3125, 0.1378173828125, 0.81591796875, 0.85009765625, 0.84521484375, 0.779296875, 0.45751953125, 0.84033203125, 0.58447265625, 0.921875, 0.51123046875, 0.82421875, 0.80224609375, 0.2000732421875, 0.90771484375, 0.4267578125, 0.58154296875, 0.78173828125, 0.736328125, 0.8173828125, 0.65087890625, 0.66357421875, 0.900390625, 0.923828125, 0.66162109375, 0.76513671875, 0.73974609375, 0.6943359375, 0.96044921875, 0.4091796875, 0.529296875, 0.82861328125, 0.7333984375, 0.81689453125, 0.73583984375, 0.66259765625, 0.72607421875, 0.8125, 0.47802734375, 0.646484375, 0.1658935546875, 0.80615234375, 0.59716796875, 0.724609375, 0.2113037109375, 0.301025390625, 0.45947265625, 0.22216796875, 0.62744140625, 0.61865234375, 0.8662109375, 0.7998046875, 0.7333984375, 0.771484375, 0.57373046875, -0.135009765625, 0.6455078125, 0.39404296875, 0.76513671875, 0.603515625, 0.76025390625, 0.630859375, 0.114013671875, 0.63671875, 0.75537109375, 0.1390380859375, 0.27001953125, 0.73681640625, 0.78271484375, 0.7646484375, -0.005977630615234375, 0.402099609375, 0.7119140625, 0.75244140625, 0.71484375, 0.64990234375, 0.3974609375, 0.333251953125, -0.004974365234375, 0.94873046875, 0.348876953125, 0.27880859375, 0.77783203125, 0.67529296875, 0.421875, 0.66357421875, 0.73388671875, 0.6640625, 0.70263671875, 0.48486328125, 0.25537109375, 0.6337890625, 0.4765625, 0.560546875, 0.75439453125, 0.1290283203125, 0.75390625, 0.67138671875, -0.06591796875, 0.7421875, 0.671875, 0.62109375, 0.487548828125, 0.61181640625, 0.56787109375, -0.158447265625, 0.767578125, 0.2235107421875, 0.701171875, 0.05010986328125, 0.60986328125, 0.7255859375, 0.646484375, 0.368896484375, 0.623046875, 0.7177734375, -0.1337890625, 0.69677734375, 0.68408203125, 0.64794921875, 0.433349609375, 0.5302734375, 0.8076171875, 0.39306640625, 0.70361328125, 0.55810546875, 0.7392578125, 0.59765625, 0.91748046875, 0.2086181640625, -0.00395965576171875, 0.11541748046875, 0.4501953125, 0.9111328125, 0.5673828125, 0.278076171875, 0.55029296875, 0.53271484375, 0.6103515625, 0.2529296875, 0.374755859375, 0.6416015625, -0.0623779296875, 0.6845703125, 0.75537109375, 0.853515625, -0.1517333984375, 0.77294921875, 0.55517578125, 0.51318359375, 0.62353515625, 0.5771484375, 0.53515625, 0.324951171875, 0.1741943359375, 0.619140625, -0.174560546875, 0.1729736328125, 0.33642578125, 0.58544921875, 0.59033203125, 0.66845703125, 0.50244140625, 0.01486968994140625, 0.3740234375, 0.442138671875, 0.249755859375, 0.52685546875, 0.17822265625, 0.036163330078125, 0.0174560546875, -0.272705078125, 0.072998046875, 0.14208984375, 0.81298828125, 0.28955078125, 0.371337890625, 0.499755859375, 0.311767578125, 0.63330078125, 0.7138671875, 0.358154296875, -0.04681396484375, -0.1251220703125, 0.32763671875, 0.049652099609375, 0.449951171875, 0.469970703125, 0.1090087890625, 0.34326171875, 0.040740966796875, 0.43017578125, 0.311767578125, 0.043548583984375, -0.2294921875, 0.2281494140625, -0.26611328125, 0.5087890625, 0.76904296875, 0.058685302734375, 0.6572265625, 0.51318359375, 0.7578125, -0.036712646484375, 0.311767578125, 0.025726318359375, 0.041351318359375, 0.0687255859375, 0.342529296875, 0.375732421875, 0.316162109375, 0.466796875, 0.371337890625, 0.07318115234375, 0.6201171875, 0.63623046875, 0.425048828125, 0.38134765625, -0.263916015625, -0.26513671875, 0.364990234375, 0.454833984375, -0.27001953125, 0.43017578125, 0.2164306640625, 0.335693359375, 0.310302734375, 0.447998046875, -0.1202392578125, 0.33740234375, 0.383544921875, -0.447509765625, -0.076904296875, 0.0176544189453125, 0.52587890625, 0.6865234375, 0.59375, -0.2413330078125, 0.38671875, 0.355224609375, 0.403564453125, 0.58154296875, 0.426025390625, 0.0280609130859375, -0.342041015625, -0.06524658203125, 0.2734375, 0.01151275634765625, -0.00833892822265625, 0.2269287109375, -0.1380615234375, 0.55712890625, -0.01837158203125, 0.36474609375, 0.52099609375, -0.353759765625, 0.356689453125, -0.11865234375, 0.2734375, 0.07598876953125, -0.207763671875, 0.1864013671875, 0.11090087890625, 0.08856201171875, -0.441162109375, 0.197998046875, -0.11285400390625, 0.1878662109375, 0.50537109375, -0.40576171875, -0.2386474609375, 0.0860595703125, -0.38720703125, -0.192626953125, -0.313232421875, 0.478759765625, -0.4306640625, -0.0682373046875, 0.01105499267578125, -0.14013671875, 0.23583984375, 0.26025390625, -0.255859375, -0.37548828125, 0.0772705078125, -0.21728515625, 0.279296875, 0.153564453125, -0.389892578125, 0.45361328125, -0.11083984375, 0.466064453125, 0.44287109375, -0.3984375, -0.16552734375, 0.3984375, -0.1669921875, -0.11944580078125, -0.1561279296875, 0.441650390625, -0.194091796875, -0.429443359375, 0.00981903076171875, 0.2122802734375, -0.151123046875, 0.019500732421875, -0.192138671875, -0.1365966796875, 0.1016845703125, -0.10931396484375, 0.139404296875, -0.454833984375, -0.353515625, 0.26953125, -0.41650390625, -0.41162109375, 0.303955078125, 0.0335693359375, -0.1527099609375, -0.396728515625, 0.1541748046875, 0.302001953125, 0.461181640625, -0.26953125, -0.244873046875, -0.128173828125, -0.1346435546875, -0.2939453125, 0.1932373046875, 0.0312042236328125, 0.04608154296875, -0.351806640625, -0.11907958984375, -0.005191802978515625, -0.29296875, -0.1134033203125, -0.09002685546875]
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import least_squares
from lmfit import minimize, Parameters, Parameter, report_fit
x = np.asarray(x); y = np.asarray(y)
def fit_fc(params, t, data):
A = params['A'].value
K = params['K'].value
B = params['B'].value
model = (1-A+A*np.cos(K * t))*np.exp(-1 * B * t)
return model - data
params = Parameters()
params.add('A', value=.9, min =0, max =1)
params.add('K', value=0.42, min=-0.2, max=0.8)
params.add('B', value=0.1, min=.01, max=.1)
result = minimize(fit_fc, params, args=(x, y), method='leastsq')
report_fit(params) # write error report
y_lsq = (1-result.params['A'] + (result.params['A'] *np.cos(result.params['K']*x))*np.exp(result.params['B'])*x*(-1))
#plot results
plt.plot(x, y, 'o', label='Data')
plt.plot(x, y_lsq, label='Least_Square_Method')
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.legend()
你试过画图吗?与您尝试的拟合相比,您的数据确实“嘈杂”。
我将您的脚本更改为使用 lmfit.Model
,这样更容易进行曲线拟合。看起来像(为简单起见删除数据):
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model, Parameters
x = np.asarray(x)
y = np.asarray(y)
def damped_cosine(t, a, k, b):
return (1-a+a*np.cos(k*t))*np.exp(-b*t)
params = Parameters()
params.add('a', value=0.9, min=0)
params.add('k', value=0.42)
params.add('b', value=0.1)
dmodel = Model(damped_cosine)
result = dmodel.fit(y, params, t=x)
print(result.fit_report())
result.plot_fit(show_init=True)
plt.show()
我已经删除了大部分参数边界——不要使用任意和严格的边界,除非你知道你需要这样做,如果你甚至没有绘制你的数据,那么你肯定不知道.
为此配合打印的报告是
[[Model]]
Model(damped_cosine)
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 42
# data points = 562
# variables = 3
chi-square = 42.2473566
reduced chi-square = 0.07557667
Akaike info crit = -1448.43356
Bayesian info crit = -1435.43905
[[Variables]]
a: 1.1104e-10 +/- 0.00205742 (1852835584.87%) (init = 0.9)
k: 0.45910709 +/- 170843.547 (37212134.66%) (init = 0.42)
b: 0.05936966 +/- 0.00309385 (5.21%) (init = 0.1)
[[Correlations]] (unreported correlations are < 0.100)
C(a, b) = -0.707
数据图和拟合(以及初始猜测)是
我认为最明显的结论是阻尼振荡函数不能非常令人信服地表示您的数据。没有任何明显的完整振荡。