\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} \newcommand\pound{\unit{lbs}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{\kern-4mu}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{\kern-4mu}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Apr 28, 2026 | 583 words | 6 min read

13.3.2. Task 2#

Learning Objectives#

Predict the output of a complete for and while loop in Python; Use loops to conduct repetitive operations to perform numerical operations in Python; Utilize NumPy arrays and vectorized operations to perform efficient numerical calculations; Use loops to create corresponding data sets (i.e. lists, arrays, and dictionaries) in Python; Create, manipulate, and read from arrays, lists, and dictionaries in Python; Manipulate and extract information from lists, arrays, and dictionaries in Python; Use loops to compare and evaluate associated elements in data sets in Python.

Introduction#

Engineers often need to calculate the area under curves (the integral of a function). While simple functions like \(\sin(x)\) have straightforward solutions, many real-world functions like \(\frac{\sin(x)}{x}\) don’t have simple closed-form solutions. For these cases, numerical approximation methods are more practical than trying to solve them analytically.

We can approximate difficult-to-integrate functions using a Maclaurin series, which is a polynomial expansion that’s much easier to work with. The Maclaurin series for \(\frac{\sin(x)}{x}\) is:

(13.3)#\[\frac{\sin(x)}{x} = \sum_{n=0}^{\infty}(-1)^n \frac{x^{2n}}{(2n + 1)!}\]

When we integrate this series term-by-term, we get:

(13.4)#\[\int_{a}^{b}\frac{\sin(x)}{x}\dd{x} = \left. \left[ \sum_{n=0}^{\infty}(-1)^n \frac{x^{2n + 1}}{(2n + 1) (2n+1)!} \right] \right|_a^b\]

Where \(a\) and \(b\) are the limits of integration. Expanding this, we have:

(13.5)#\[\int_{a}^{b}\frac{\sin(x)}{x}\dd{x} = \sum_{n=0}^{\infty}(-1)^n \frac{b^{2n + 1} - a^{2n + 1}}{(2n + 1) (2n+1)!}\]

This alternating series (terms alternate between positive and negative) naturally converges, meaning adding more terms brings you closer to the true answer. Since the series is alternating and terms get smaller, you can achieve a desired accuracy level by adding terms until the rounded sum stops changing.

Note

No prior calculus knowledge is required to complete this task – you’ll simply calculate terms and sum them until reaching your desired precision.

Task Instructions#

Design a program that uses the Maclaurin series for \(\frac{\sin(x)}{x}\) to estimate the integral of this function over an interval \([a,b]\). The program should prompt the user to input the following values in order:

  1. Lower limit of integration (\(a\))

  2. Upper limit of integration (\(b\))

  3. Decimal places for convergence (positive integer)

  4. Maximum number of terms (positive integer)

Inputs should be validated as they are entered. The decimal places and maximum number of terms must be positive integers. If either is non-positive, print an error message as shown in the sample output below and then end the program.

Use a loop to calculate the integral estimate by adding terms from the series one at a time. The contribution of the \(n\)-th term to the total integral is given by:

(13.6)#\[\text{Term}_n = (-1)^n \frac{b^{2n + 1} - a^{2n + 1}}{(2n + 1) (2n+1)!}\]

After adding each term, print the current estimate of the integral. Continue adding terms until the rounded value of the integral stops changing for three consecutive iterations at the specified decimal place. If the maximum number of terms is reached before convergence, print an error message as shown in the sample output below, and end the program.

Develop a flow diagram to express your design. Save your file in a PDF called: py3_ind_2_username.pdf.

Once your flowchart is complete, implement your design in Python Starting from the template file provided: ENGR133_Python_Template.py. Import the NumPy library at the start of your program using: import numpy as np and use its functions for numerical calculations. For example, you should use np.power for calculating the power. Additionally, import the math library using: import math and use math.factorial for calculating the factorial components of the \(n\)-th term.

Hint

The built-in round() function can be helpful when comparing the sum between iterations.

Save your completed program as: py3_ind_2_username.py.

Sample Output#

Use the values in Table 13.13 below to test your program.

Table 13.13 Test Cases#

Case

\(a\)

\(b\)

decimal places

maximum terms

1

-1

4

3

30

2

-1

4

3

-30

3

2.75

5.3

5

10

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

$ python3 py3_ind_2_username.py Enter the lower limit of integration: -1 Enter the upper limit of integration: 4 Enter the number of decimal places for convergence: 3 Enter the maximum number of terms: 30

Approximations: n = 0: sum = 5.000 n = 1: sum = 1.389 n = 2: sum = 3.097 n = 3: sum = 2.633 n = 4: sum = 2.713 n = 5: sum = 2.704 n = 6: sum = 2.704 n = 7: sum = 2.704 The integral from -1.0 to 4.0 is estimated to be 2.704. Total number of terms: 8

Case 2 Sample Output

$ python3 py3_ind_2_username.py Enter the lower limit of integration: -1 Enter the upper limit of integration: 4 Enter the number of decimal places for convergence: 3 Enter the maximum number of terms: -30 Error: Input a positive integer

Case 3 Sample Output

$ python3 py3_ind_2_username.py Enter the lower limit of integration: 2.75 Enter the upper limit of integration: 5.3 Enter the number of decimal places for convergence: 5 Enter the maximum number of terms: 10

Approximations: n = 0: sum = 2.55000 n = 1: sum = -4.56556 n = 2: sum = 2.14224 n = 3: sum = -1.15373 n = 4: sum = -0.14612 n = 5: sum = -0.35707 n = 6: sum = -0.32491 n = 7: sum = -0.32864 n = 8: sum = -0.32830 n = 9: sum = -0.32832 Error: The approximation did not converge to 5 decimal places with only 10 terms.

Table 13.14 Deliverables#

Deliverables

Description

py3_ind_2_username.pdf

Flowchart(s) for this task.

py3_ind_2_username.py

Your completed Python code.