Problem 270496 · easy · Level 02 Linear Data Structures

How Many Samples to 63 %?

first-order system · step response · time constant · logarithm

A soldering station switches its iron on at full power at sample 0. Its controller logs the tip's temperature rise y (°C above where it started) once every dt seconds. The tip heats as a first-order discrete system:

y[0] = 0
y[k] = a * y[k-1] + b        for k = 1, 2, 3, ...

a (between 0 and 1) is the share of the rise that the tip keeps from one sample to the next (the rest leaks away), and b is the heat added per sample by full power, in °C. Setting y[k] = y[k-1] shows where it settles: the final value final = b / (1 - a).

The time constant is the time the response takes to cover 1 - e^(-1), about 63.2 %, of the way to its final value. For this system it can be counted, and it can also be computed: the gap to the final value shrinks by a factor a every sample, so the formula

tau = -dt / ln(a)

gives the time constant in seconds (ln is math.log).

Write time_constant(a, b, dt) that returns a tuple of three numbers:

  1. the first sample number k with y[k] >= (1 - math.exp(-1)) * final,
  2. that moment in seconds, k * dt,
  3. the formula value -dt / math.log(a).

Press Run with plot(...) of the first 30 samples to see the curve; the count and the formula agree to within one sample.

Examples

Input:  a = 0.8, b = 0.5, dt = 0.5
Output: (5, 2.5, 2.2407100588622755)
Explanation: final = 0.5 / 0.2 = 2.5 °C and 63.2 % of it is 1.580 °C.
y = 0, 0.5, 0.9, 1.22, 1.476, 1.6808: sample 5 is the first above.

Input:  a = 0.3, b = 2, dt = 1
Output: (1, 1, 0.8305835450825373)
Explanation: a fast tip: y[1] = 2 is already 70 % of the final 2.857 °C.

Constraints

  • answers are compared with a tolerance of 1e-6; do not round

Goals

  • Simulate the step response of a first-order discrete system
  • Measure a time constant as the time to cover 63.2 % of the way
  • Compare the count with the formula for the time constant of a discrete system
Starting Python…