python应用 曲线拟合04 → 多项式拟合


  • 主要是使用 numpy 库中的 polyfit() 函数,见第 66 行, z = np.polyfit(x_proton, y, 3) ,其中待拟合曲线的横纵坐标分别为第 1, 2 个参数,第 3 个参数 3 表示使用 3 次多项式拟合。得到的拟合结果写入返回列表 z,按照幂指数从高到低排序,比如这里 z[0] 代表 x3 前面的系数,z[1] 代表 x2 前面的系数,以此类推。
 import xlrd
import xlwt
import numpy as np
import matplotlib.pyplot as plt def pol2(x, a, b, c):
x = np.array(x)
return a + b*x + c*pow(x, 2) def pol3(x, a, b, c, d):
x = np.array(x)
return a + b*x + c*pow(x, 2) + d*pow(x, 3) def pol4(x, a, b, c, d, e):
x = np.array(x)
return a + b*x + c*pow(x, 2) + d*pow(x, 3) + e*pow(x, 4) def pol5(x, a, b, c, d, e, f):
x = np.array(x)
return a + b*x + c*pow(x, 2) + d*pow(x, 3) + e*pow(x, 4) + f*pow(x, 5) def pol6(x, a, b, c, d, e, f, g):
x = np.array(x)
return a + b*x + c*pow(x, 2) + d*pow(x, 3) + e*pow(x, 4) + f*pow(x, 5) +\
g*pow(x, 6) def main():
data_file = xlrd.open_workbook('./data_78As.xlsx')
sheet_proton = data_file.sheet_by_name('proton')
sheet_neutron = data_file.sheet_by_name('neutron') write_excl = xlwt.Workbook(encoding='utf-8')
write_sheet_proton = write_excl.add_sheet('proton')
write_sheet_neutron = write_excl.add_sheet('neutron') x_proton = sheet_proton.col_values(0)
x_proton_add = x_proton[:]
x_proton_add.insert(0, 0.23)
x_proton_add.insert(0, 0.22)
x_proton_add.insert(0, 0.21)
x_proton_add.insert(0, 0.20)
x_proton_add.append(0.36)
x_proton_add.append(0.37)
x_proton_add.append(0.38)
x_proton_add.append(0.39)
x_proton_add.append(0.40) j = 0
for temp in x_proton_add:
write_sheet_proton.write(j, 0, temp)
j = j + 1 for i in range(18):
y = sheet_proton.col_values(i+1)
plt.scatter(x_proton, y)
print(y)
# popt, pcov = curve_fit(pol5, x_proton, y, [1, 1, 1, 1, 1, 1])
# print(popt) z = np.polyfit(x_proton, y, 3) y_fit = pol3(x_proton_add, z[3], z[2], z[1], z[0])
plt.plot(x_proton_add, y_fit, color='green', linewidth=1.0)
plt.show()
j = 0
for temp in y_fit:
print(x_proton_add[j])
if x_proton_add[j] <= 0.23:
write_sheet_proton.write(j, i+1, temp)
j = j + 1
elif x_proton_add[j] > 0.23 and x_proton_add[j] < 0.35:
write_sheet_proton.write(j, i+1, y[j-4])
j = j + 1
else:
write_sheet_proton.write(j, i+1, temp)
j = j + 1 plt.clf()
x_neutron = sheet_neutron.col_values(0)
x_neutron_add = x_neutron[:]
x_neutron_add.insert(0, 0.23)
x_neutron_add.insert(0, 0.22)
x_neutron_add.insert(0, 0.21)
x_neutron_add.insert(0, 0.20)
x_neutron_add.append(0.36)
x_neutron_add.append(0.37)
x_neutron_add.append(0.38)
x_neutron_add.append(0.39)
x_neutron_add.append(0.40) j = 0
for temp in x_neutron_add:
write_sheet_neutron.write(j, 0, temp)
j = j + 1 for i in range(18):
y = sheet_neutron.col_values(i+1)
plt.scatter(x_neutron, y)
print(y)
# popt, pcov = curve_fit(pol5, x_proton, y, [1, 1, 1, 1, 1, 1])
# print(popt) z = np.polyfit(x_neutron, y, 3) y_fit = pol3(x_neutron_add, z[3], z[2], z[1], z[0])
plt.plot(x_neutron_add, y_fit, color='green', linewidth=1.0)
plt.show()
j = 0
for temp in y_fit:
if x_neutron_add[j] <= 0.23:
write_sheet_neutron.write(j, i+1, temp)
j = j + 1
elif x_neutron_add[j] > 0.23 and x_neutron_add[j] < 0.35:
write_sheet_neutron.write(j, i+1, y[j-4])
j = j + 1
else:
write_sheet_neutron.write(j, i+1, temp)
j = j + 1 write_excl.save("result_78As.xls") if __name__ == '__main__':
main()

