大学での物理実験 (単純スリット実験) からの一連のデータがあり、このデータを lmfit ライブラリから構築したモデルに当てはめようとしています。次の形式の副鼻腔の正方形が必要です。
I(X)= I0.sinc²(pi.aX/(ラムダ.D))
a : スリットの幅、lambda : 光の波長 D : 距離カメラ/スリット I0 : 元の強度
import csv as csv
from math import pi
import matplotlib.pyplot as plt
import numpy as np
from lmfit import *
# create data to be fitted
with open('data_1.csv', 'r') as f:
values = list(csv.reader(f, delimiter=','))
values = np.array(values[1:], dtype=np.float)
position = values[:, 0]
intensity = values[:, 1]
#define function model
def fct(x, I0, a, D, b):
return I0 * np.square(np.sinc(pi * a * (x + b) / (0.00000063 * D)))
#b is for the horizontal shift because my experience
#was centered on 700 due to the camera
# do fit
vmodel = Model(fct)
vmodel.set_param_hint('I0', min=0., max=300.)
vmodel.set_param_hint('a', value=0.0005, min=0.0, max=1.)
vmodel.set_param_hint('D', value=0.53, min=0.0, max=1.)
vmodel.set_param_hint('b', min=0., max=2000.)
pars = vmodel.make_params()
result = vmodel.fit(intensity, pars, x=position)
# write report
print(result.fit_report())
#after we plot the data, with position on x and intensity on y
完全に間違った値とエラーを返します:
RuntimeWarning: invalid value encountered in double_scalars spercent =
'({0:.2%})'.format(abs(par.stderr/par.value))
[[Model]]
Model(fct)
[[Fit Statistics]]
# function evals = 7
# data points = 1280
# variables = 4
chi-square = 4058147.794
reduced chi-square = 3180.367
Akaike info crit = 10326.876
Bayesian info crit = 10347.494
[[Variables]]
I0: 0 +/- 0 (nan%) (init= 0)
a: 0.00050000 +/- 0 (0.00%) (init= 0.0005)
D: 0.50000000 +/- 0 (0.00%) (init= 0.5)
b: 400 +/- 0 (0.00%) (init= 400)
私を手伝ってくれますか ?このライブラリから多くの型モデルを試しましたが、何も正しく機能せず、本当に必要です。私はすでにnp.squareで2Dの問題を解決しており、その他の読み物もあり、主な問題はモデルです。回答をお待ちしております。