\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% 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{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\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}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\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 % % 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} \]

Oct 24, 2024 | 759 words | 8 min read

7.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; 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#

It is common that an engineer must evaluate or perform operations on a variety of functions. One such operation is to find the area under the function – what is known in Calculus as taking an anti-derivative or integral of the function. Many common functions can be integrated to produce another function, leading to relatively simple calculations (e.g., the antiderivative of \(sin(x)\) is \(-cos(x)\)). However, there are occasionally functions that do not integrate into another common function (e.g., the antiderivative of \(sin(x^2) \text{ is } \int_{}^{}sin(x^2)dx\)).

A definite integral, a mathematical concept taught in Calculus classes, gives the area under some function between two x-values. To evaluate this integral, we can apply the Fundamental Theorem of Calculus. This theorem says:

(7.3)#\[\int_{a}^{b}f(x)dx = F(b)-F(a)\]

where \(F(x)\) is the antiderivative of \(f(x)\). Note that for this task, while the concept of integrals is being used, no prior knowledge of Calculus and integrals is required or necessary to complete it.

For an example of a simple function, \(sin(x)\), which has antiderivative \(-cos(x)\), this is a straightforward task:

(7.4)#\[\int_{0}^{\Pi}sin(x)dx = -cos(\Pi)-(-cos(0)) = 1 + 1 = 2\]

However, when the function is difficult to integrate, a numerical method can be more convenient. Maclaurin series, like those you studied in Task 3 and their more generalized counterpart, Taylor series, can be used to estimate a function using a polynomial, which will always integrate easily.

The Maclaurin Series for \(\frac{sin(x)}{x}\) is:

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

The antiderivative of this series can now be found by integrating this sum, and since integrating polynomials is relatively straightforward, the integral of our original function can then be numerically calculated as:

(7.6)#\[\int_{a}^{b}\frac{sin(x)}{x}dx = \Big[\sum_{n=0}^{\infty}(-1)^n\frac{x^{2n+1}}{(2n+1)\times (2n+1)!}\Big] \Big|_a^b\]

Where the vertical line after the summation tells you the limits when applying the fundamental theorem of calculus. That is, you plug b in for x and evaluate, then plug a in for x and evaluate, then subtract the second from the first.

Because the series above is an alternating series (i.e., its terms approach 0 and switch between being positive and negative), the series will converge to a value. If you only care about the sum being accurate to a given decimal place, you can stop the sum once you’ve reached stability to that level of rounding (i.e., the sum no longer changes at that level of rounding).

Task Instructions#

Write a program using the ENGR133_Python_Template.py that uses a Maclaurin series for \(\frac{sin(x)}{x}\) to compute the integral over a specified interval from \([a,b]\). You will specify the decimal place to which you would like the sum to converge. However, because more terms are needed to converge to more decimal places, you will set an upper limit for the maximum number of terms that will be calculated. You will ask the user to input (in this order, use the input() function):

  1. The lower limit of integration, a.

  2. The upper limit of integration, b.

  3. The number of decimal places for convergence.

  4. The maximum number of terms to calculate.

Your program will need to handle situations when the user enters an unacceptable value for some of the inputs:

a. The number of decimal places for convergence and the maximum number of terms must both be positive integers only. If non-positive integers are entered, end the program and print an appropriate error message to the screen as “Error: Input a positive integer”.

Your program should output the estimated value for the integral after each term is added. The program should stop iterating (i.e., adding new terms) once the rounded value of integral does not change after three consecutive iterations. If the program reaches the maximum number of terms specified before converging, then the program should output an appropriate error message to the user.

Develop a flow diagram to represent the program described above. Include the checks on the input values as a subroutine and show its logic in a separate flowchart. Save your file in a PDF called: py3_ind_username.pdf.

Translate your flow diagrams into Python program.

Save your file as: py3_ind_2_username.py.

Sample Output#

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

Table 7.10 Test Cases#

Case

\(a\)

\(b\)

decimal places

maximum terms

1

-1

4

3

30

2

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.0 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: 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.55 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.3283 n = 9: sum = -0.32832 Error: The approximation did not converge to 5 decimal places with only 10 terms.