I have function f(x) and I want to integrate the function from 0 to some point x in the interval (0,2). I know how to solve the problem in mathematica, however I don’t know how to solve this problem in python.
Mathematica
OM=0.3
OL=0.7
f[x_] := ((1 + x)^3 OM + OL)^(-1/2);
Plot[NIntegrate[f[x], {x, 0, z2}], {z2, 0, 2}]
My code in python:
import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
y = np.linspace(0, 2, 20)
Om=0.3
Ol=1-Om
def H(x):
return (Om*(1+x)**3 +Ol)**(-1./2)
#Integral from a single point in the inverval
print quad(H,0,2)
How can I do exactly the same in python?
Advertisement
Answer
[integrate.quad(H, 0, t)[0] for t in y]
quads inputs are f, a, b, here you do integration from 0 to t for all t in y.