Calibration search with early rejection and ties

An engineer calibrates a voltage sensor using the correction y^=a+bx\widehat{y}=a+bx, where aa is an offset in volts and bb is dimensionless. A grid search tests combinations drawn from specified lists of parameter values.


The measured voltages are 0, 1, and 2 V, with corresponding reference voltages 1, 3, and 6 V. The code scores each parameter pair using its largest absolute residual. It abandons a pair when that pair is already worse than the best completed result.


Python
measured = [0, 1, 2]
reference = [1, 3, 6]

best_error = float("inf")
best_a = None
best_b = None
calculations = 0

for a in [0, 1, 2]:
    for b in [1, 2, 3]:
        worst = 0
        for j in range(3):
            error = abs(a + b * measured[j] - reference[j])
            calculations += 1
            worst = max(worst, error)
            if worst > best_error:
                break
        if worst < best_error:
            best_error = worst
            best_a = a
            best_b = b


Here, float("inf") is larger than any finite error, and break exits only the innermost loop. Report the final selected offset a in V, selected scale b, stored best error in V, and number of residual calculations. Enter all four answers as exact integers.

Your answers

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

More Applications of Loops Questions: