1 Star 2 Fork 0

itg / somepython

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
曲线拟合.py 698 Bytes
一键复制 编辑 原始数据 按行查看 历史
itg 提交于 2023-05-24 22:46 . first commit
from IPython.core.interactiveshell import InteractiveShell
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
# 定义拟合函数
def func(x, a, b, c):
return a * x ** 2 + b * x + c
data = np.loadtxt('data.txt')
# 实验数据
#x = [1, 2, 3, 4, 5]
#y = [1, 1, 6, 10, 15]
# 实验数据
x = data[:, 0]
y = data[:, 1]
# 调用curve_fit进行拟合
popt, pcov = curve_fit(func, x, y)
# 打印拟合参数
print(popt)
# [ 1. 2. 1.]
# 绘制拟合曲线
x1 = np.linspace(0, 6, 100)
y1 = func(x1, popt[0], popt[1], popt[2])
plt.plot(x1, y1, 'r-', label='fit')
plt.plot(x, y, 'o', label='data')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
1
https://gitee.com/x-itg/my_curve_fit.git
git@gitee.com:x-itg/my_curve_fit.git
x-itg
my_curve_fit
somepython
main

搜索帮助