# Trapezoid Rule to approximate integral( f(x), x, a, b ) over N subintervals # The user input is between the hashtags. You can run the code by cutting and # pasting into a SageMath Cell. # def f(x): return x**2+x a = 0.0 b = 3.0 N = 12 # h = ( b - a ) / (1.0*N) x = float(a) s = ( f(a) + f(b) ) / 2.0 for i in range(N-1): x = x + h s = s + f(x) print( "Trapezoid rule approximation:", h * s )