def test_simple_dlqr(): # Example taken from M. de Oliveira's MAE280B lecture notes H = State([[0, 0, 1, 0], [0, 0, 0, 1], [4.03428022844288e-06, 0, 0, 0.0515652322798669], [0, 0, -0.000104315254033883, 0]], [[0, 0], [1e-5/3, 0], [0, 0], [0, 0.01]], eye(4), dt=0.1) k, _, _ = lqr(H[:, 1], eye(4)) H.a = H.a.T f, _, _ = lqr(H[:, 0], block_diag(0, 0, 1e-5, 1e-5), 0.1) assert_almost_equal(k, array([[0, 0, -2.08727337333631e-06, 0]])) assert_almost_equal(f, array([[1.71884123e-11, 0, 0, -1.79301359e-15]]))
def test_simple_dlqr(): # Example taken from M. de Oliveira's MAE280B lecture notes H = State([[0, 0, 1, 0], [0, 0, 0, 1], [4.03428022844288e-06, 0, 0, 0.0515652322798669], [0, 0, -0.000104315254033883, 0]], [[0, 0], [1e-5 / 3, 0], [0, 0], [0, 0.01]], eye(4), dt=0.1) k, _, _ = lqr(H[:, 1], eye(4)) H.a = H.a.T f, _, _ = lqr(H[:, 0], block_diag(0, 0, 1e-5, 1e-5), 0.1) assert_almost_equal(k, array([[0, 0, -2.08727337333631e-06, 0]])) assert_almost_equal(f, array([[1.71884123e-11, 0, 0, -1.79301359e-15]]))
def test_simple_lqr(): # Example taken from M. de Oliveira's MAE280B lecture notes H = State([[0, 0, 1, 0], [0, 0, 0, 1], [4.03428022844288e-06, 0, 0, 0.0515652322798669], [0, 0, -0.000104315254033883, 0]], [[0, 0], [1e-5 / 3, 0], [0, 0], [0, 0.01]], eye(4)) k, _, _ = lqr(H[:, 1], eye(4)) H.a = H.a.T f, _, _ = lqr(H[:, 0], block_diag(0, 0, 1e-5, 1e-5), 0.1) assert_almost_equal(k, array([[1.00554916, -1, 52.52180106, 18.51107167]])) assert_almost_equal( f, array([[-577.370350, 173.600463, 0.383744946, 0.050228534]]), decimal=5)
def test_simple_lqr(): # Example taken from M. de Oliveira's MAE280B lecture notes H = State([[0, 0, 1, 0], [0, 0, 0, 1], [4.03428022844288e-06, 0, 0, 0.0515652322798669], [0, 0, -0.000104315254033883, 0]], [[0, 0], [1e-5/3, 0], [0, 0], [0, 0.01]], eye(4)) k, _, _ = lqr(H[:, 1], eye(4)) H.a = H.a.T f, _, _ = lqr(H[:, 0], block_diag(0, 0, 1e-5, 1e-5), 0.1) assert_almost_equal(k, array([[1.00554916, -1, 52.52180106, 18.51107167]])) assert_almost_equal(f, array([[-577.370350, 173.600463, 0.383744946, 0.050228534]]), decimal=5)
def test_simple_lqry(): # Scalar matrices H = State(1, 1, 1, 1) k, x, e = lqr(H, Q=3, weight_on='output') assert_almost_equal(array([k[0, 0], x[0, 0], e[0]]), [1.5, 3, -0.5 + 0j]) # Wrong S shape assert_raises(ValueError, lqr, H, Q=3, S=eye(2), weight_on='output')
def test_simple_lqry(): # Scalar matrices H = State(1, 1, 1, 1) k, x, e = lqr(H, Q=3, weight_on='output') assert_almost_equal(array([k[0, 0], x[0, 0], e[0]]), [1.5, 3, -0.5+0j]) # Wrong S shape assert_raises(ValueError, lqr, H, Q=3, S=eye(2), weight_on='output')
def test_lqr_arguments(): # First arg is not LTI assert_raises(ValueError, lqr, 1, 1) # Static Gain assert_raises(ValueError, lqr, State(1), 1) # Wrong string assert_raises(ValueError, lqr, Transfer(1, [1, 1]), 1, weight_on='asdf') # scalar matrices H = Transfer(1, [1, 1]) k, x, e = lqr(H, 3) assert_almost_equal(array([k[0, 0], x[0, 0], e[0]]), [1, 1, -2 + 0j])
def test_lqr_arguments(): # First arg is not LTI assert_raises(ValueError, lqr, 1, 1) # Static Gain assert_raises(ValueError, lqr, State(1), 1) # Wrong string assert_raises(ValueError, lqr, Transfer(1, [1, 1]), 1, weight_on='asdf') # scalar matrices H = Transfer(1, [1, 1]) k, x, e = lqr(H, 3) assert_almost_equal(array([k[0, 0], x[0, 0], e[0]]), [1, 1, -2+0j])
def test_feedback_dynamic_static(): M = array([[2, 2, 1, -1], [3, 2, -2, 3], [1, -1, 1, -3], [0, -1, 2, 0]]) a, b, c, d = matrix_slice(M, [3, 3]) G = State(a, b, eye(3)) H, _, _ = lqr(G, eye(3)) CL = feedback(G, H) assert_almost_equal(CL.poles, array([-5.42998305, -3.75504185, -1.55400925])) G = State(a, b, c, d) CL = feedback(G, 5+0j) assert_almost_equal(concatenate_state_matrices(CL), array([[2., -3., 11., -1.], [3., 17., -32., 3.], [1., -16., 31., -3.], [0., -1., 2., 0.]])) CL = feedback(5, G) assert_almost_equal(concatenate_state_matrices(CL), array([[2., -3., 11., -5.], [3., 17., -32., 15.], [1., -16., 31., -15.], [0., 5., -10., 5.]]))