Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions control/matlab/timeresp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def step(sys, T=None, X0=0., input=0, output=None, return_x=False):
yout: array
Response of the system

xout: array (if selected)
Individual response of each x variable

T: array
Time values of the output

xout: array (if selected)
Individual response of each x variable



See Also
Expand All @@ -62,11 +62,11 @@ def step(sys, T=None, X0=0., input=0, output=None, return_x=False):
transpose = True, return_x=True)

if return_x:
return yout, xout, T
return yout, T, xout

return yout, T

def impulse(sys, T=None, input=0, output=None):
def impulse(sys, T=None, X0=0., input=0, output=None, return_x=False):
'''
Impulse response of a linear system

Expand All @@ -84,6 +84,11 @@ def impulse(sys, T=None, input=0, output=None):
T: array-like object, optional
Time vector (argument is autocomputed if not given)

X0: array-like or number, optional
Initial condition (default = 0)

Numbers are converted to constant arrays with the correct shape.

input: int
Index of the input that will be used in this simulation.

Expand All @@ -94,9 +99,13 @@ def impulse(sys, T=None, input=0, output=None):
-------
yout: array
Response of the system

T: array
Time values of the output

xout: array (if selected)
Individual response of each x variable

See Also
--------
lsim, step, initial
Expand All @@ -106,11 +115,15 @@ def impulse(sys, T=None, input=0, output=None):
>>> yout, T = impulse(sys, T)
'''
from ..timeresp import impulse_response
T, yout = impulse_response(sys, T, 0, input, output,
transpose = True)
T, yout, xout = impulse_response(sys, T, X0, input, output,
transpose = True, return_x=True)

if return_x:
return yout, T, xout

return yout, T

def initial(sys, T=None, X0=0., input=None, output=None):
def initial(sys, T=None, X0=0., input=None, output=None, return_x=False):
'''
Initial condition response of a linear system

Expand Down Expand Up @@ -142,9 +155,13 @@ def initial(sys, T=None, X0=0., input=None, output=None):
-------
yout: array
Response of the system

T: array
Time values of the output

xout: array (if selected)
Individual response of each x variable

See Also
--------
lsim, step, impulse
Expand All @@ -155,8 +172,12 @@ def initial(sys, T=None, X0=0., input=None, output=None):

'''
from ..timeresp import initial_response
T, yout = initial_response(sys, T, X0, output=output,
transpose=True)
T, yout, xout = initial_response(sys, T, X0, output=output,
transpose=True, return_x=True)

if return_x:
return yout, T, xout

return yout, T

def lsim(sys, U=0., T=None, X0=0.):
Expand Down
23 changes: 23 additions & 0 deletions control/tests/matlab_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ def testStep(self):
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

yout, tout, xout = step(sys, T=t, X0=0, return_x=True)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

#Test MIMO system, which contains ``siso_ss1`` twice
sys = self.mimo_ss1
y_00, _t = step(sys, T=t, input=0, output=0)
Expand Down Expand Up @@ -188,6 +192,20 @@ def testImpulse(self):
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

# Play with arguments
yout, tout = impulse(sys, T=t, X0=0)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

X0 = np.array([0, 0]);
yout, tout = impulse(sys, T=t, X0=X0)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

yout, tout, xout = impulse(sys, T=t, X0=0, return_x=True)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

#Test MIMO system, which contains ``siso_ss1`` twice
sys = self.mimo_ss1
y_00, _t = impulse(sys, T=t, input=0, output=0)
Expand All @@ -206,6 +224,11 @@ def testInitial(self):
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

# Play with arguments
yout, tout, xout = initial(sys, T=t, X0=x0, return_x=True)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

#Test MIMO system, which contains ``siso_ss1`` twice
sys = self.mimo_ss1
x0 = np.matrix(".5; 1.; .5; 1.")
Expand Down
19 changes: 19 additions & 0 deletions control/tests/timeresp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ def test_impulse_response(self):
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

# Play with arguments
tout, yout = impulse_response(sys, T=t, X0=0)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

X0 = np.array([0, 0])
tout, yout = impulse_response(sys, T=t, X0=X0)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

tout, yout, xout = impulse_response(sys, T=t, X0=0, return_x=True)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

# Test MIMO system, which contains ``siso_ss1`` twice
sys = self.mimo_ss1
_t, y_00 = impulse_response(sys, T=t, input=0, output=0)
Expand All @@ -111,6 +125,11 @@ def test_initial_response(self):
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

# Play with arguments
tout, yout, xout = initial_response(sys, T=t, X0=x0, return_x=True)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

# Test MIMO system, which contains ``siso_ss1`` twice
sys = self.mimo_ss1
x0 = np.matrix(".5; 1.; .5; 1.")
Expand Down
16 changes: 14 additions & 2 deletions control/timeresp.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def step_response(sys, T=None, X0=0., input=None, output=None,


def initial_response(sys, T=None, X0=0., input=0, output=None,
transpose=False):
transpose=False, return_x=False):
# pylint: disable=W0622
"""Initial condition response of a linear system

Expand Down Expand Up @@ -535,6 +535,8 @@ def initial_response(sys, T=None, X0=0., input=0, output=None,
Time values of the output
yout: array
Response of the system
xout: array
Individual response of each x variable

See Also
--------
Expand All @@ -553,11 +555,15 @@ def initial_response(sys, T=None, X0=0., input=0, output=None,
U = np.zeros_like(T)

T, yout, _xout = forced_response(sys, T, U, X0, transpose=transpose)

if return_x:
return T, yout, _xout

return T, yout


def impulse_response(sys, T=None, X0=0., input=0, output=None,
transpose=False):
transpose=False, return_x=False):
# pylint: disable=W0622
"""Impulse response of a linear system

Expand Down Expand Up @@ -599,6 +605,8 @@ def impulse_response(sys, T=None, X0=0., input=0, output=None,
Time values of the output
yout: array
Response of the system
xout: array
Individual response of each x variable

See Also
--------
Expand Down Expand Up @@ -637,4 +645,8 @@ def impulse_response(sys, T=None, X0=0., input=0, output=None,
T, yout, _xout = forced_response(
sys, T, U, new_X0,
transpose=transpose)

if return_x:
return T, yout, _xout

return T, yout