Real-World Problem in Numerical Analysis:Approximating an Integral Using the Trapezoidal Rule

Problem Statement

Consider a company analyzing its daily sales, modeled as a continuous function $ S(t) $, where $ S(t) $ represents the sales rate (in dollars per hour) at time $( t )$ (in hours) during a 10-hour workday.

The sales rate function is given by:

$$
S(t) = 200 + 50 \sin\left(\frac{\pi t}{10}\right)
$$

The company wants to compute the total sales revenue over the 10-hour workday.

Since $ S(t) $ is a continuous function, the total revenue can be calculated as:
$$
\text{Revenue} = \int_0^{10} S(t) , dt
$$

However, instead of solving this integral analytically, we’ll approximate it using the trapezoidal rule.


Solution

The trapezoidal rule approximates the integral:
$$
\int_a^b f(x) , dx \approx \frac{\Delta x}{2} \left[f(x_0) + 2f(x_1) + 2f(x_2) + \dots + 2f(x_{n-1}) + f(x_n)\right]
$$
where $ \Delta x = \frac{b-a}{n} $ is the width of each subinterval, and $( x_0, x_1, \dots, x_n )$ are the points dividing the interval $([a, b])$.


Python Implementation

Here’s the $Python$ code to solve the problem and visualize the results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import matplotlib.pyplot as plt

# Sales rate function
def sales_rate(t):
return 200 + 50 * np.sin(np.pi * t / 10)

# Trapezoidal rule implementation
def trapezoidal_rule(f, a, b, n):
x = np.linspace(a, b, n + 1) # Divide the interval [a, b] into n subintervals
y = f(x) # Compute function values at these points
h = (b - a) / n # Width of each subinterval
integral = (h / 2) * (y[0] + 2 * sum(y[1:-1]) + y[-1])
return integral, x, y

# Parameters
a, b = 0, 10 # Integration limits
n = 10 # Number of subintervals

# Compute the integral and function values
integral, x_points, y_points = trapezoidal_rule(sales_rate, a, b, n)

# Exact integral for comparison (computed analytically)
exact_integral = 200 * (b - a) + (500 / np.pi) * (np.cos(np.pi * a / 10) - np.cos(np.pi * b / 10))

# Visualization
t = np.linspace(a, b, 500) # High-resolution points for the curve
sales = sales_rate(t)

plt.figure(figsize=(10, 6))

# Plot the sales rate curve
plt.plot(t, sales, label="Sales Rate $S(t)$", color="blue")

# Plot trapezoids
for i in range(len(x_points) - 1):
plt.fill_between([x_points[i], x_points[i+1]], [y_points[i], y_points[i+1]], color="skyblue", alpha=0.4)

# Annotate
plt.title("Approximating Total Sales with the Trapezoidal Rule", fontsize=14)
plt.xlabel("Time (hours)", fontsize=12)
plt.ylabel("Sales Rate ($/hour)", fontsize=12)
plt.axhline(0, color="black", linewidth=0.7, linestyle="--", alpha=0.7)
plt.legend(["Sales Rate $S(t)$", "Trapezoidal Approximation"], loc="upper right")
plt.grid(alpha=0.3)

# Show plot
plt.show()

# Print results
print(f"Trapezoidal Approximation of Revenue: ${integral:.2f}")
print(f"Exact Revenue (Analytical): ${exact_integral:.2f}")
print(f"Error: ${abs(integral - exact_integral):.2f}")

Explanation of Code

  1. Sales Rate Function:
    $ S(t) = 200 + 50 \sin\left(\frac{\pi t}{10}\right) $ models the sales rate as a function of time, peaking midway through the day.

  2. Trapezoidal Rule:

    • The interval $([0, 10])$ is divided into $( n )$ subintervals.
    • The rule computes the approximate integral using the formula provided.
    • The trapezoidal_rule function returns the approximate integral and points for visualization.
  3. Comparison with Exact Integral:

    • The exact integral is computed using the analytical solution for verification.
  4. Visualization:

    • The sales rate curve is plotted.
    • Trapezoids used for the approximation are shown as shaded regions.

Results and Visualization

  1. Numerical Result:

    • Trapezoidal Approximation: $$2315.69$
    • Exact Revenue (Analytical): $$2318.31$
    • Error: $$2.62$ (exact match for $( n = 10 )$)
  2. Graph:

    • The sales rate $ S(t) $ is shown as a smooth curve.
    • The trapezoidal approximation overlays the curve with shaded regions representing each subinterval.

The visualization highlights how the trapezoidal rule effectively estimates the total revenue, even with a small number of subintervals.