Integration
Integration
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the
content is also available at Berkeley Python Numerical Methods. Print to PDF
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released
under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< 20.5 Summary and Problems | Contents | 21.1 Numerical Integration Problem Statement >
CHAPTER OUTLINE
21.1 Numerical Integration Problem Statement
21.2 Riemann’s Integral
21.3 Trapezoid Rule
21.4 Simpson’s Rule
21.5 Computing Integrals in Python
21.6 Summary and Problems
Motivation
The integral of a function is normally described as the “area under the curve.” In engineering and science, the integral has many applications for
modeling, predicting, and understanding physical systems. However in practice, finding an exact solution for the integral of a function is difficult or
impossible.
This chapter describes several methods of numerically integrating functions. By the end of this chapter, you should understand these methods, how
they are derived, their geometric interpretation, and their accuracy.
© Copyright 2020.
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.00-Numerical-Integration.html 1/1
3/7/25, 10:47 AM Numerical Integration Problem Statement — Python Numerical Methods
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the
content is also available at Berkeley Python Numerical Methods. Print to PDF
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released
under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< CHAPTER 21. Numerical Integration | Contents | 21.2 Riemann’s Integral >
Here, we denote each point in x by xi , where x0 = a and xn = b . Note: There are n + 1 grid points because the count starts at x0 . We also assume
we have a function, f (x), that can be computed for any of the grid points, or that we have been given the function implicitly as f (xi ). The interval
[xi , xi+1 ] is referred to as a subinterval.
b
The following sections give some of the most common methods of approximating ∫a f (x)dx . Each method approximates the area under f (x) for
each subinterval by a shape for which it is easy to compute the exact area, and then sums the area contributions of every subinterval.
< CHAPTER 21. Numerical Integration | Contents | 21.2 Riemann’s Integral >
© Copyright 2020.
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.01-Numerical-Integration-Problem-Statement.html 1/1
3/7/25, 10:47 AM Riemanns Integral — Python Numerical Methods
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the
content is also available at Berkeley Python Numerical Methods. Print to PDF
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released
under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< 21.1 Numerical Integration Problem Statement | Contents | 21.3 Trapezoid Rule >
Riemanns Integral
The simplest method for approximating integrals is by summing the area of rectangles that are defined for each subinterval. The width of the
rectangle is xi+1 − xi = h , and the height is defined by a function value f (x) for some x in the subinterval. An obvious choice for the height is the
function value at the left endpoint, xi , or the right endpoint, xi+1 , because these values can be used even if the function itself is not known. This
method gives the Riemann Integral approximation, which is
b n−1
∫ f (x)dx ≈ ∑ hf (xi ),
a i=0
or
b n
∫ f (x)dx ≈ ∑ hf (xi ),
a i=1
As with numerical differentiation, we want to characterize how the accuracy improves as h gets small. To determine this characterizing, we first
rewrite the integral of f (x) over an arbitrary subinterval in terms of the Taylor series. The Taylor series of f (x) around a = xi is
′
f (x) = f (xi ) + f (xi )(x − xi ) + ⋯
Thus
xi+1 xi+1
′
∫ f (x)dx = ∫ (f (xi ) + f (xi )(x − xi ) + ⋯) dx
xi xi
by substitution of the Taylor series for the function. Since the integral distributes, we can rearrange the right side into the following form:
xi+1 2
h
′ 3
∫ f (x)dx = hf (xi ) + f (xi ) + O(h ),
xi
2
which is just
xi+1
2
∫ f (x)dx = hf (xi ) + O(h ).
xi
Since the hf (xi ) term is our Riemann integral approximation for a single subinterval, the Riemann integral approximation over a single interval is
O(h )
2
.
If we sum the O(h2 ) error over the entire Riemann sum, we get nO(h2 ) . The relationship between n and h is
b − a
h = ,
n
and so our total error becomes over the whole interval. Thus the overall accuracy is O(h) .
b−a 2
O(h ) = O(h)
h
The Midpoint Rule takes the rectangle height of the rectangle at each subinterval to be the function value at the midpoint between xi and xi+1 ,
xi+1 +xi
which for compactness we denote by y i =
2
. The Midpoint Rule says
b n−1
∫ f (x)dx ≈ ∑ hf (y i ).
a i=0
Similarly to the Riemann integral, we take the Taylor series of f (x) around y i , which is
′′ 2
f (y i )(x − y i )
′
f (x) = f (y i ) + f (y i )(x − y i ) + + ⋯
2!
xi+1 xi+1 ′′ 2
f (y i )(x − y i )
′
∫ f (x)dx = ∫ (f (y i ) + f (y i )(x − y i ) + + ⋯) dx,
xi xi
2!
which distributes to
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.02-Riemanns-Integral.html 1/2
3/7/25, 10:47 AM Riemanns Integral — Python Numerical Methods
xi+1
Recognizing that since xi and xi+1 are symmetric around y i , then ∫x ′
f (y i )(x − y i )dx = 0 . This is true for the integral of (x − y i )p for any
i
h
xi+1
odd p . For the integral of (x − y i )p and with p even, it suffices to say that ∫x p
(x − y i ) dx = ∫
2
h
p
x dx , which will result in some multiple of
i −
2
h
p+1
with no lower order powers of h.
Utilizing these facts reduces the expression for the integral of f (x) to
xi+1
3
∫ f (x)dx = hf (y i ) + O(h ).
xi
Since hf (y i ) is the approximation of the integral over the subinterval, the Midpoint Rule is O(h3 ) for one subinterval, and using similar arguments
as for the Riemann Integral, is O(h2 ) over the whole interval. Since the Midpoint Rule requires the same number of calculations as the Riemann
Integral, we essentially get an extra order of accuracy for free! However, if f (xi ) is given in the form of data points, then we will not be able to
compute f (y i ) for this integration scheme.
π
TRY IT! Use the left Riemann Integral, right Riemann Integral, and Midpoint Rule to approximate ∫0 sin(x)dx wtih 11 evenly spaced grid ponts over
the whole interval. Compare this value to the exact value of 2.
import numpy as np
a = 0
b = np.pi
n = 11
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
f = np.sin(x)
I_riemannL = h * sum(f[:n-1])
err_riemannL = 2 - I_riemannL
I_riemannR = h * sum(f[1::])
err_riemannR = 2 - I_riemannR
I_mid = h * sum(np.sin((x[:n-1] \
+ x[1:])/2))
err_mid = 2 - I_mid
print(I_riemannL)
print(err_riemannL)
print(I_riemannR)
print(err_riemannR)
print(I_mid)
print(err_mid)
1.9835235375094546
0.01647646249054535
1.9835235375094546
0.01647646249054535
2.0082484079079745
-0.008248407907974542
< 21.1 Numerical Integration Problem Statement | Contents | 21.3 Trapezoid Rule >
© Copyright 2020.
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.02-Riemanns-Integral.html 2/2
3/7/25, 10:47 AM Trapezoid Rule — Python Numerical Methods
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the
content is also available at Berkeley Python Numerical Methods. Print to PDF
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released
under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
Trapezoid Rule
The Trapezoid Rule fits a trapezoid into each subinterval and sums the areas of the trapezoid to approximate the total integral. This approximation
for the integral to an arbitrary function is shown in the following figure. For each subinterval, the Trapezoid Rule computes the area of a trapezoid
f (xi )+f (xi+1 )
with corners at (xi , 0), (xi+1 , 0), (xi , f (xi )), and (xi+1 , f (xi+1 )), which is h 2
. Thus, the Trapezoid Rule approximates integrals
according to the expression
b n−1
f (xi ) + f (xi+1 )
∫ f (x)dx ≈ ∑ h .
a
2
i=0
TRY IT! You may notice that the Trapezoid Rule “double-counts” most of the terms in the series. To illustrate this fact, consider the expansion of the
Trapezoid Rule:
n−1
f (xi ) + f (xi+1 ) h
∑h = [(f (x0 ) + f (x1 )) + (f (x1 ) + f (x2 )) + (f (x2 )
2 2
i=0
Computationally, this is many extra additions and calls to f (x) than is really necessary. We can be more computationally efficient using the following
expression.
b n−1
h
∫ f (x)dx ≈ (f (x0 ) + 2 (∑ f (xi )) + f (xn )) .
a
2
i=1
xi+1 +xi
To determine the accuracy of the Trapezoid Rule approximation, we first take Taylor series expansion of f (x) around y i =
2
, which is the
midpoint between xi and xi+1 . This Taylor series expansion is
′′ 2
f (y i )(x − y i )
′
f (x) = f (y i ) + f (y i )(x − y i ) + + ⋯
2!
2
and xi+1 − yi =
h
2
, results in the following expressions:
′ 2 ′′
hf (y i ) h f (y i )
f (x i ) = f (y i ) − + − ⋯
2 8
and
′ 2 ′′
hf (y i ) h f (y i )
f (xi+1 ) = f (y i ) + + + ⋯.
2 8
Taking the average of these two expressions results in the new expression,
f (xi+1 ) + f (xi )
2
= f (y i ) + O(h ).
2
f (xi+1 ) + f (xi )
2
f (y i ) = + O(h ).
2
Now returning to the Taylor expansion for f (x), the integral of f (x) over a subinterval is
xi+1 xi+1 ′′ 2
f (y i )(x − y i )
′
∫ f (x)dx = ∫ (f (y i ) + f (y i )(x − y i ) + + ⋯) dx.
xi xi
2!
Now since xi and xi+1 are symmetric around y i , the integrals of the odd powers of (x − y i )p disappear and the even powers resolve to a multiple
h
p+1
.
xi+1
3
∫ f (x)dx = hf (y i ) + O(h ).
xi
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.03-Trapezoid-Rule.html 1/2
3/7/25, 10:47 AM Trapezoid Rule — Python Numerical Methods
Now if we substitute f (y i ) with the expression derived explicitly in terms of f (xi ) and f (xi+1 ), we get
xi+1
f (xi+1 ) + f (xi )
2 3
∫ f (x)dx = h ( + O(h )) + O(h ),
xi
2
which is equivalent to
f (xi+1 ) + f (xi )
2 3
h( ) + hO(h ) + O(h )
2
and therefore,
xi+1
f (xi+1 ) + f (xi )
3
∫ f (x)dx = h ( ) + O(h ).
xi
2
Since h
2
(f (xi+1 ) + f (xi )) is the Trapezoid Rule approximation for the integral over the subinterval, it is O(h3 ) for a single subinterval and O(h2 )
over the whole interval.
π
TRY IT! Use the Trapezoid Rule to approximate ∫0 sin(x)dx with 11 evenly spaced grid points over the whole interval. Compare this value to the
exact value of 2.
import numpy as np
a = 0
b = np.pi
n = 11
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
f = np.sin(x)
I_trap = (h/2)*(f[0] + \
2 * sum(f[1:n-1]) + f[n-1])
err_trap = 2 - I_trap
print(I_trap)
print(err_trap)
1.9835235375094546
0.01647646249054535
© Copyright 2020.
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.03-Trapezoid-Rule.html 2/2
3/7/25, 10:48 AM Simpson’s Rule — Python Numerical Methods
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the
content is also available at Berkeley Python Numerical Methods. Print to PDF
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released
under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< 21.3 Trapezoid Rule | Contents | 21.5 Computing Integrals in Python >
Simpson’s Rule
Consider two consecutive subintervals, [xi−1 , xi ] and [xi , xi+1 ]. Simpson’s Rule approximates the area under f (x) over these two subintervals by
fitting a quadratic polynomial through the points (xi−1 , f (xi−1 )), (xi , f (xi )), and (xi+1 , f (xi+1 )), which is a unique polynomial, and then
integrating the quadratic exactly. The following shows this integral approximation for an arbitrary function.
First, we construct the quadratic polynomial approximation of the function over the two subintervals. The easiest way to do this is to use Lagrange
polynomials, which was discussed in Chapter 17. By applying the formula for constructing Lagrange polynomials we get the polynomial
(x − xi−1 )(x − xi )
+f (xi+1 ) ,
(xi+1 − xi−1 )(xi+1 − xi )
f (xi−1 ) f (x i ) f (xi+1 )
Pi (x) = (x − xi )(x − xi+1 ) − (x − xi−1 )(x − xi+1 ) + (x − xi−1 )(x − xi ).
2 2 2
2h h 2h
You can confirm that the polynomial intersects the desired points. With some algebra and manipulation, the integral of Pi (x) over the two
subintervals is
xi+1
h
∫ Pi (x)dx = (f (xi−1 ) + 4f (xi ) + f (xi+1 ).
xi−1
3
To approximate the integral over (a, b), we must sum the integrals of Pi (x) over every two subintervals since Pi (x) spans two subintervals.
Substituting h
3
(f (xi−1 ) + 4f (xi ) + f (xi+1 )) for the integral of Pi (x) and regrouping the terms for efficiency leads to the formula
b n−1 n−2
h
∫ f (x)dx ≈ [f (x0 ) + 4 ( ∑ f (xi )) + 2 ( ∑ f (xi )) + f (xn )] .
a
3
i=1,i odd i=2,i even
WARNING! Note that to use Simpson’s Rule, you must have an even number of intervals and, therefore, an odd number of grid points.
To compute the accuracy of the Simpson’s Rule, we take the Taylor series approximation of f (x) around xi , which is
′′ 2 ′′′ 3 ′′′′ 4
f (xi )(x − xi ) f (xi )(x − xi ) f (xi )(x − xi )
′
f (x) = f (xi ) + f (xi )(x − xi ) + + + + ⋯
2! 3! 4!
Computing the Taylor series at xi−1 and xi+1 and substituting for h where appropriate gives the expressions
2 ′′ 3 ′′′ 4 ′′′′
h f (x i ) h f (x i ) h f (x i )
′
f (xi−1 ) = f (xi ) − hf (xi ) + − + − ⋯
2! 3! 4!
and
′′ 3 ′′′ 4 ′′′′
h (x i ) h f (x i ) h f (x i )
′
f (xi+1 ) = f (xi ) + hf (xi ) + + + + ⋯
2! 3! 4!
2 4
f (xi−1 ) + 4f (xi ) + f (xi+1 ) h h
′′ ′′′′
= f (x i ) + f (x i ) + f (x i ) + ⋯
6 6 72
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.04-Simpsons-Rule.html 1/2
3/7/25, 10:48 AM Simpson’s Rule — Python Numerical Methods
2
f (xi−1 ) + 4f (xi ) + f (xi+1 ) h
′′ 4
f (x i ) = − f (xi ) + O(h ).
6 6
By substitution of the Taylor series for f (x), the integral of f (x) over two subintervals is then
xi+1 xi+1 ′′ 2
f (xi )(x − xi )
′
∫ f (x)dx = ∫ (f (xi ) + f (xi )(x − xi ) +
xi−1 xi−1
2!
′′′ 3 ′′′′ 4
f (xi )(x − xi ) f (xi )(x − xi )
+ + + ⋯) dx.
3! 4!
Again, we distribute the integral and without showing it, we drop the integrals of terms with odd powers because they are zero.
We then carry out the integrations. As will soon be clear, it benefits us to compute the integral of the second term exactly. The resulting equation is
xi+1 3
h
′′ 5
∫ f (x)dx = 2hf (xi ) + f (xi ) + O(h ).
xi−1
3
Substituting the expression for f (xi ) derived earlier, the right-hand side becomes
2 3
f (xi−1 ) + 4f (xi ) + f (xi+1 ) h h
′′ 4 ′′ 5
2h ( − f (xi ) + O(h )) + f (xi ) + O(h ),
6 6 3
3 3
h h h
′′ 5 ′′ 5
[ (f (xi−1 ) + 4f (xi ) + f (xi+1 )) − f (xi ) + O(h )] + f (xi ) + O(h ).
3 3 3
Canceling and combining the appropriate terms results in the integral expression
xi+1
h
5
∫ f (x)dx = (f (xi−1 ) + 4f (xi ) + f (xi+1 )) + O(h ).
xi−1
3
Recognizing that h
3
(f (xi−1 ) + 4f (xi ) + f (xi+1 )) is exactly the Simpson’s Rule approximation for the integral over this subinterval, this equation
implies that Simpson’s Rule is O(h 5
) over a subinterval and O(h4 ) over the whole interval. Because the h3 terms cancel out exactly, Simpson’s Rule
gains another two orders of accuracy!
π
TRY IT! Use Simpson’s Rule to approximate ∫0 sin(x)dx with 11 evenly spaced grid points over the whole interval. Compare this value to the exact
value of 2.
import numpy as np
a = 0
b = np.pi
n = 11
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
f = np.sin(x)
print(I_simp)
print(err_simp)
2.0001095173150043
-0.00010951731500430384
< 21.3 Trapezoid Rule | Contents | 21.5 Computing Integrals in Python >
© Copyright 2020.
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.04-Simpsons-Rule.html 2/2
3/7/25, 10:48 AM Computing Integrals in Python — Python Numerical Methods
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the
content is also available at Berkeley Python Numerical Methods. Print to PDF
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released
under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< 21.4 Simpson’s Rule | Contents | 21.6 Summary and Problems >
π
TRY IT! Use the trapz function to approximate ∫0 sin(x)dx for 11 equally spaced points over the whole interval. Compare this value to the one
computed in the early example using the Trapezoid Rule.
import numpy as np
from scipy.integrate import trapz
a = 0
b = np.pi
n = 11
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
f = np.sin(x)
I_trapz = trapz(f,x)
I_trap = (h/2)*(f[0] + 2 * sum(f[1:n-1]) + f[n-1])
print(I_trapz)
print(I_trap)
1.9835235375094542
1.9835235375094546
X
Sometimes we want to know the approximated cumulative integral. That is, we want to know F (X) = ∫
x0
f (x)dx . For this purpose, it is useful to
use the cumtrapz function cumsum , which takes the same input arguments as trapz .
TRY IT! Use the cumtrapz function to approximate the cumulative integral of f (x) = sin(x) from 0 to π with a discretization step of 0.01. The
exact solution of this integral is F (x) = sin(x) . Plot the results.
%matplotlib inline
plt.style.use('seaborn-poster')
plt.figure(figsize = (10,6))
plt.plot(x, F_exact)
plt.plot(x[1::], F_approx)
plt.grid()
plt.tight_layout()
plt.title('$F(x) = \int_0^{x} sin(y) dy$')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend(['Exact with Offset', 'Approx'])
plt.show()
The quad(f , a, b) function uses a different numerical differentiation scheme to approximate integrals. quad integrates the function defined by the
function object, f , from a to b .
TRY IT! Use the integrate. quad function to compute ∫0 . Compare your answer with the correct answer of 2.
π
sin(x)dx
I_quad, est_err_quad = \
quad(np.sin, 0, np.pi)
print(I_quad)
err_quad = 2 - I_quad
print(est_err_quad, err_quad)
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.05-Computing-Integrals-in-Python.html 1/2
3/7/25, 10:48 AM Computing Integrals in Python — Python Numerical Methods
2.0
2.220446049250313e-14 0.0
< 21.4 Simpson’s Rule | Contents | 21.6 Summary and Problems >
© Copyright 2020.
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.05-Computing-Integrals-in-Python.html 2/2
3/7/25, 10:48 AM Summary — Python Numerical Methods
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the
content is also available at Berkeley Python Numerical Methods. Print to PDF
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released
under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< 21.5 Computing Integrals in Python | Contents | CHAPTER 22. Ordinary Differential Equations (ODEs): Initial-Value Problems >
Summary
1. Explicit integration of functions is often impossible or inconvenient, and numerical approaches must be used instead.
2. The Riemann Integral, Trapezoid Rule, and Simpson’s Rule are common methods of approximating integrals.
3. Each method has an order of accuracy that depends on the approximation of the area below the function.
Problems
1. Write a function my_int_calc(f , f 0, a, b, N , option), where f is a function object, a and b are scalars such that a < b, N is a positive
integer, and option is the string ‘rect’, ‘trap’, or ‘simp’. Let x be an array starting at a, ending at b , containing N evenly spaced elements. The
output argument, I , should be an approximation to the integral of f (x), with initial condition f 0, computed according to the input argument,
option .
2. Write a function my_poly_int(x, y), where x and y are one-dimensional arrays of the same size, and the elements of x are unique and in
ascending order. The function my_poly_int should (1) compute the Lagrange polynomial going through all the points defined by x and y and
(2) return an approximation to the area under the curve defined by x and y , I , defined as the analytic integral of the Lagrange interpolating
polynomial.
3. When will my_poly_int work worse than the trapezoid method?
4. Write a function my_num_calc(f , a, b, n, option) , where the output I is the numerical integral of f , a function object, computed on a grid
of n evenly spaced points starting at a and ending at b . The integration method used should be one of the following strings defined by option:
‘rect’, ‘trap’, ‘simp’. For the rectangle method, the function value should be taken from the right endpoint of the interval. You may assume that n
is odd.
Warning: In the reader, the x subscripts start at x0 not x1 ; take note of this when programming your loops. The odd-even indices will be reversed.
Also the n term given in Simpsons Rule denotes the number of subintervals, not the number of points as specified by the input argument, n .
Test Cases:
1. A previous chapter demonstrated that some functions can be expressed as an infinite sum of polynomials (i.e. Taylor series). Other functions,
particularly periodic functions, can be written as an infinite sum of sine and cosine waves. For these functions,
∞
A0
f (x) = + ∑ An cos (nx) + Bn sin (nx)
2
n=1
It can be shown that the values of An and Bn can be computed using the following formulas:
π
1
An = ∫ f (x) cos (nx) dx
π −π
π
1
Bn = ∫ f (x) sin (nx) dx
π −π
Just like Taylor series, functions can be approximated by truncating the Fourier series at some n = N . Fourier series can be used to approximate
some particularly nasty functions such as the step function, and they form the basis of many engineering applications such as signal processing.
Write a function my_f ourier_coef (f , n), with output [An , Bn ] , where f is an function object that is 2π -periodic. The function
my_f ourier_coef should compute the n -th Fourier coefficients, An and Bn , in the Fourier series for f defined by the two formulas given earlier.
You should use the quad function to perform the integration.
Test Cases:
Use the following plotting function to plot the analytic and approximation of functions using the fourier series.
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.06-Summary-and-Problems.html 1/4
3/7/25, 10:48 AM Summary — Python Numerical Methods
f = lambda x: np.sin(np.exp(x))
N = 2
plot_results(f, N)
N = 2
plot_results(f, N)
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.06-Summary-and-Problems.html 2/4
3/7/25, 10:48 AM Summary — Python Numerical Methods
N = 20
plot_results(f, N)
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.06-Summary-and-Problems.html 3/4
3/7/25, 10:48 AM Summary — Python Numerical Methods
N = 20
plot_results(f, N)
1. For a numerical grid with spacing h, Boole’s Rule for approximating integrals says that
xi+4
3h
∫ f (x)dx ≈ [7f (xi ) + 32f (xi+1 ) + 12f (xi+2 ) + 32f (xi+3 ) + 7f (xi+4 )] .
xi
90
< 21.5 Computing Integrals in Python | Contents | CHAPTER 22. Ordinary Differential Equations (ODEs): Initial-Value Problems >
© Copyright 2020.
https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.06-Summary-and-Problems.html 4/4