Skip to content

Commit 1953399

Browse files
authored
Merge pull request #119 from kayarre/master
Make output of time response commands consistent across step_response, impulse_response, initial_response and update matlab version of the functions to return arguments in the same order as MATLAB.
2 parents fd5df69 + a88d4a0 commit 1953399

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

control/matlab/timeresp.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ def step(sys, T=None, X0=0., input=0, output=None, return_x=False):
4040
yout: array
4141
Response of the system
4242
43-
xout: array (if selected)
44-
Individual response of each x variable
45-
4643
T: array
4744
Time values of the output
4845
46+
xout: array (if selected)
47+
Individual response of each x variable
48+
4949
5050
5151
See Also
@@ -62,11 +62,11 @@ def step(sys, T=None, X0=0., input=0, output=None, return_x=False):
6262
transpose = True, return_x=True)
6363

6464
if return_x:
65-
return yout, xout, T
65+
return yout, T, xout
6666

6767
return yout, T
6868

69-
def impulse(sys, T=None, input=0, output=None):
69+
def impulse(sys, T=None, X0=0., input=0, output=None, return_x=False):
7070
'''
7171
Impulse response of a linear system
7272
@@ -84,6 +84,11 @@ def impulse(sys, T=None, input=0, output=None):
8484
T: array-like object, optional
8585
Time vector (argument is autocomputed if not given)
8686
87+
X0: array-like or number, optional
88+
Initial condition (default = 0)
89+
90+
Numbers are converted to constant arrays with the correct shape.
91+
8792
input: int
8893
Index of the input that will be used in this simulation.
8994
@@ -94,9 +99,13 @@ def impulse(sys, T=None, input=0, output=None):
9499
-------
95100
yout: array
96101
Response of the system
102+
97103
T: array
98104
Time values of the output
99105
106+
xout: array (if selected)
107+
Individual response of each x variable
108+
100109
See Also
101110
--------
102111
lsim, step, initial
@@ -106,11 +115,15 @@ def impulse(sys, T=None, input=0, output=None):
106115
>>> yout, T = impulse(sys, T)
107116
'''
108117
from ..timeresp import impulse_response
109-
T, yout = impulse_response(sys, T, 0, input, output,
110-
transpose = True)
118+
T, yout, xout = impulse_response(sys, T, X0, input, output,
119+
transpose = True, return_x=True)
120+
121+
if return_x:
122+
return yout, T, xout
123+
111124
return yout, T
112125

113-
def initial(sys, T=None, X0=0., input=None, output=None):
126+
def initial(sys, T=None, X0=0., input=None, output=None, return_x=False):
114127
'''
115128
Initial condition response of a linear system
116129
@@ -142,9 +155,13 @@ def initial(sys, T=None, X0=0., input=None, output=None):
142155
-------
143156
yout: array
144157
Response of the system
158+
145159
T: array
146160
Time values of the output
147161
162+
xout: array (if selected)
163+
Individual response of each x variable
164+
148165
See Also
149166
--------
150167
lsim, step, impulse
@@ -155,8 +172,12 @@ def initial(sys, T=None, X0=0., input=None, output=None):
155172
156173
'''
157174
from ..timeresp import initial_response
158-
T, yout = initial_response(sys, T, X0, output=output,
159-
transpose=True)
175+
T, yout, xout = initial_response(sys, T, X0, output=output,
176+
transpose=True, return_x=True)
177+
178+
if return_x:
179+
return yout, T, xout
180+
160181
return yout, T
161182

162183
def lsim(sys, U=0., T=None, X0=0.):

control/tests/matlab_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ def testStep(self):
161161
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
162162
np.testing.assert_array_almost_equal(tout, t)
163163

164+
yout, tout, xout = step(sys, T=t, X0=0, return_x=True)
165+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
166+
np.testing.assert_array_almost_equal(tout, t)
167+
164168
#Test MIMO system, which contains ``siso_ss1`` twice
165169
sys = self.mimo_ss1
166170
y_00, _t = step(sys, T=t, input=0, output=0)
@@ -188,6 +192,20 @@ def testImpulse(self):
188192
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
189193
np.testing.assert_array_almost_equal(tout, t)
190194

