Scroll to Top

Virtual Math Learning Center

Virtual Math Learning Center Texas A&M University Virtual Math Learning Center

Approximating Definite Integrals with Riemann Sums in Python

Author: David Manuel

In this video, it is demonstrated how to use Python to approximate a definite integral using a left endpoint Reimann sum. The presenter shows how to create a list of the left endpoint values, substitute the list of into the function, and then sum the list of the y values times the width of the rectangles.

Transcript 20

Problem: Approximate the integral below using 50 left endpoint rectangles.\[\int_0^2 e^{-x^2}\, dx\]

Python Code: 
from sympy import *
import numpy as np

x=symbols('x')
f=exp(-x**2)
a=0
b=2
n=50
dx=(b-a)/n
xi=np.arange(a,b,dx)
yi=[f.subs(x,i) for i in xi]
Approx=np.sum(yi)*dx
print('The area is approximately', Approx)

Open Python Notebook File

See more videos from this section