![](https://secure.gravatar.com/avatar/9138c60aeb2cdeecc2ad704182d50f94.jpg?s=120&d=mm&r=g)
Salut Camille: Just don't ask for the solution in the last point, since in order to calculate a value of the solution at that point it has to go a bit beyond (y should be defined from both sides of ti for the derivative to exist in ti (this is a necessary condition)), but interpolation does not really define values beyond the right limit. Here I've modified your example: http://nbviewer.ipython.org/github/guziy/PyNotebooks/blob/master/ode_demo.ip... Cheers 2014-07-30 4:05 GMT-04:00 camille chambon <camillechambon@yahoo.fr>:
camille chambon <camillechambon <at> yahoo.fr> writes:
Hello,
I would like to solve the ODE dy/dt = -2y + data(t), between t=0..3, for
y(t=0)=1.
I wrote the following code:
import numpy as npfrom scipy.integrate import odeintfrom scipy.interpolate
import interp1dt = np.linspace(0, 3, 4)data = [1, 2, 3, 4]linear_interpolation = interp1d(t, data)def func(y, t0):
print 't0', t0 return -2*y + linear_interpolation(t0)soln =
odeint(func, 1, t)
When I run this code, I get several errors:
ValueError: A value in x_new is above the interpolation
range.odepack.error: Error occurred while calling the Python function named func
My interpolation range is between 0.0 and 3.0.
Printing the value of t0 in func, I realized that t0 is actually sometimes
above my interpolation range: 3.07634612585, 3.0203768998, 3.00638459329, ...
I have a few questions:
- how does integrate.ode makes t0 vary? Why does it make t0 exceed the
infimum (3.0) of my interpolation range?
- in spite of these errors, integrate.ode returns an array which seems to
errors?
- if I shouldn't ignore these errors, what is the best way to avoid them?
2 suggestions for the last question:
- in interp1d, I could set bounds_error=False and fill_value=data[-1] since the t0 outside of my interpolation range seem to be closed to t[-1]: linear_interpolation = interp1d(t, data, bounds_error=False, fill_value=data[-1]) But first I would like to be sure that with any other func and any other data the t0 will always remain closed to t[-1]. For example, if integrate.ode chooses a t0 below my interpolation range, the fill_value would be still data[-1], which would not be correct. Maybe to know how integrate.ode makes t0 vary would help me to be sure of that (see my first question).
- in func, I could enclose the linear_interpolation call in a try/except block, and, when I catch a ValueError, I recall linear_interpolation but with t0 truncated:
def func(y, t0): try: interpolated_value = linear_interpolation(t0) except ValueError:
interpolated_value = linear_interpolation(int(t0)) # truncate t0
return -2*y + interpolated_value
At least this solution permits linear_interpolation to still raise an exception if integrate.ode makes a t0 above 4.0 or below -1.0. I can then be alerted of incoherent behavior. But it is not really readable and the
contain correct value. So, should I just catch and ignore these truncation seems to me a little arbitrary by now.
Maybe I'm just overthinking about these errors. Please let me know.
Thanks in advance.
Cheers,
Camille
_______________________________________________ SciPy-User mailing list SciPy-User <at> scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
Sorry, I made a typo in the range of t: I want to solve the ODE dy/dt = -2y + data(t) between t=0..3, and not between t=0..4. I corrected my original message. Cheers, Camille
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
-- Sasha