195+
# Play with arguments
196+
yout, tout = impulse(sys, T=t, X0=0)
197+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
198+
np.testing.assert_array_almost_equal(tout, t)
199+
200+
X0 = np.array([0, 0]);
201+
yout, tout = impulse(sys, T=t, X0=X0)
202+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
203+
np.testing.assert_array_almost_equal(tout, t)
204+
205+
yout, tout, xout = impulse(sys, T=t, X0=0, return_x=True)
206+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
207+
np.testing.assert_array_almost_equal(tout, t)
208+
191209
#Test MIMO system, which contains ``siso_ss1`` twice
192210
sys = self.mimo_ss1
193211
y_00, _t = impulse(sys, T=t, input=0, output=0)
@@ -206,6 +224,11 @@ def testInitial(self):
206224
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
207225
np.testing.assert_array_almost_equal(tout, t)
208226

227+
# Play with arguments
228+
yout, tout, xout = initial(sys, T=t, X0=x0, return_x=True)
229+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
230+
np.testing.assert_array_almost_equal(tout, t)
231+
209232
#Test MIMO system, which contains ``siso_ss1`` twice
210233
sys = self.mimo_ss1
211234
x0 = np.matrix(".5; 1.; .5; 1.")

control/tests/timeresp_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ def test_impulse_response(self):
8787
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
8888
np.testing.assert_array_almost_equal(tout, t)
8989

90+
# Play with arguments
91+
tout, yout = impulse_response(sys, T=t, X0=0)
92+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
93+
np.testing.assert_array_almost_equal(tout, t)
94+
95+
X0 = np.array([0, 0])
96+
tout, yout = impulse_response(sys, T=t, X0=X0)
97+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
98+
np.testing.assert_array_almost_equal(tout, t)
99+
100+
tout, yout, xout = impulse_response(sys, T=t, X0=0, return_x=True)
101+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
102+
np.testing.assert_array_almost_equal(tout, t)
103+
90104
# Test MIMO system, which contains ``siso_ss1`` twice
91105
sys = self.mimo_ss1
92106
_t, y_00 = impulse_response(sys, T=t, input=0, output=0)
@@ -111,6 +125,11 @@ def test_initial_response(self):
111125
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
112126
np.testing.assert_array_almost_equal(tout, t)
113127

128+
# Play with arguments
129+
tout, yout, xout = initial_response(sys, T=t, X0=x0, return_x=True)
130+
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
131+
np.testing.assert_array_almost_equal(tout, t)
132+
114133
# Test MIMO system, which contains ``siso_ss1`` twice
115134
sys = self.mimo_ss1
116135
x0 = np.matrix(".5; 1.; .5; 1.")

control/timeresp.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def step_response(sys, T=None, X0=0., input=None, output=None,
493493

494494

495495
def initial_response(sys, T=None, X0=0., input=0, output=None,
496-
transpose=False):
496+
transpose=False, return_x=False):
497497
# pylint: disable=W0622
498498
"""Initial condition response of a linear system
499499
@@ -535,6 +535,8 @@ def initial_response(sys, T=None, X0=0., input=0, output=None,
535535
Time values of the output
536536
yout: array
537537
Response of the system
538+
xout: array
539+
Individual response of each x variable
538540
539541
See Also
540542
--------
@@ -553,11 +555,15 @@ def initial_response(sys, T=None, X0=0., input=0, output=None,
553555
U = np.zeros_like(T)
554556

555557
T, yout, _xout = forced_response(sys, T, U, X0, transpose=transpose)
558+
559+
if return_x:
560+
return T, yout, _xout
561+
556562
return T, yout
557563

558564

559565
def impulse_response(sys, T=None, X0=0., input=0, output=None,
560-
transpose=False):
566+
transpose=False, return_x=False):
561567
# pylint: disable=W0622
562568
"""Impulse response of a linear system
563569
@@ -599,6 +605,8 @@ def impulse_response(sys, T=None, X0=0., input=0, output=None,
599605
Time values of the output
600606
yout: array
601607
Response of the system
608+
xout: array
609+
Individual response of each x variable
602610
603611
See Also
604612
--------
@@ -637,4 +645,8 @@ def impulse_response(sys, T=None, X0=0., input=0, output=None,
637645
T, yout, _xout = forced_response(
638646
sys, T, U, new_X0,
639647
transpose=transpose)
648+
649+
if return_x:
650+
return T, yout, _xout
651+
640652
return T, yout

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy