import numpy as np import matplotlib.pyplot as plt from scipy.integrate import quad %matplotlib inline
def f(x) : return x * np.tanh(50.0*(x-0.5)) + 1.0 x = np.linspace(0,1,100) plt.plot(x,f(x));
Some parts change slowly (or change uniformly), and some parts change quickly.
Question, how would you set the interval size $\Delta x$ to get an accurate integral using the trapazoid method?
Question, how do you know where to put the variable widths?
Question, what if you want adaptive trapazoid widths for any function, even ones you don't know yet?
All of these are pretty complicated, there is a simpler way
def adaptTrap(f, a, b, rtol=1E-3): m = (a+b)/2 I1 = (b-a)/2*(f(a)+f(b)) I2 = (m-a)/2*(f(a)+f(m)) + (b-m)/2*(f(m)+f(b)) if np.abs((I1-I2)/I2) < rtol: return I2 else: return adaptTrap(f, a, m) + adaptTrap(f, m, b)