State feedback u = -K x drives the state to zero: a lift controlled like that always returns to height 0. To send it to a height r the controller adds a feedforward term:
u[k] = -K x[k] + N * r
The closed loop is then x[k+1] = (A - B K) x[k] + B N r. If it is stable it settles where the state stops changing, x = (A - B K) x + B N r, that is
x_ss = (I - A + B K)^-1 B N r and the output y_ss = C x_ss = g * N * r
where g = C (I - A + B K)^-1 B is the loop's steady-state gain from N r to y. Choosing N = 1 / g makes y_ss = r, for every reference. (Instead of inverting the matrix, solve (I - A + B K) w = B for w with numpy.linalg.solve; then g = C w.) If g is 0 the output's final value does not depend on r at all, and no N can help.
Write track(A, B, C, K, r, steps) that returns (N, y): the feedforward gain, and the output C x after steps steps of the loop from x = 0 with the reference r held constant (each step: u = N*r - K x, then x = A x + B u). Return None if |g| < 1e-12.
Examples
Input: A = [[1, 0.5], [0, 1]], B = [0.125, 0.5], C = [1, 0], K = [0.8, 1.2], r = 3, steps = 40
Output: (0.8, 3.0000037224030907)
Explanation: the lift (height, speed) with dt = 0.5 s. At rest at height r the acceleration must be 0,
so N r = K[0] r: N = 0.8. After 20 s the car is 4 micrometres from the third floor.
Input: A = [[0.9]], B = [0.1], C = [1], K = [2], r = 50, steps = 10
Output: (2.9999999999999996, 48.587623754999996)
Explanation: a hot plate (0.9 of the heat stays each step). g = 0.1 / (1 - 0.9 + 0.2) = 1/3, so N = 3.
Input: A = [[1, 0.5], [0, 1]], B = [0.125, 0.5], C = [0, 1], K = [0.8, 1.2], r = 3, steps = 40
Output: None
Explanation: with this sensor the output is the speed, which always settles at 0.
Constraints
1 <= n <= 6; one input and one output;0 <= steps <= 500- in every test with
|g| >= 1e-12the loopA - B Kis stable andI - A + B Kcan be inverted - answers are compared with a tolerance of
1e-6; a list is accepted in place of the tuple
Goals
- Add a reference to a state-feedback loop with u = -K x + N r
- Find the steady state of the closed loop by solving a linear system
- Choose the feedforward gain N so the output settles exactly on the reference