得到结果:

python应用 曲线拟合04的更多相关文章

  1. Python web前端 04 盒子模型

    Python web前端 04 盒子模型 盒子模型是由内容(content).内边距(padding).外边距(margin).边框(border)组成的 一.边框 border #border 边框 ...

  2. Python并发编程04 /多线程、生产消费者模型、线程进程对比、线程的方法、线程join、守护线程、线程互斥锁

    Python并发编程04 /多线程.生产消费者模型.线程进程对比.线程的方法.线程join.守护线程.线程互斥锁 目录 Python并发编程04 /多线程.生产消费者模型.线程进程对比.线程的方法.线 ...

  3. Python网络编程04 /recv工作原理、展示收发问题、粘包现象

    Python网络编程04 /recv工作原理.展示收发问题.粘包现象 目录 Python网络编程04 /recv工作原理.展示收发问题.粘包现象 1. recv工作原理 2. 展示收发问题示例 发多次 ...

  4. Configuring Ubuntu for deep learning with Python in Ubuntu16.04

    博主最近浏览到一个网站PyImageSearch,看到里面的项目还不错,就顺手配置一下环境,试着去跑下里面的模型. 首先,需要配置好需要运行模型的环境,其实主要的步骤分为以下三步: 1. 安装Ubun ...

  5. 【Python】torrentParser1.04 增加获得磁力链URI功能

    代码: #------------------------------------------------------------------------------------ # torrentP ...

  6. chrome headless+selenium+python+(ubuntu 16.04/centos7) 下的实现

    Ubuntu 16.04 下: 0x01 安装chrome 1 下载源加入系统源列表 sudo wget http://www.linuxidc.com/files/repo/google-chrom ...

  7. Python标准库04 文件管理 (部分os包,shutil包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在操作系统下,用户可以通过操作系统的命令来管理文件,参考linux文件管理相关命令 ...

  8. Python快速学习04:循环 & 函数

    前言 系列文章:[传送门] 也就今天认识了 LC ,很开心. 本文目录 循环 for while 中断 函数 函数定义 函数调用 for循环 Python 中的for 循环象shell 脚本里的for ...

  9. python数据分析---第04章 NumPy基础:数组和矢量计算

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

随机推荐

  1. 【Codeforces】CF Round #592 (Div. 2) - 题解

    Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...

  2. Javascript之其实我觉得原型链没有难的那么夸张!

    原型链.闭包.事件循环等,可以说是js中比较复杂的知识了,复杂的不是因为它的概念,而是因为它们本身都涉及到很多的知识体系.所以很难串联起来,有一个完整的思路.我最近想把js中有点意思的知识都总结整理一 ...

  3. CSS动画实例:升空的气球

    1.制作一个气球 设页面中有<div class="balloon"></div>,为. balloon设置样式规则如下: .balloon { heigh ...

  4. Dubbo系列之 (四)服务订阅(1)

    辅助链接 Dubbo系列之 (一)SPI扩展 Dubbo系列之 (二)Registry注册中心-注册(1) Dubbo系列之 (三)Registry注册中心-注册(2) Dubbo系列之 (四)服务订 ...

  5. 手机APP无法抓包(无法连接服务器)

    一. 把证书放到系统信任区 前提:手机已root 详细步骤 计算证书名 openssl x509 -subject_hash_old -in charles-ssl-proxying-certific ...

  6. python基础 Day4

    python Day4 1.列表 列表初识 之前的的三种str.int.bool在有的条件下不够用 str:存储少量的数据. 切片还是对其进行任何操作,获取的内容都是str类型.存储的数据单一. 列表 ...

  7. J20航模遥控器开源项目系列教程(二)使用说明 | 遥控器制作完成了,怎么用?

    我们的开源宗旨:自由 协调 开放 合作 共享 拥抱开源,丰富国内开源生态,开展多人运动,欢迎加入我们哈~ 和一群志同道合的人,做自己所热爱的事! 项目开源地址:https://github.com/C ...

  8. WordCount( Java )

    Github项目地址:https://github.com/Sabot1203/WordCount 一. 题目描述 实现一个简单而完整的软件工具(源程序特征统计程序). 进行单元测试.回归测试.效能测 ...

  9. go二叉树、struct、接口

    二叉树实现.及遍历 二叉树定义 type Student struct { Name string left* Student right* Student } 如果每个节点有两个指针分别用来指向左子 ...

  10. 这可能是最详细的解析HTTP走私攻击的文章

    前言 HTTP Desync Attacks也就是HTTP走私攻击,是我见到的比较有趣的一种攻击方式,这里来对这种漏洞进行介绍. TL;DR HTTP走私攻击利用了HTTP协议本身的问题:HTTP中存 ...