From 10167f9672f59e74c7891a082c7e4de0cff0fb6d Mon Sep 17 00:00:00 2001 From: 72004 Date: Tue, 20 Oct 2026 20:23:21 +0500 Subject: [PATCH] Don't fail in step_info() if the 90% rise level is never reached If the response never reaches the rise time limits (e.g. the default time vector ends too early for a system with a large feedthrough transient), np.nonzero(...)[0][0] raised IndexError. Leave RiseTime, SettlingMin and SettlingMax as NaN in that case. --- control/tests/timeresp_test.py | 19 +++++++++++++++++++ control/timeresp.py | 24 ++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/control/tests/timeresp_test.py b/control/tests/timeresp_test.py index ba76c2b91..9f3ed8aea 100644 --- a/control/tests/timeresp_test.py +++ b/control/tests/timeresp_test.py @@ -1483,3 +1483,22 @@ def test_timeresp_aliases(): with pytest.raises(TypeError, match="unrecognized keyword"): ct.step_info(sys, timepts, unknown=True) + + +def test_step_info_rise_not_reached(): + # Response that never reaches 90% of the final value: the rise time + # (and settling min/max) are undefined, but step_info should not fail + timepts = np.linspace(0, 1, 101) + yout = 1 - np.exp(-timepts) + step_info = ct.step_info(yout, timepts, yfinal=1) + assert np.isnan(step_info['RiseTime']) + assert np.isnan(step_info['SettlingMin']) + assert np.isnan(step_info['SettlingMax']) + assert np.isnan(step_info['SettlingTime']) + assert step_info['SteadyStateValue'] == 1 + + # Large feedthrough: default time vector ends before 90% is reached + sys = ct.tf([2.99, -0.0685], [1, 2.83]) + step_info = ct.step_info(sys) + assert isclose(step_info['SteadyStateValue'], sys.dcgain()) + assert step_info['Peak'] == pytest.approx(2.99) diff --git a/control/timeresp.py b/control/timeresp.py index 1d252b270..8125dba9f 100644 --- a/control/timeresp.py +++ b/control/timeresp.py @@ -1698,14 +1698,15 @@ def step_info( steady_state_value: complex = np.nan if not np.isnan(InfValue) and not np.isinf(InfValue): - # RiseTime - tr_lower_index = np.nonzero( - sgnInf * (yout - RiseTimeLimits[0] * InfValue) >= 0 - )[0][0] - tr_upper_index = np.nonzero( - sgnInf * (yout - RiseTimeLimits[1] * InfValue) >= 0 - )[0][0] - rise_time = T[tr_upper_index] - T[tr_lower_index] + # RiseTime (undefined if the response never gets there, + # e.g. if the time vector is too short) + tr_lower = np.nonzero( + sgnInf * (yout - RiseTimeLimits[0] * InfValue) >= 0)[0] + tr_upper = np.nonzero( + sgnInf * (yout - RiseTimeLimits[1] * InfValue) >= 0)[0] + if tr_lower.size and tr_upper.size: + tr_upper_index = tr_upper[0] + rise_time = T[tr_upper_index] - T[tr_lower[0]] # SettlingTime outside_threshold = np.nonzero( @@ -1717,8 +1718,11 @@ def step_info( if settled < len(T): settling_time = T[settled] - settling_min = min((yout[tr_upper_index:]).min(), InfValue) - settling_max = max((yout[tr_upper_index:]).max(), InfValue) + if tr_lower.size and tr_upper.size: + settling_min = min( + (yout[tr_upper_index:]).min(), InfValue) + settling_max = max( + (yout[tr_upper_index:]).max(), InfValue) # Overshoot y_os = (sgnInf * yout).max()