Skip to content

Commit 8356044

Browse files
authored
Merge pull request #682 from miroslavfikar/mifi_ufun
Fix bug in ufun: extrapolation throwing errors
2 parents 89291e6 + 9bd61b0 commit 8356044

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

control/iosys.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,13 +1852,10 @@ def input_output_response(
18521852
# but has a lot less overhead => simulation runs much faster
18531853
def ufun(t):
18541854
# Find the value of the index using linear interpolation
1855-
idx = np.searchsorted(T, t, side='left')
1856-
if idx == 0:
1857-
# For consistency in return type, multiple by a float
1858-
return U[..., 0] * 1.
1859-
else:
1860-
dt = (t - T[idx-1]) / (T[idx] - T[idx-1])
1861-
return U[..., idx-1] * (1. - dt) + U[..., idx] * dt
1855+
# Use clip to allow for extrapolation if t is out of range
1856+
idx = np.clip(np.searchsorted(T, t, side='left'), 1, len(T)-1)
1857+
dt = (t - T[idx-1]) / (T[idx] - T[idx-1])
1858+
return U[..., idx-1] * (1. - dt) + U[..., idx] * dt
18621859

18631860
# Create a lambda function for the right hand side
18641861
def ivp_rhs(t, x):

0 commit comments

Comments
 (0)