Skip to content

improved default time vector for time response functions that takes zeros into account. #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 17, 2020
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
4 changes: 4 additions & 0 deletions control/statesp.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,10 @@ def dcgain(self):
gain = np.tile(np.nan, (self.outputs, self.inputs))
return np.squeeze(gain)

def is_static_gain(self):
"""True if and only if the system has no dynamics, that is,
if A and B are zero. """
return not np.any(self.A) and not np.any(self.B)

# TODO: add discrete time check
def _convertToStateSpace(sys, **kw):
Expand Down
14 changes: 10 additions & 4 deletions control/tests/sisotool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ def test_sisotool(self):
initial_point_2, 4)

# Check the step response before moving the point
# new array needed because change in compute step response default time
step_response_original = np.array(
[0., 0.0217, 0.1281, 0.3237, 0.5797, 0.8566, 1.116,
1.3261, 1.4659, 1.526])
[0. , 0.0069, 0.0448, 0.124 , 0.2427, 0.3933, 0.5653, 0.7473,
0.928 , 1.0969])
#old: np.array([0., 0.0217, 0.1281, 0.3237, 0.5797, 0.8566, 1.116,
# 1.3261, 1.4659, 1.526])
assert_array_almost_equal(
ax_step.lines[0].get_data()[1][:10], step_response_original, 4)

Expand Down Expand Up @@ -77,9 +80,12 @@ def test_sisotool(self):
bode_mag_moved, 4)

# Check if the step response has changed
# new array needed because change in compute step response default time
step_response_moved = np.array(
[0., 0.0239, 0.161 , 0.4547, 0.8903, 1.407,
1.9121, 2.2989, 2.4686, 2.353])
[0. , 0.0072, 0.0516, 0.1554, 0.3281, 0.5681, 0.8646, 1.1987,
1.5452, 1.875 ])
#old: array([0., 0.0239, 0.161 , 0.4547, 0.8903, 1.407,
# 1.9121, 2.2989, 2.4686, 2.353])
assert_array_almost_equal(
ax_step.lines[0].get_data()[1][:10], step_response_moved, 4)

Expand Down
65 changes: 41 additions & 24 deletions control/tests/timeresp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def setUp(self):
# Create some transfer functions
self.siso_tf1 = TransferFunction([1], [1, 2, 1])
self.siso_tf2 = _convert_to_transfer_function(self.siso_ss1)

# tests for pole cancellation
self.pole_cancellation = TransferFunction([1.067e+05, 5.791e+04],
[10.67, 1.067e+05, 5.791e+04])
self.no_pole_cancellation = TransferFunction([1.881e+06],
[188.1, 1.881e+06])

# Create MIMO system, contains ``siso_ss1`` twice
A = np.matrix("1. -2. 0. 0.;"
Expand Down Expand Up @@ -167,6 +173,14 @@ def test_step_info(self):
2.50,
rtol=rtol)

# confirm that pole-zero cancellation doesn't perturb results
# https://github.com/python-control/python-control/issues/440
step_info_no_cancellation = step_info(self.no_pole_cancellation)
step_info_cancellation = step_info(self.pole_cancellation)
for key in step_info_no_cancellation:
np.testing.assert_allclose(step_info_no_cancellation[key],
step_info_cancellation[key], rtol=1e-4)

def test_impulse_response(self):
# Test SISO system
sys = self.siso_ss1
Expand Down Expand Up @@ -348,33 +362,41 @@ def test_step_robustness(self):
sys2 = TransferFunction(num, den2)

# Compute step response from input 1 to output 1, 2
t1, y1 = step_response(sys1, input=0, T_num=100)
t2, y2 = step_response(sys2, input=0, T_num=100)
t1, y1 = step_response(sys1, input=0, T=2, T_num=100)
t2, y2 = step_response(sys2, input=0, T=2, T_num=100)
np.testing.assert_array_almost_equal(y1, y2)

