0

I'm trying to compute an integral from z to 0.

The same problem has been asked and answered previously here: Integral with variable limits in python

However, they integrated from 0 to z. I need to do the same integral, but from z to 0. I followed the answer, but I'm unable to reverse the limits to be from z to 0. Is there any way to go about that?

Only posting the relevant parts - currently, I have:

from scipy.integrate import quad
from pylab import *
import numpy as np
import math
import matplotlib.pyplot as plt
from functools import partial

# Function to integrate for Cosmological Horizon
def cosmo_horizon(x):
        # Flat Space
        Omega2_m = 0.3
        Omega2_r = 10 ** (-4)
        Omega2_k = 0
        Omega2_l = 1 - Omega2_m - Omega2_r
        result = (Omega2_m * (1 + x) ** 3 + Omega2_r * (1 + x) ** 4 + Omega2_l + Omega2_k * (1 + x) ** 2) ** (-1 / 2)
        return result


def r(x):
    result = np.array(
        list(map(partial(quad, cosmo_horizon, 0), x)) # integrate cosmo_horizon from 0 to z
    )[:, 0]

    return result


# Light speed (km/s)
c = 3*(10**5)

# Hubble constant (km/s/Mpc)
H0 = 71

# Redshift Array
z_begin = 0.1
z_final = 1100.1
z_step = 0.1
z = np.arange(z_begin, z_final, z_step)

integral = r(z)
coeff = c / H0
integralCosmoHorizon = np.multiply(coeff, integral)
plt.plot(
    np.log10(z), integralCosmoHorizon, color='r',
    label='Euclidean Universe Ωk=0, Ωm=0.3, Ωr=10^-4, Ω_l=(1-Ωm-Ωr)'
)
plt.xlabel('Redshift log10(z)')
plt.ylabel('Cosmological Horizon (Mpc)')
plt.title('Cosmological Horizon vs Redshift')
plt.legend()
plt.show()

Again, the code above works to integrate from 0 to z, but I'd like to change the limits so it integrates from z to 0. How can I do that?

--

Otherwise, as a last resort (only if the above is not possible), is there any way to simply invert the x axis labels in the 0 to z integral plot (which I'm already getting) without inverting the graph/plot itself? Or invert the plot without inverting the labels? I've tried doing plt.gca().invert_xaxis(), but that inverts the axis labels as well as its corresponding plot. In other words, plt.gca().invert_xaxis() gives me what the graph should look like, but I want to preserve the x tick labels from the 0 to z plot. Can I invert the plot with plt.gca().invert_xaxis() and keep the axis labels the same as before inverting or just invert the labels and not the plot? That also pretty much gives me what the graph would look like for the z to 0 integral.

Thank you!

  • 3
    Mathematically, the integral from z to 0 is just the negative of the integral from 0 to z. Is there any reason you can't swap the two and then negate the result? – Frank Yellin Dec 02 '21 at 00:41
  • https://math.stackexchange.com/questions/232455/is-integration-from-a-to-b-same-or-b-to-a-or-is-negative – Craig Dec 02 '21 at 00:41

0 Answers0