首页 > 解决方案 > 无法拟合超级简单数据。为什么?

问题描述

所以我有一个实验的数据。我想要的只是拟合一个对数函数,如果可能的话,将基数保持为可变参数以获得最佳拟合。我不认为这是太多的要求。

所以这是我拥有的两个 x 和 y 数据列表(它们是 numpy 数组):

optxlst = np.arange( 1, 92 )

optylst = [ 
  1.96892911,  3.2887339,   4.39055229,  5.27383471,  6.06991106,  6.87452902,
  7.50480744,  7.9697223,   8.46885029,  8.94166484,  9.40262963,  9.78905132,
 10.13815734, 10.45350799, 10.72270686, 10.9475476,  11.19546712, 11.40757756,
 11.60946643, 11.84196217, 11.99959217, 12.15661292, 12.30413587, 12.38961966,
 12.5699555,  12.76926629, 12.90418109, 13.03114574, 13.13427797, 13.22579756,
 13.33522992, 13.42566857, 13.53639778, 13.59552165, 13.67813624, 13.76984529,
 13.8265399,  13.89902442, 13.96015279, 14.01978188, 14.06700569, 14.11720781,
 14.07497814, 14.17468682, 14.21045489, 14.2578857,  14.30824306, 14.34443375,
 14.37487868, 14.40496269, 14.42757652, 14.47123075, 14.49446011, 14.52862657,
 14.55842334, 14.59823539, 14.61842774, 14.66413474, 14.74780246, 14.75705207,
 14.80998592, 14.87927607, 14.90345136, 14.93509437, 14.9453211,  14.96428837,
 14.96461419, 15.01489101, 15.06245498, 15.0761481,  15.11197484 ,15.16265455,
 15.21534841, 15.25283963, 15.24408824, 15.27259487, 15.29698918, 15.3260376,
 15.33598051, 15.377234,   15.3913971,  15.41035688, 15.41644309, 15.43849198,
 15.46378739, 15.48653162, 15.49380553, 15.49286959, 15.49551899, 15.5068906,
 15.50743637]

如果绘制它们会产生以下图:

在此处输入图像描述

现在我想要的只是创建一个很好的对数函数拟合。但我收到错误消息:

>> RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 600.

在这一点上,我没有想法。这是代码的相关部分:

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


optxlst = [...] 
optylst = [...]



def logf( x, b, d ):  #b is supposed to be the base of the log. Using a math formula here.
    return np.log( x ) / np.log( b ) + d


######################## Fitting ####################   

popt, pcov = curve_fit( logf, optxlst, optylst) #<-this causes the error
print( popt )



######################## Plotting ###################

xticks=[]
for a in range( 0, 91, 10 ):
    xticks.append( a ) 

plt.plot( optxlst,optylst, 'bo' , markersize=3, label="Datepoints" )
plt.xticks( xticks )
plt.legend()
plt.title( 'Curve fitting of Stab.check', fontsize='16')
plt.xlabel( 'Iteration', fontsize='16')
plt.ylabel( 'Xcoord[um]', fontsize='18')
plt.show()

标签: pythonplotcurve-fittinglogarithm

解决方案


推荐阅读