Nonlinear magnetic model fit -with a definite integral- using scipy.integrate.quad and lmfit

妖精的绣舞 提交于 2019-12-12 04:18:07

问题


I would like to fit an hysteresis curve, with a superparamagnetic behavior, using a magnetic model which includes a Langevin function and a pair distribution function [1]. To fit with this equation I must solve a definite integral. I was trying to use scipy.integrate.quad for this purpose and the features of lmfit, but I do not get -at least- to simulate a reasonable curve (see the code below). Some real physical parameters that can be used to simulate this equation are: Dm = 3.2E-9 m, w = 0.26, NT = 1.7E12, kB=1.38E-23 J/K and T=300K. Simulating with this values it must result in a superparamagnetic curve as the contained in the link below. I would appreciate any suggestion to make this code work and to improve it.

Langevin equation with PDF

www.dropbox.com/pri/get/superpara.dat?_subject_uid=197016565&w=AADkHqW1w-gE9pQkG0oLoE7tNG1J-rWxN0lcIM9ioXWiLA

from lmfit import minimize, Minimizer, Parameters, Parameter, report_fit
from numpy import loadtxt, vectorize, sqrt, log, log10, inf, exp, pi, tanh
from scipy.integrate import quad
import matplotlib.pyplot as plt

dat = loadtxt('superpara.dat')

u0_H   = dat[:, 0]
dat1 = dat[:, 1]

def PDF(Dmag, u0_H, params):
    v = params.valuesdict()
    pdf = (1/(v['w']*sqrt(2*pi)*Dmag))*exp(-(log(Dmag/v['Dm']))**2 / (2*v['w']**2))
    x = (pi/(6*v['kB']*v['T']))*v['Ms']*(u0_H*1e-4)*Dmag**3
    return (pi/6)*v['Ms']*(Dmag**3)*( (1/tanh(x))-(1/x) )*pdf

def curve(u0_H, params):
    return params['NT']*quad(PDF, 0.0, inf, args=(u0_H, params))[0]

vcurve = vectorize(curve, excluded=set([1]))

def fit_function(params, u0_H, dat1):
    model1 = vcurve(u0_H, params)
    resid1 = dat1 - model1
    return resid1.flatten()

params = Parameters()
params.add('Dm' , value= 3.2e-9  , vary= True)
params.add('w'  , value= 0.26    , vary= True)
params.add('Ms' , value= 0.1     , vary= True)
params.add('T'  , value= 300     , vary= False)
params.add('kB' , value= 1.38e-23, vary= False)
params.add('NT' , value= 1.7e12  , vary= True)

minner = Minimizer(fit_function, params, fcn_args=(u0_H, dat1))
result = minner.minimize()

report_fit(result)

y1_fit = vcurve(u0_H, result.params)
y1_init = vcurve(u0_H, params)

plt.plot(u0_H, dat1, 'k+', u0_H, y1_init, 'b-', u0_H, y1_fit, 'r-')
plt.show()

来源:https://stackoverflow.com/questions/44464981/nonlinear-magnetic-model-fit-with-a-definite-integral-using-scipy-integrate-qu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!