def bulStoer(F, x, y, xStop, H, tol=1.0e-6): X = [] Y = [] X.append(x) Y.append(y) while x < xStop: H = min(H, xStop - x) y = midpoint.integrate(F, x, y, x + H, tol) # Midpoint method x = x + H X.append(x) Y.append(y) return numpy.array(X), numpy.array(Y)
#!/usr/bin/env python3 import numpy as np import midpoint def intf(x): return np.exp(x) rangex = np.linspace(0, 10, 100, endpoint=True) out = "{} integration, e^x on ({},{}): {}" print(out.format( "midpoint", rangex[0], rangex[-1], midpoint.integrate(intf, rangex) )) from scipy.integrate import quad print(out.format( "scipy", rangex[0], rangex[-1], quad(intf, rangex[0], rangex[-1]) ))
import numpy as np import midpoint import trapezoid import time def intf(x): return np.exp(x) rangex = np.linspace(0, 10, 100, endpoint=True) out = "{} integration, e^x on ({},{}): {}\nelapsed: {}" start = time.clock() res = midpoint.integrate(intf, rangex) elapsed = time.clock() - start print(out.format("midpoint", rangex[0], rangex[-1], res, elapsed)) start = time.clock() res = trapezoid.integrate(intf, rangex) elapsed = time.clock() - start print(out.format("trapezoid", rangex[0], rangex[-1], res, elapsed)) from scipy.integrate import quad start = time.clock() res = quad(intf, rangex[0], rangex[-1]) elapsed = time.clock() - start