Calculating An Integral Using Simpsons Rule Python
Simpson's Rule is a numerical method for approximating the integral of a function. This guide explains how to implement Simpson's Rule in Python to calculate definite integrals, including the formula, Python code, and practical examples.
What is Simpson's Rule?
Simpson's Rule is a numerical integration technique that approximates the area under a curve by fitting parabolas to segments of the curve. It's more accurate than the trapezoidal rule and provides better results with fewer intervals.
The rule works by dividing the interval into an even number of subintervals (typically 2, 4, 6, etc.) and approximating the area under the curve using parabolas that pass through three consecutive points.
Simpson's Rule Formula
The formula for Simpson's Rule is:
\[ \int_{a}^{b} f(x) \, dx \approx \frac{h}{3} \left[ f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + \cdots + 2f(x_{n-2}) + 4f(x_{n-1}) + f(x_n) \right] \]
Where:
his the width of each subinterval (h = (b - a)/n)nis the number of subintervals (must be even)x_iare the points at which the function is evaluated
Simpson's Rule is particularly useful when the exact integral is difficult or impossible to compute analytically, or when the function is only known at discrete points.
How to Use Simpson's Rule
To use Simpson's Rule effectively:
- Define the function you want to integrate.
- Choose the interval
[a, b]and the number of subintervalsn(must be even). - Calculate the width of each subinterval
h = (b - a)/n. - Evaluate the function at each point
x_i = a + i*hfori = 0ton. - Apply the Simpson's Rule formula to compute the approximate integral.
Note: For better accuracy, use an even number of subintervals. The more subintervals you use, the more accurate the approximation will be, but it will also increase computational time.
Python Implementation
Here's a Python function that implements Simpson's Rule:
def simpsons_rule(f, a, b, n):
"""
Approximate the integral of f from a to b using Simpson's Rule.
Parameters:
f (function): The function to integrate
a (float): Lower bound of integration
b (float): Upper bound of integration
n (int): Number of subintervals (must be even)
Returns:
float: Approximate value of the integral
"""
if n % 2 != 0:
raise ValueError("n must be even")
h = (b - a) / n
integral = f(a) + f(b)
for i in range(1, n):
x = a + i * h
if i % 2 == 0:
integral += 2 * f(x)
else:
integral += 4 * f(x)
integral *= h / 3
return integral
This function takes a function f, integration bounds a and b, and the number of subintervals n as inputs, and returns the approximate integral value.
Example Calculation
Let's calculate the integral of f(x) = x^2 from 0 to 2 using Simpson's Rule with 4 subintervals.
- Define the function:
f(x) = x^2 - Set
a = 0,b = 2, andn = 4 - Calculate
h = (2 - 0)/4 = 0.5 - Evaluate the function at points
x_0 = 0,x_1 = 0.5,x_2 = 1.0,x_3 = 1.5, andx_4 = 2.0 - Apply Simpson's Rule formula:
\[ \int_{0}^{2} x^2 \, dx \approx \frac{0.5}{3} \left[ f(0) + 4f(0.5) + 2f(1.0) + 4f(1.5) + f(2.0) \right] \]
\[ = \frac{0.5}{3} \left[ 0 + 4(0.25) + 2(1) + 4(2.25) + 4 \right] \]
\[ = \frac{0.5}{3} \left[ 1 + 2 + 9 + 4 \right] = \frac{0.5}{3} \times 16 = \frac{8}{3} \approx 2.6667 \]
The exact value of this integral is 8/3 ≈ 2.6667, so our approximation is very close.