Engineering Data Analysis Programs: From Inputs to Results

Engineering Data Analysis Programs turn an engineering calculation into a repeatable sequence of instructions. You might use one to summarize repeated force measurements or calculate a performance measure for several test runs. The central task is to express the calculation clearly: identify the inputs, apply the intended operations, and return results with meaningful units. Here, assume that the measurements supplied to the program have already been checked and are suitable for the calculation.

Specify the calculation before coding

A function contract states what a function receives, what it returns, and the conditions under which its calculation applies. For a function that calculates mean force, the contract might specify a nonempty sequence of equally weighted readings in newtons and a returned mean in newtons.


Write the governing calculation first. For nn equally weighted readings,


S=∑i=1nxi,xˉ=Sn.S=\sum_{i=1}^{n}x_i, \qquad \bar{x}=\frac{S}{n}.


The sum SS and mean xˉ\bar{x} have the same units as each reading. The count nn is dimensionless. Equally weighted readings are an assumption: averaging values without weights may answer a different question when readings represent different durations.


Parameters make choices explicit. If a calculation counts readings at or above a force limit, pass that limit into the function instead of hiding it inside the calculation.


A flow diagram shows measurements and a threshold entering an analysis function and summary values leaving it.


Build a reusable function

A function packages a calculation so that you can call it with different inputs. This Python function implements the mean directly:


Python
def mean_force(forces_N):
    total_N = 0.0
    count = 0
    for force_N in forces_N:
        total_N += force_N
        count += 1
    return total_N / count


An accumulator stores a running result as a loop processes successive values. Here, total_N accumulates force and count accumulates the number of readings. Initialize both before the loop so that each reading contributes to the same calculation.


The return statement sends the computed value back to the calling code. Printing a value displays it, but printing alone does not provide that value for a later calculation. Returning results lets another part of the program calculate a comparison, format a report, or combine outputs.



Place updates and decisions deliberately

A loop repeats work; a conditional decides whether particular work happens. For example, a conditional can increment a selected-reading count when force_N >= limit_N. The total number of readings should still include every reading if the required result is a fraction of the complete run.


Different summaries require different accumulated quantities:


Required result

Accumulated quantities

Final operation

Sum of readings

Sum of all readings

Return the sum

Mean of readings

Sum and total count

Divide sum by total count

Fraction meeting a condition

Selected count and total count

Divide selected count by total count


Indentation determines which statements repeat in Python. A final division normally belongs after all readings have been processed. A return inside the loop exits the function as soon as execution reaches it, which can leave later readings unused.



Check behavior and report clearly

Use small cases with predictable results to check the program. Identical readings should produce that same mean. Reordering equally weighted readings should preserve the mathematical result, although floating-point arithmetic can introduce tiny numerical differences. Repeating a function call with the same inputs should give the same result when the function depends only on those inputs.


Check units alongside the arithmetic. A force mean remains in newtons; a selected fraction has no units. Multiply a fraction by 100100 only when reporting a percentage. Retain full working precision and round the final reported value to the requested precision.


What to remember

  • State inputs, outputs, units, and assumptions before coding.
  • Initialize accumulators where each independent calculation begins.
  • Use loops for repetition and conditionals for selection.
  • Return the completed result and round when reporting it.

Example: Summarizing Repeated Load-Cell Readings

A fixture test produces four equally weighted force readings: 120120, 135135, 145145, and 140 N140\ \text{N}. An engineer needs the mean force and the fraction of readings at or above a specified limit. For this run, the limit is 140 N140\ \text{N}. Assume each input list is nonempty and its readings have already been checked.


Use Python. Retain full precision in calculations, then report mean forces to one decimal place and percentages to one decimal place.


a) Write a function that accepts the readings and limit as arguments and returns the mean force and selected fraction. Keep display formatting outside the function.


Use a running sum for the mean and a separate counter for readings meeting the inclusive limit. Both accumulators start at ________ inside the function, so every call begins a new calculation.


