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 | import numpy as np |
Explanation of Code
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.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.
Comparison with Exact Integral:
- The exact integral is computed using the analytical solution for verification.
Visualization:
- The sales rate curve is plotted.
- Trapezoids used for the approximation are shown as shaded regions.
Results and Visualization
Numerical Result:
- Trapezoidal Approximation: $$2315.69$
- Exact Revenue (Analytical): $$2318.31$
- Error: $$2.62$ (exact match for $( n = 10 )$)
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.