1

I'm currently trying to fit some parameters to an existing data file. After adding a fitting routine I keep getting the 'TypeError: '*numpy.float64' object is not iterable*' error, which seems to have something to do with the Dl function that i defined. I haven't been able to solve this myself, so I'd be really grateful for any tips concerning this matter.

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)

2 Answers 2

1

In your routine Dl, the iteration over z is invalid. z is a scalar at each invokation.

Transform your program so that either Dl is given an array or remove the loop in Dl.

Sign up to request clarification or add additional context in comments.

Comments

1

Your problem seems to be here:

chi2_min=m(omega_min, M_min, 70, M)

omega_min is a float, which gets passed to Dl() in m() here:

for j in Dl(z,omega_m,H0):

and then Dl() tries to iterate it:

for i in z:

which raises your error

To fix, I recommend you pass omega_min as a list:

chi2_min=m([omega_min], M_min, 70, M)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.