gpt4 book ai didi

Python 最小二乘法与 scipy.integrate.quad

转载 作者:行者123 更新时间:2023-12-01 05:36:27 26 4
gpt4 key购买 nike

我无法破译代码的错误消息,无法找到适合两个参数(eps 和 sig)的复杂最小二乘法的一些参数。

from pylab import *
import scipy
import numpy as np

from scipy import integrate, optimize

# Estimate parameters with least squares fit
T = [90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300]
B = [-0.2221, -0.18276, -0.15348, -0.13088, -0.11293, -0.09836, -0.086301, -0.076166, -0.067535, -0.060101, -0.053636, -0.047963, -0.04295, -0.038488, -0.034494, -0.030899, -0.027648, -0.02469, -0.022, -0.019534, -0.017268, -0.015181]


def funeval(Temp,eps,sig):
return -2.*np.pi*scipy.integrate.quad( lambda x: np.exp(4.*eps/Temp*((sig/x)**6.-(sig/x)**12.)*(x**2)) ,0.0,Inf )[0]

def residuals(p,y,Temp):
eps,sig = p
err = y-(funeval(Temp,eps,sig) )
return err

print funeval(90.,0.001, 0.0002)

plsq = scipy.optimize.leastsq(residuals, [0.00001, 0.0002], args=(B, T))

funeval 给出了合理的 float ,但当我运行代码时它返回:

error: Supplied function does not return a valid float.

该错误似乎对初始条件不敏感。我是 python 新手,因此任何帮助或帮助指南将不胜感激。谢谢。

最佳答案

对于 funeval(90.,0.001, 0.0002)Temp 是一个奇异值;但是,当您调用 scipy.optimize 时,您将整个 T 数组传递给 funeval 导致 scipy.integrate崩溃。

一个快速解决方法是执行以下操作:

def funeval(Temp,eps,sig):
out=[]
for T in Temp:
val = scipy.integrate.quad( lambda x: np.expm1( ((4.*eps)/T)* ((sig/x)**12.-(sig/x)**6.)* (x**2.) ), 0.0, np.inf )[0]
out.append(val)
return np.array(out)

def residuals(p,y,Temp):
eps,sig = p
err = y-(funeval(Temp,eps,sig) )
return err

print funeval([90],0.001, 0.0002)

plsq = scipy.optimize.leastsq(residuals, [0.00001, 0.0002], args=(B, T))
(array([ 3.52991175e-06, 9.04143361e-02]), 1)

不幸的是,这并不能很好地收敛。你能解释一下你想做什么吗?

关于Python 最小二乘法与 scipy.integrate.quad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18927786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com