def test_auto_generated_time_vector(self):
# confirm a TF with a pole at p simulates for 7.0/p seconds
# confirm a TF with a pole at p simulates for ratio/p seconds
p = 0.5
ratio = 9.21034*p # taken from code
ratio2 = 25*p
np.testing.assert_array_almost_equal(
_ideal_tfinal_and_dt(TransferFunction(1, [1, .5]))[0],
(7/p))
(ratio/p))
np.testing.assert_array_almost_equal(
_ideal_tfinal_and_dt(TransferFunction(1, [1, .5]).sample(.1))[0],
(7/p))
# confirm a TF with poles at 0 and p simulates for 7.0/p seconds
(ratio2/p))
# confirm a TF with poles at 0 and p simulates for ratio/p seconds
np.testing.assert_array_almost_equal(
_ideal_tfinal_and_dt(TransferFunction(1, [1, .5, 0]))[0],
(7/p))
(ratio2/p))

# confirm a TF with a natural frequency of wn rad/s gets a
# dt of 1/(7.0*wn)
# dt of 1/(ratio*wn)
wn = 10
ratio_dt = 1/(0.025133 * ratio * wn)
np.testing.assert_array_almost_equal(
_ideal_tfinal_and_dt(TransferFunction(1, [1, 0, wn**2]))[1],
1/(7.0*wn))
1/(ratio_dt*ratio*wn))
wn = 100
np.testing.assert_array_almost_equal(
_ideal_tfinal_and_dt(TransferFunction(1, [1, 0, wn**2]))[1],
1/(ratio_dt*ratio*wn))
zeta = .1
np.testing.assert_array_almost_equal(
_ideal_tfinal_and_dt(TransferFunction(1, [1, 2*zeta*wn, wn**2]))[1],
1/(7.0*wn))
1/(ratio_dt*ratio*wn))
# but a smapled one keeps its dt
np.testing.assert_array_almost_equal(
_ideal_tfinal_and_dt(TransferFunction(1, [1, 2*zeta*wn, wn**2]).sample(.1))[1],
Expand All @@ -384,37 +406,32 @@ def test_auto_generated_time_vector(self):
.1)
np.testing.assert_array_almost_equal(
_ideal_tfinal_and_dt(TransferFunction(1, [1, 2*zeta*wn, wn**2]))[1],
1/(7.0*wn))
1/(ratio_dt*ratio*wn))


# TF with fast oscillations simulates only 5000 time steps even with long tfinal
self.assertEqual(5000,
len(_default_time_vector(TransferFunction(1, [1, 0, wn**2]),tfinal=100)))
# and simulates for 7.0/dt time steps
self.assertEqual(
len(_default_time_vector(TransferFunction(1, [1, 0, wn**2]))),
int(7.0/(1/(7.0*wn))))

sys = TransferFunction(1, [1, .5, 0])
sysdt = TransferFunction(1, [1, .5, 0], .1)
# test impose number of time steps
self.assertEqual(10, len(step_response(sys, T_num=10)[0]))
self.assertEqual(10, len(step_response(sysdt, T_num=10)[0]))
# test that discrete ignores T_num
self.assertNotEqual(15, len(step_response(sysdt, T_num=15)[0]))
# test impose final time
np.testing.assert_array_almost_equal(
100,
step_response(sys, 100)[0][-1],
decimal=.5)
np.ceil(step_response(sys, 100)[0][-1]))
np.testing.assert_array_almost_equal(
100,
step_response(sysdt, 100)[0][-1],
decimal=.5)
np.ceil(step_response(sysdt, 100)[0][-1]))
np.testing.assert_array_almost_equal(
100,
impulse_response(sys, 100)[0][-1],
decimal=.5)
np.ceil(impulse_response(sys, 100)[0][-1]))
np.testing.assert_array_almost_equal(
100,
initial_response(sys, 100)[0][-1],
decimal=.5)
np.ceil(initial_response(sys, 100)[0][-1]))


def test_time_vector(self):
Expand Down
Loading