From 306a2165f0111f9f950c2d2ad97e3e75b5eb291c Mon Sep 17 00:00:00 2001 From: Miroslav Fikar Date: Sun, 12 Dec 2021 14:58:15 +0100 Subject: [PATCH 1/2] extrapolation in ufun throwed errors --- control/iosys.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/control/iosys.py b/control/iosys.py index 2c9e3aba5..b575be0f1 100644 --- a/control/iosys.py +++ b/control/iosys.py @@ -1856,6 +1856,8 @@ def ufun(t): if idx == 0: # For consistency in return type, multiple by a float return U[..., 0] * 1. + elif idx == len(T): # request for extrapolation, stop at right side + return U[..., idx-1] * 1. else: dt = (t - T[idx-1]) / (T[idx] - T[idx-1]) return U[..., idx-1] * (1. - dt) + U[..., idx] * dt From 5a75ca07334e3537f142aef12738e3eb598aaf56 Mon Sep 17 00:00:00 2001 From: Richard Murray Date: Sun, 26 Dec 2021 07:31:22 -0800 Subject: [PATCH 2/2] update iosys ufun to extrapolate --- control/iosys.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/control/iosys.py b/control/iosys.py index b575be0f1..bcf36610c 100644 --- a/control/iosys.py +++ b/control/iosys.py @@ -1852,15 +1852,10 @@ def input_output_response( # but has a lot less overhead => simulation runs much faster def ufun(t): # Find the value of the index using linear interpolation - idx = np.searchsorted(T, t, side='left') - if idx == 0: - # For consistency in return type, multiple by a float - return U[..., 0] * 1. - elif idx == len(T): # request for extrapolation, stop at right side - return U[..., idx-1] * 1. - else: - dt = (t - T[idx-1]) / (T[idx] - T[idx-1]) - return U[..., idx-1] * (1. - dt) + U[..., idx] * dt + # Use clip to allow for extrapolation if t is out of range + idx = np.clip(np.searchsorted(T, t, side='left'), 1, len(T)-1) + dt = (t - T[idx-1]) / (T[idx] - T[idx-1]) + return U[..., idx-1] * (1. - dt) + U[..., idx] * dt # Create a lambda function for the right hand side def ivp_rhs(t, x):