我目前正在尝试将一些参数适合现有的数据文件。添加拟合例程后,我不断收到'TypeError: '*numpy.float64' object is not iterable*'
错误,这似乎与我定义的Dl函数有关。我自己无法解决此问题,因此,我非常感谢您提供与此相关的任何提示。
import pylab as p
import scipy as s
from scipy.integrate import odeint,quad
import numpy as np
import matplotlib.pyplot as plt
import math
z = np.arange(0.00, 1.5, 0.02)
z1, m1, sigma_m = np.loadtxt('data.txt', unpack=True, usecols=[0,1,2])
yerr = sigma_m
def H(z,omega_m,H0):
return H0*p.sqrt(omega_m*(1+z)**3+1-omega_m)
def Dl(z,omega_m,H0):
c = 3*10**5
y = []
for i in z:
y1 = c*(1+i)*quad(f,0.0,i, args=(omega_m,H0))[0]
y.append(y1)
return p.asarray(y)
def f(z,omega_m,H0):
return 1./H(z,omega_m,H0)
def m(z,omega_m,H0,M):
q = []
for j in Dl(z,omega_m,H0):
q1 = M+5*np.log10(j)+25.0
q.append(q1)
return p.asarray(q)
def chi2(omega_m, M):
return sum((m(z1,omega_m,70,M)-m1)/sigma_m)**2
chi2_min=1*10**30
o = np.arange(0.00, 1.5, 0.02)
Mrange = np.arange(-1.5, 1.5, 0.02)
for omega_m in o:
for M in Mrange:
if chi2(omega_m, M) < chi2_min:
omega_min=omega_m
M_min=M
chi2_min=m(omega_min, M_min, 70, M)
print(M_min)
print(chi2_min)
最佳答案
在例程Dl
中,对z的迭代无效。 z是每次调用的标量。
转换程序,以便为D1提供一个数组或删除D1中的循环。