如何拟合包含带有变量限制的整数的函数? [英] How do I fit a function that includes an integral with a variable limit?

查看:145
本文介绍了如何拟合包含带有变量限制的整数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python还是很陌生(还有Stack Overflow,所以如果我做不到这一点,对不起!)。为了将宇宙学参数ΩM和ΩΛ提取出来,我尝试将以下方程式拟合到一些数据中:

I'm very new to Python (and also Stack Overflow so sorry if I'm not doing this right!). I'm trying to fit the following equation to some data in order to extract the cosmological parameters ΩM and ΩΛ:

适合的方程式

其中

曲线D方程

在我的方程中,大写卷曲D = Ho * dl,因此Ho已被抵消。我目前正在尝试使用ΩM+ΩΛ= 1形式。我有m(z)和z的数据,而且我知道卷曲的M常数。

In my equation, capital curly D=Ho*dl so the Ho has been cancelled out. I'm currently trying to use the ΩM+ΩΛ=1 form. I have the data for m(z) and z, and I know the curly M constant.

这是我目前的代码:

import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

d=np.loadtxt("data.txt")
z=d[:,0]
m=d[:,7]
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
    return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(z,OM,OV):
    return M+5*np.log10(c*(1+z)*integrate.quad(fn,0,z,args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)

运行此命令时,出现错误:

When I run this, I get the error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() 
or a.all()

我知道这是因为您不能在quad函数中使用独立的数组变量作为限制,但是我不确定如何解决这个问题,因为我发现或尝试实现的任何方法都没有用。非常感谢任何帮助或提示!先感谢您。

I understand that this is because you can't use an independent array variable as a limit in the quad function, but I'm not sure how I would go about correcting this as nothing I've found or tried to implement has worked. Very much appreciate any help or hints! Thank you in advance.

更新:

以下是我一直在使用的一些示例数据:

here is some example data that I have been using:

z=[0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079, 
0.088, 0.063, 0.071, 0.052, 0.05]
m=[16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61, 
18.27, 19.28, 18.24, 18.33, 17.54, 17.69]


推荐答案

不确定,请确保这不是您要找的答案,但这可能会帮助您找到解决(科学)问题的真正方法。

I'm not sure that it is an answer you looking for, but it may help you to find a real solution for your (scientific) problem.

在编程部分出现错误是因为 curve_fit 发送一个向量z作为函数 curve 的第一个参数。 integrate.quad 希望两个浮点数(积分限制)作为第二个和第三个参数。好吧, integrate.quad 在第三个参数中得到一个数组,对此感到非常不高兴-您奇怪的错误消息。

So, on the programming part, the error appears because curve_fit sending a vector of z as a first argument of the function curve However integrate.quad wants two float numbers (integration limits) as the second and third arguments. Well, integrate.quad gets an array in the third argument, and is very upset about this - your strange error message.

现在我想您想将0积分到最大z 。如果是这样,您的代码应如下所示:

NOW I guess you want to integrate from 0 to a biggest z. If so your code should look like this:

import numpy as np
import scipy.integrate as integrate
from scipy.optimize import curve_fit

z=np.array( [0.03, 0.05, 0.026, 0.075, 0.026, 0.014, 0.101, 0.02, 0.036, 0.045, 0.043, 0.018, 0.079, 
0.088, 0.063, 0.071, 0.052, 0.05] )
m=np.array( [16.26, 17.63, 16.08, 18.43, 16.28, 14.47, 19.16, 15.18, 16.66, 17.61, 17.19, 15.61, 
18.27, 19.28, 18.24, 18.33, 17.54, 17.69] )
c=299792458 #speed of light
M=-18.316469239 #curly M


def fn(Z,OM,OV):
    return np.power((((1+Z)**2)*(1+OM*Z)-Z*(2+Z)*OV),-0.5)

def curve(R,OM,OV):
    return M+5*np.log10(c*(1+R)*integrate.quad(fn,0,R[-1],args=(OM,OV))[0])

parameter = curve_fit(curve,z,m)

print parameter

这会产生一些结果:

(array([-61.73621869, -42.41853305]), array([[5.43407019e+15, 2.84207191e+15],
       [2.84207191e+15, 1.48643143e+15]]))

可能不是有用的数字。至少现在您了解了错误的根源,并可以通过更改代码的方式来解决最大的宇宙学问题。

which may or may not be useful numbers. At least now you understand a source of the error and can change your code in such a way, that you will solve the biggest cosmological problem.

祝您好运!

这篇关于如何拟合包含带有变量限制的整数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