首页 > 解决方案 > 具有无限求和的 Python 曲线拟合

问题描述

我正在尝试在 python 中进行曲线拟合,其中包括无限求和。

我总是出错。

from mpmath import nsum, exp, inf
import numpy as np
import pylab
from scipy.optimize import curve_fit

# get the data
y=[]
x=[]
for t in range(0,100,10):
    x += [t]
    y += [round(float((nsum(lambda n: exp(-(0.0002)*(n)*t),[0, inf]))),3)]
print(y)

# curve fitting
def test(t,D):
    return  [round(float((nsum(lambda n: exp(-(D)*(n)*t),[0, inf]))),3)]

                 
parameters, params_covariance=curve_fit(test, x, y)

print(parameters)

我希望我可以从曲线拟合函数中得到估计的 D 但我总是得到错误:

TypeError: cannot create mpf from array([mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('0.0'),
       mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('0.0')],
      dtype=object)

谢谢!

标签: pythonpython-3.xcurve-fitting

解决方案


您需要将 test() 的输入和输出列在列表中

def test(x,D):
    return  [float((nsum(lambda n: exp(-(D)*(n)*t),[0, inf]))) for t in x]

推荐阅读