-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework2problem4parta.py
executable file
·62 lines (39 loc) · 1.46 KB
/
homework2problem4parta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import division
import numpy as np
#Simpson's integration rule, \int_{a}^{b} f(x) dx with N sample points, also in 1b
def simpson(f, x_init, x_final, N_simp):
h = (x_final-x_init)/N_simp
I = f(x_init) + f(x_final)
odds = [4*f(x_init + k*h) for k in range(1,N_simp,2)]
evens = [2*f(x_init + k*h) for k in range(2,N_simp,2)]
I += sum(odds) + sum(evens)
I *= h/3.
return I
OM, OL = 0.3, 0.7 #matter and DE fractions
H0 = 67 #km/s/Mpc
def H(z):
return H0 * np.sqrt(OM*(1+z)**3 + OL)
def D(z, N): #need a number of sample points to get convergence of integration scheme as requested
def f(zprime):
return (1+zprime)/(H(zprime)**3)
def Integrand(x):
'''
x = (zprime - z)/(1+(zprime-z)), transformation from Mark Newman's Computatational Physics
implicitly a variable of z but the for the integration routine the Integrand should only have one argument
'''
return (1/(1-x)**2) * f((x/(1-x)) + z)
eps_integration = 1e-14 #undefined at integal bound
Integral = simpson(Integrand, 0, 1-eps_integration, N) #integrate Integrand from x = 0 to x = 1-eps
return (5/2.) * OM * H(z) * H0**2 * Integral
z = 50
N = 2
eps = 1. #just need to be greater than requested accuracy
while eps > 1e-5:
Dnew, Dold = D(z, 2*N), D(z, N)
eps = abs(Dnew-Dold)
N *= 2
print('')
print('D(z = %i) is %.06f'%(z, Dnew))
print('')
with open('homework2problem4parta_result.txt', 'a') as f:
f.write('D(z = %i) is %.04f \n'%(z, Dnew))