通过scipy.optimize.curve_fit拟合曲线并固定端点

时间:2019-04-13 22:18:43

标签: scipy curve-fitting endpoint minimization

以下脚本通过curve_fit(来自scipy.optimize)拟合曲线弓形,如下所示:

ydata = numpy.array[ 1.6504   1.63928044  1.62855028  1.6181874   1.60817119 1.59848249   1.58910347  1.58001759  1.57120948  1.56266487  1.55437054  1.54631424   1.5384846   1.53087109  1.52346397  1.5162542   1.5092334   1.50239383   1.4957283   1.48923013  1.48289315  1.47671162  1.4706802   1.46479393  1.45904821  1.45343874  1.44796151  1.44261281  1.43738913  1.43228723  1.42730406  1.42243677  1.4176827   1.41303936  1.40850439  1.40407561  1.39975096  1.39552851  1.39140647  1.38738314  1.38345695  1.37962642  1.37589018  1.37224696  1.36869555  1.36523487  1.36186389  1.35858169  1.35538741  1.35228028  1.34925958  1.34632469  1.34347504  1.34071015  1.33802957  1.33543295  1.33291998  1.33049042  1.32814407  1.32588081  1.32370057  1.32160331  1.31958908  1.31765795  1.31581005  1.31404556  1.31236472  1.3107678   1.30925513  1.30782709  1.30648411  1.30522666  1.3040553   1.30297062  1.30197327  1.30106398  1.30024355  1.29951286  1.29887287  1.29832464  1.29786933  1.29750821  1.29724268  1.29707426  1.29700463  1.29703564  1.29716927  1.29740773  1.2977534   1.29820885  1.29877688  1.29946049  1.3002629   1.30118751  1.30223793  1.30341792  1.30473139  1.30618232  1.30777475  1.30951267  1.3114    ]

xdata = numpy.array[ 0.    0.01  0.02  0.03  0.04  0.05  0.06  0.07  0.08  0.09 0.1   0.11 0.12  0.13  0.14  0.15  0.16  0.17  0.18  0.19  0.2   0.21  0.22  0.23  0.24  0.25  0.26  0.27  0.28  0.29  0.3   0.31  0.32  0.33  0.34  0.35  0.36  0.37  0.38  0.39  0.4   0.41  0.42  0.43  0.44  0.45  0.46  0.47  0.48  0.49  0.5   0.51  0.52  0.53  0.54  0.55  0.56  0.57  0.58  0.59  0.6   0.61  0.62  0.63  0.64  0.65  0.66  0.67  0.68  0.69  0.7   0.71  0.72  0.73  0.74  0.75  0.76  0.77  0.78  0.79  0.8   0.81  0.82  0.83  0.84  0.85  0.86  0.87  0.88  0.89  0.9   0.91  0.92  0.93  0.94  0.95  0.96  0.97  0.98  0.99  1.  ]


sigma = np.ones(len(xdata))
sigma[[0, -1]] = 0.01


def function_cte(x, b):
     return 1.31*x + 1.57*(1-x) - b*x*(1-x)

def function_linear(x, c1, c2):
     return 1.31*x + 1.57*(1-x) - (c1+c2*x)*x*(1-x)

popt_cte, pcov_cte = curve_fit(function_cte, xdata, ydata, sigma=sigma)
popt_lin, pcov_lin = curve_fit(function_linear, xdata, ydata, sigma=sigma)

但是我得到了图enter image description here中的情节,

即,两个函数的初始点都与要拟合的数据不一致(xdata,ydata)。

我想在端点(0.0,1.57)和(1.0,1.31)的同一点上限制拟合,以最大程度地减少误差。基于此代码的任何想法还是最好采用另一种方式?

谢谢!

0 个答案:

没有答案