python拟合sir模型_用python将数据拟合到自定义模型

下面的代码将scipy的曲线拟合参数限制在指定的范围内。在本例中,第一个参数的边界为+/-无穷大(无界),第二个参数的边界为+/-100,但拟合参数在边界内并正常拟合,第三个参数受其边界限制。在import numpy

import matplotlib

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

xData = numpy.array([5.0, 6.1, 7.2, 8.3, 9.4])

yData = numpy.array([ 10.0, 18.4, 20.8, 23.2, 35.0])

def standardFunc(data, a, b, c):

return a * data + b * data**2 + c

# some initial parameter values - must be within bounds

initialParameters = numpy.array([1.0, 1.0, 1.0])

# bounds on parameters - initial parameters must be within these

lowerBounds = (-numpy.Inf, -100.0, -5.0)

upperBounds = (numpy.Inf, 100.0, 5.0)

parameterBounds = [lowerBounds, upperBounds]

fittedParameters, pcov = curve_fit(standardFunc, xData, yData, initialParameters, bounds = parameterBounds)

# values for display of fitted function

a, b, c = fittedParameters

# for plotting the fitting results

xPlotData = numpy.linspace(min(xData), max(xData), 50)

y_plot = standardFunc(xPlotData, a, b, c)

plt.plot(xData, yData, 'D') # plot the raw data as a scatterplot

plt.plot(xPlotData, y_plot) # plot the equation using the fitted parameters

plt.show()

print('fitted parameters:', fittedParameters)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值