Data Analysis for Engineers/Module 3

Module 3 · Section 6 of 11

Lesson 3.5 - Interpolation

Target: ~9 min read - 20 min hands-on

Overview

Calibration tables and datasheets rarely give you the exact value you need - thermocouple tables, pump curves, valve Cv charts, and material-property-vs-temperature tables all require interpolating between listed points. scipy.interpolate.interp1d handles linear interpolation; kind="cubic" gives a smoother curve when the underlying relationship is known to be non-linear.

Why This Matters (Engineering Context)

Reading a value between tabulated points - correctly, rather than rounding to the nearest listed row - is routine in instrumentation, rotating-equipment selection, and thermal design.

Code-Along

from scipy.interpolate import interp1d
import numpy as np

# [Instrumentation] a thermocouple-style calibration table: mV output vs temperature
temp_C = np.array([0, 100, 200, 300, 400, 500, 600])
mV = np.array([0.000, 4.096, 8.138, 12.209, 16.397, 20.644, 24.905])

# interp1d(x, y) builds a callable. We pass mV as x and temp as y to INVERT the
# table: give it a measured millivolt, get back an estimated temperature.
mv_to_temp_linear = interp1d(mV, temp_C, kind="linear")   # straight lines between points
mv_to_temp_cubic = interp1d(mV, temp_C, kind="cubic")     # smooth cubic spline

measured_mV = np.array([2.5, 6.0, 15.0, 22.0])   # calling on an array queries all at once
print("Measured mV:", measured_mV)
print("Temp (linear):", np.round(mv_to_temp_linear(measured_mV), 1))
print("Temp (cubic): ", np.round(mv_to_temp_cubic(measured_mV), 1))

reading = 10.5
# the interpolator returns a 0-d array; float(...) pulls out a plain number
print(f"\n{reading} mV -> {float(mv_to_temp_linear(reading)):.1f} C (linear)")

Run it: linear and cubic interpolation agree closely for a smoothly varying table like this, with small differences between tabulated points. Confirm your query stays within the table's range - interp1d raises an error by default rather than extrapolating.

Practice Exercises

  1. Query both interpolators at 1.0 mV and compare - which do you trust more near the bottom of the table, and why?
  2. Call mv_to_temp_linear(30) (outside the table range) and observe the error. Then recreate the interpolator with fill_value="extrapolate" and compare.
  3. Build a second interpolation for a pump head-vs-flow curve of your choosing (at least 5 points) and interpolate at 3 query flows.
# Try the practice exercises here

Knowledge Check

  1. Why might you need cubic instead of linear interpolation for some tables?
  2. What happens by default if you query interp1d outside the range of the original table?
  3. Give one engineering example of a table where interpolation is routinely required.
Answer key
  1. When the true relationship is smoothly curved, a cubic spline better approximates values between points
  2. It raises a ValueError (extrapolation must be explicitly enabled)
  3. Thermocouple calibration tables, pump curves, valve Cv charts, steam/refrigerant property tables

28 / 63 sections · Course home · Join the coaching cohort