# How Flight Works 8, Exercise 3: a simple altitude-hold autopilot and its gain # Each second the controller changes the climb rate in proportion to the altitude # error. A low gain is slow, a high gain overshoots. Illustrative model only. TARGET = 10000.0 for gain in (0.05, 0.2, 1.5): alt, climb = 9700.0, 0.0 peak = alt for _ in range(60): climb = gain * (TARGET - alt) * 0.1 + climb * 0.5 alt += climb peak = max(peak, alt) print("gain", gain, "-> after 60 s", round(alt), "m, highest point", round(peak), "m") # Output when run: # gain 0.05 -> after 60 s 9835 m, highest point 9835 m # gain 0.2 -> after 60 s 9976 m, highest point 9976 m # gain 1.5 -> after 60 s 10000 m, highest point 10008 m # # A low gain is safe but slow; a moderate gain settles close to the target; a high # gain reaches it fastest but overshoots slightly. Designers pick a gain that is # quick without being jumpy, which is the autopilot version of smooth flying.