Problem 371667 · easy · Level 03 Linear Management & Searching

The Gap a P Controller Leaves

proportional control · steady-state error · disturbance · simulation

A farm's water tank is filled by a pump and drains in two ways: an overflow pipe lets out h / tau centimetres of depth per minute (more when the tank is fuller), and the cattle troughs draw a steady draw cm per minute. A proportional controller runs the pump at

u = Kp * (target - h)        (cm per minute; a pump cannot run backwards, so at least 0)

As you saw in Level 1, a P controller settles short of its target: it only pumps while there is an error, so some error must remain to supply the outflow. This time you work that gap out two ways.

By simulation. The tank starts empty, h = 0. Take steps of dt = 0.1 minutes; in each step, in this order:

u = max(0, Kp * (target - h))
h = h + 0.1 * (u - h / tau - draw), then at least 0 (a tank cannot hold less than nothing)

Run round(minutes / 0.1) steps and take the error target - h at the end.

By formula. At the steady state the depth stops changing, so Kp * (target - h) - h / tau - draw = 0. Solve that for the error e = target - h (write h = target - e and collect the terms in e).

Write p_offset(target, tau, draw, Kp, minutes) that returns a tuple (simulated_error, formula_error) in centimetres. After a long enough run the two agree; after a short one the tank is still filling. Collect the depths in a list and press Run with plot(depths) to watch it creep up to its final depth, not to the target.

Examples

Input:  target = 60, tau = 20, draw = 0.5, Kp = 2, minutes = 30
Output: (1.70731707, 1.70731707)
Explanation: the error must supply 60 / 20 = 3 cm/min of overflow at the target
plus 0.5 cm/min for the cattle, so e = (3 + 0.5) / (2 + 1 / 20) = 1.7073 cm.

Input:  target = 60, tau = 20, draw = 0.5, Kp = 2, minutes = 1
Output: (7.58607166, 1.70731707)
Explanation: after one minute the tank is still 7.6 cm short and still rising.

Constraints

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

Goals

  • Simulate a proportional controller on a first-order tank
  • Derive the steady-state error by setting the rate of change to zero
  • Check a formula against a long simulation
Starting Python…