Skip to content

Commit 994fedc

Browse files
committed
fix discrete time X0, U processing
1 parent 7396f76 commit 994fedc

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

control/iosys.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,7 @@ def input_output_response(
18201820
legal_shapes = [(sys.ninputs, n_steps)]
18211821
U = _check_convert_array(U, legal_shapes,
18221822
'Parameter ``U``: ', squeeze=False)
1823+
U = U.reshape(-1, n_steps)
18231824

18241825
# Check to make sure this is not a static function
18251826
nstates = _find_size(sys.nstates, X0)
@@ -1908,7 +1909,7 @@ def ivp_rhs(t, x):
19081909
# Compute the solution
19091910
soln = sp.optimize.OptimizeResult()
19101911
soln.t = T # Store the time vector directly
1911-
x = [float(x0) for x0 in X0] # State vector (store as floats)
1912+
x = X0 # Initilize state
19121913
soln.y = [] # Solution, following scipy convention
19131914
y = [] # System output
19141915
for i in range(len(T)):

control/tests/iosys_test.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,32 @@ def test_discrete(self, tsys):
731731
np.testing.assert_allclose(ios_t, lin_t,atol=0.002,rtol=0.)
732732
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
733733

734+
def test_discrete_iosys(self, tsys):
735+
"""Create a discrete time system from scratch"""
736+
linsys = ct.StateSpace(
737+
[[-1, 1], [0, -2]], [[0], [1]], [[1, 0]], [[0]], True)
738+
739+
# Create nonlinear version of the same system
740+
def nlsys_update(t, x, u, params):
741+
A, B = params['A'], params['B']
742+
return A @ x + B @ u
743+
def nlsys_output(t, x, u, params):
744+
C = params['C']
745+
return C @ x
746+
nlsys = ct.NonlinearIOSystem(
747+
nlsys_update, nlsys_output, inputs=1, outputs=1, states=2, dt=True)
748+
749+
# Set up parameters for simulation
750+
T, U, X0 = tsys.T, tsys.U, tsys.X0
751+
752+
# Simulate and compare to LTI output
753+
ios_t, ios_y = ios.input_output_response(
754+
nlsys, T, U, X0,
755+
params={'A': linsys.A, 'B': linsys.B, 'C': linsys.C})
756+
lin_t, lin_y = ct.forced_response(linsys, T, U, X0)
757+
np.testing.assert_allclose(ios_t, lin_t,atol=0.002,rtol=0.)
758+
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
759+
734760
def test_find_eqpts(self, tsys):
735761
"""Test find_eqpt function"""
736762
# Simple equilibrium point with no inputs
@@ -1526,7 +1552,6 @@ def secord_update(t, x, u, params={}):
15261552
"""Second order system dynamics"""
15271553
omega0 = params.get('omega0', 1.)
15281554
zeta = params.get('zeta', 0.5)
1529-
u = np.array(u, ndmin=1)
15301555
return np.array([
15311556
x[1],
15321557
-2 * zeta * omega0 * x[1] - omega0*omega0 * x[0] + u[0]

0 commit comments

Comments
 (0)