Write the following Python lines in order. The indentation specified for each line determines which statements belong to the function, loop and conditional:

  1. With no indentation, define the function: def summarize_forces(forces_N, limit_N):
  2. Indent four spaces: total_N = 0.0
  3. Indent four spaces: selected = 0
  4. Indent four spaces: for force_N in forces_N:
  5. Indent eight spaces: total_N += force_N
  6. Indent eight spaces: if force_N >= limit_N:
  7. Indent twelve spaces: selected += 1
  8. Return to four spaces, ending the loop: count = len(forces_N)
  9. Indent four spaces: mean_N = total_N / count
  10. Indent four spaces: fraction = selected / count
  11. Indent four spaces: return mean_N, fraction

The sum update occurs for every reading. The selected-count update occurs only when its condition is true. After the loop, dividing by the complete list length gives the equally weighted mean and the fraction of all readings meeting the limit. The returned mean has units of newtons; the returned fraction is dimensionless.


b) Trace the function for the fixture test and calculate both reported results.


Call the function with the supplied readings and the 140 N140\ \text{N} limit using mean_N, fraction = summarize_forces([120, 135, 145, 140], 140).


The running values change as follows. A reading equal to the limit contributes to the selected count because the condition uses >=.

  • After 120 N120\ \text{N}: the running sum is 120 N120\ \text{N} and the selected count is 00.
  • After 135 N135\ \text{N}: the running sum is 255 N255\ \text{N} and the selected count is 00.
  • After 145 N145\ \text{N}: the running sum is 400 N400\ \text{N} and the selected count is 11.
  • After 140 N140\ \text{N}: the running sum is 540 N540\ \text{N} and the selected count is 22.

There are ________ readings, so


Fˉ=540 N4=135.0 N.\bar{F}=\frac{540\ \text{N}}{4}=135.0\ \text{N}.


The selected fraction and percentage are


f=24=0.5,100f=50.0%.f=\frac{2}{4}=0.5, \qquad 100f=50.0\%.


The function returns (135.0, 0.5). Report a mean force of 135.0 N135.0\ \text{N} and 50.0%50.0\% of readings at or above the limit. The function returns a fraction; multiplying by 100100 belongs to percentage reporting.


c) Reuse the function for readings of 100100, 110110, and 120 N120\ \text{N} with a new limit of 115 N115\ \text{N}. Explain why the earlier run does not affect the result.


Pass the new readings and limit as arguments using mean_N, fraction = summarize_forces([100, 110, 120], 115). There is no need to edit the function or manually clear its previous results.


On entering the function, total_N and selected are initialized again. The new running sum is


S=(100+110+120) N=330 N.S=(100+110+120)\ \text{N}=330\ \text{N}.


Only the 120 N120\ \text{N} reading meets the new limit. With ________ readings in this call,


Fˉ=330 N3=110.0 N,f=13=0.33333….\bar{F}=\frac{330\ \text{N}}{3}=110.0\ \text{N}, \qquad f=\frac{1}{3}=0.33333\ldots.


The reported percentage is


100f=33.3333…%≈33.3%.100f=33.3333\ldots\%\approx33.3\%.


Report 110.0 N110.0\ \text{N} and 33.3%33.3\%. The earlier run contributes nothing because the accumulators are local variables initialized during each function call. The function returns the unrounded fraction for any later calculations.

A force-processing program applies a dimensionless gain and then adds an offset of 2.0 N2.0\ \text{N}:


Python
def apply_gain(force_N, gain):
    return force_N * gain

result_N = apply_gain(48.0, 1.25)
result_N += 2.0


What value does result_N contain after both statements execute?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A program calculates the fraction of five force readings that meet or exceed a specified limit. All readings and the limit are in newtons.


Python
def selected_fraction(forces_N, limit_N):
    selected = 0
    for force_N in forces_N:
        if force_N >= limit_N:
            selected += 1
    return selected / len(forces_N)

fraction = selected_fraction([95, 100, 105, 110, 90], 100)


What value is assigned to fraction?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An engineering prediction program has three force residuals, defined as predicted force minus measured force: 3.03.0, −4.0-4.0, and 0.0 N0.0\ \text{N}. The following function calculates their root-mean-square error:


Python
def rms_error(errors_N):
    squared_total = 0.0
    for error_N in errors_N:
        squared_total += error_N ** 2
    return (squared_total / len(errors_N)) ** 0.5

result_N = rms_error([3.0, -4.0, 0.0])


What is result_N? Answer in newtons, to two decimal places, entering only the number.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice