scipy curve_fit 使用例子

本文提供了一个使用scipy库中curve_fit函数进行曲线拟合的实际示例,详细介绍了如何通过该函数来逼近数据并模拟复杂曲线。
摘要由CSDN通过智能技术生成

一个使用curve_fit来模拟曲线的例子

# From https://docs.scipy.org/
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Define func of parameters a, b, c and variable x
def func(x, a, b, c):
    return a * np.exp(-b * x) + c

# generate some noisy data and plot it
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
np.random.seed(1729)
y_noise = 0.2 * np.random.normal(size=xdata.size)
ydata = y + y_noise
plt.plot(xdata, ydata, 'b-', label='data')

#fit a, b, c and overplot
popt, pcov = curve_fit(func, xdata, ydata)
plt.plot(xdata, func(xdata, *popt), 'r-',
                    label='fit: a =%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

# Constrain the optimization to the region of 0 <= a <= 3.0 <= b <= 1 and 0 <= c <= 0.5
popt, pcov = curve_fit(func, xdata, ydata, bounds=(
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值