使用Sellmeier方程(Sellmeier Dispersionfitting)拟合波长-折射率数据——Python实现
文章目录
- 一、前言
- 二、数据处理
- 三、Python代码
- 四、使用及其说明
- 五、总结
一、前言
对于已测量出的 波长-折射率 数据,我们希望找到一个方程来拟合它,这里我们采用三阶 selmier
方程,其形式如下:
n(x)=1+B1x2x2−C12+B2x2x2−C22+B3x2x2−C32n(x) = \sqrt{1 + \dfrac{B_1x^2}{x^2-C_1^2} + \dfrac{B_2x^2}{x^2-C_2^2} + \dfrac{B_3x^2}{x^2-C_3^2}} n(x)=1+x2−C12B1x2+x2−C22B2x2+x2−C32B3x2
我们利用已知的波长数据(Wavelength)与折射率数据(Refractive Index)去拟合方程中的 B1, C1, B2, C2, B3, C3
等参数。
二、数据处理
我们有一段关于水的折射率与入射光波长的关系数据:
Wavelength(x) | RefractiveIndex(n) |
---|---|
0.5 | 1.335 |
0.6 | 1.3321 |
0.7 | 1.3311 |
0.8 | 1.329 |
0.9 | 1.3281 |
1 | 1.3272 |
1.1 | 1.3255 |
1.2 | 1.3244 |
1.3 | 1.3225 |
1.4 | 1.3213 |
1.5 | 1.319 |
将其保存在 Water.txt
文档内,与我们接下来的 Python 文件放在同一个目录下。
三、Python代码
计算折射率的 selmier
方程代码如下:
def selmier(L, B1, C1, B2, C2, B3, C3):offset = 1# L = x ** 2ssum = B1 * L / (L - C1 ** 2) + B2 * L / (L - C2 ** 2) + B3 * L / (L - C3 ** 2)nn = offset + ssumreturn nn
拟合采用的是 pycse
包中的 nlinfit
函数,其用法和 MATLAB 类似,拟合代码如下:
pars, pint, se = nlinfit(selmier, Lambda ** 2, n ** 2, coeffs, 0.05)
注意这里的 coeffs
为我们给定的初始参数值,这里我初始参数(B1, C1, B2, C2, B3, C3
)全部设为 0.1。完整代码如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 30 19:38:56 2022
@author: zq
"""import numpy as np
import matplotlib.pyplot as plt
from pycse import nlinfitdef selmier(L, B1, C1, B2, C2, B3, C3):offset = 1# L = x ** 2ssum = B1 * L / (L - C1 ** 2) + B2 * L / (L - C2 ** 2) + B3 * L / (L - C3 ** 2)nn = offset + ssumreturn nndef Welcome():w = """
===================================================\n
Created on Wed Mar 30 19:38:56 2022\tThe fit function is Show in README.txt. \tThis code can solve this problem, but the requirements for the initial predicted value are relatively high. It is recommended that you use a matlab code. \tYou can contact me to obtain a copy of matlab code.\t\tEmail: z.q.feng@qq.com\n
\t\t\tor\n
\t\tBlog: https://blog.csdn.net/weixin_46584887?spm=1010.2135.3001.5343\tOf course if you know how to use matlab...@author: z.q.feng\n
==================================================="""print(w, end = '')if __name__ == '__main__':Welcome()# coeffs = np.loadtxt('settings.txt')coeffs = np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1])if len(coeffs) != 6:raise ValueError("The coefficients' number that noticed in the settings must be equal to 6 !")while (True):file = input("\nPlease enter the datas' filename(without .txt) : ")tmp = np.loadtxt(file + '.txt')Lambda, n = tmp[:, 0], tmp[:, 1]try:pars, pint, se = nlinfit(selmier, Lambda ** 2, n ** 2, coeffs, 0.05)except RuntimeError:print('\n\tOptimal parameters not found.\n\tPlease change the coefficients in settings...\n')breaknn = selmier(Lambda ** 2, pars[0], pars[1], pars[2], pars[3], pars[4], pars[5]) ** 0.5tmp = np.linalg.norm(n - nn, 2)print('\n\tSuccessfully create coeffs-' + file + '.txt and fit-' + file + '.png.')print('\n\tThe recovery residual is %.6f.' % (tmp))with open("coeffs-" + file + ".txt", 'w') as f:f.write('\t' + file + '\n')for i in range(len(pars)):if i % 2 == 0:f.write('B' + str(i // 2 + 1) + '\t' + str(pars[i]) + '\n')else:f.write('C' + str(i // 2 + 1) + '\t' + str(pars[i]) + '\n')plt.figure(figsize = (8, 6))plt.plot(Lambda, n, 'r-', label = 'Original')plt.plot(Lambda, nn, 'b--', label = 'Fit')plt.xlabel('Wave length')plt.ylabel('Refractive index')plt.title('Original-curve vs. Fit-curve')plt.grid()plt.legend()plt.savefig("fit-" + file + ".png", dpi = 300)q = input("\nQuit ? (y / n) : ")if q == 'y':breakelse:continue
四、使用及其说明
运行上述代码,运行界面如下:
这里我们输入的为我们的 Water.txt
文件的文件名,可循环输入多个拟合多组数据。输出有两个:coeff-Water.txt
和 fit-Water.png
。
coeff-Water.txt:
Water
B1 7.876519563674877
C1 0.2569073177409549
B2 7.874100532538284
C2 0.25690470607109844
B3 -15.01175094249148
C3 0.2607512075672549
fit-Water.png:
其中,终端还会输出我们的拟合残差:
Please enter the datas' filename(without .txt) : WaterSuccessfully create coeffs-Water.txt and fit-Water.png.The recovery residual is 0.004401.Quit ? (y / n) :
五、总结
Python 的 nlinfit
函数精度不如 MATLAB 的高,但是配置 MATLAB 环境远比配置搭建 Python 环境困难,此外,我还编写了一份 Windows
下可直接运行出结果的 EXE
文件来拟合参数,上传到资源里啦:
使用Sellmeier方程(Sellmeier Dispersionfitting)拟合波长-折射率数据——Python实现相关推荐
- Matlab 隐函数方程求解最小二乘法拟合一阶线性拟合二阶拟合传感器实验
九层妖塔 起于垒土 Matlab 最小二乘法拟合一阶线性拟合&传感器实验 一.代码 二.数据处理结果 三.Notes 一.代码 %电容传感器位移实验数据 最小二乘法一阶线性拟合 x = ...
- MATLAB快速拟合二组数据
MATLAB快速拟合二组数据 第一步:打开MATLAB,点击主页中的新建变量,点击修改变量名为a,然后复制数据进去,接着新建变量b,复制数据进去. 第二步:点击上端的APP,选择第一个图标 第三步:选 ...
- python拟合非线性模型_python-绘制分段拟合到非线性数据
我有一个与this previous StackOverflow question类似的问题.我有一个数据集,我想将几??个分段函数拟合到该数据集中,然后绘制结果. 数据在下面以红色绘制. 为了提供一 ...
- matlab数据拟合语句,Matlab数据拟合程序 - 范文中心
课程设计名称: 设计二:数据拟合 指导教师: 张莉 课程设计时数: 6 课程设计设备:安装了Matlab .C ++软件的计算机 课程设计日期: 实验地点: 第五教学楼北902 课程设计目的: 1. ...
- Tensorflow基本开发步骤——以逻辑回归拟合二维数据为例
深度学习大概有如下四个步骤:准备数据,搭建模型,迭代训练和使用模型. 一.准备数据 使用y=2x作为主体,通过加入一些随机干扰噪声产生一组数据.代码如下: import tensorflow as t ...
- pythonchar中的拟合方法_在python中利用numpy求解多项式以及多项式拟合的方法
构建一个二阶多项式:x^2 - 4x + 3 多项式求解 >>> p = np.poly1d([1,-4,3]) #二阶多项式系数 >>> p(0) #自变量为0时 ...
- 【Pytorch神经网络实战案例】06 逻辑回归拟合二维数据
1 逻辑回归与拟合过程 1.1 准备数据-code_01_moons.py(第1部分) import sklearn.datasets import torch import numpy as np ...
- python解zuobiaoxi方程_滑坡稳定性分析程序初探---Python版!
0 前言 山体滑坡是常见的自然灾害,从理论分析的角度讲,滑坡的稳定性分析方法源自于高中物理学,如图1所示.前者的滑动分析非常简单,在已知滑块的重量以及接触面摩擦系数的基础上通过计算下滑力和抗滑力的关系 ...
- 深度学习之TensorFlow 第三章基本开发步骤--以逻辑回归拟合二维数据为例(转)
深度学习有四个步骤: 准备数据 搭建模型 迭代训练 使用模型 import tensorflow as tf import numpy as np #数组 import matplotlib ...
最新文章
- 特征工程(feature engineering)是什么?特征工程(feature engineering)包含哪些方面?
- 记一个自己项目上线的全过程
- 已经围上为何不算目_在湖人打球顺风顺水,戴维斯为何还要亏本卖掉洛杉矶豪宅?...
- linux 多个会话同时执行命令后history记录不全的解决方案
- 剑指Offer 二维数组中的查找
- 前端 Offer 提速:如何写出有亮点的简历
- python 网络编程----非阻塞或异步编程
- python增强对比度_python增加图像对比度的方法
- 刷网络课_网络营销实践心得—刘荟萌
- 【操作系统】Semaphore处理读者-写者问题
- bax在计算机英语的意思,BaX(X=S,Se,Te)的电子结构计算
- 独家对话 HybridOS 操作系统掌门人魏永明:“我们的目标是取代物联网中的安卓” | 人物志
- Varnish 缓存
- 【C语言开源项目】盘点 GitHub 上不错的 4 个C语言项目
- 计算机管理usb出现问号,在Windows操作系统的设备管理器中的多个设备名上有问号...
- 方法重写与方法重载的区别
- HC32L130基于Xmodem协议实现IAP串口在线升级
- j计算机屏幕关闭时间,win7如何设置自动关闭电脑屏幕的时间?
- FinalCutPro快捷键
- 仓库智能化管理:WMS仓储管理系统解决方案
热门文章
- ZWAVE Notification Command Class, Version 3-8
- 趣链科技BlocFace平台全量通过可信区块链BaaS评测
- 高数数学——多元函数微分学
- 天意U盘维护系统1.8无法用Ultroiso制作
- SAP 物料移动类型
- Developer Distribution Agreement
- 【历史上的今天】4 月 20 日:中国接入国际互联网;戴尔登顶 PC 市场;计算机先驱诞生日
- 一周小结(二)——说说jar包那些事儿
- 螺栓拧紧失效原因与控制方法
- android 以音频播放器为例实现通知栏显示通知,并实现切歌、暂停、播放,并实现加载网络图片,并实现关闭第三方APP音频