I'm interested in solving a differential equation in which I don't know the analytical form of the right hand side, I only know its numerical value for a finite set of values of the independent variable:


I haven't found a library that allows me to solve this problem and I want to make sure if there are any before trying to solve it myself, I figured one would have to interpolate the array to match the specific grid that the integrator uses, or fix the grid to the points $x_i$ for which I have the value $f(x_i)$.

  • 2
    $\begingroup$ Both methods that you're proposing should work. Particularly, if you create an interpolation function you can use it as a parameter for the integrator. $\endgroup$
    – nicoguaro
    Sep 12 '18 at 2:42
  • $\begingroup$ Thinking a little bit, collocation methods might work well for your problem. $\endgroup$
    – nicoguaro
    Sep 12 '18 at 20:05

Because the r.h.s. is independent of $y$, your case is already handled as a special case by some standard interpolation libraries. Here's an example of how to do with cubic splines in scipy (docs link), I picked a nice function $(1+x^2)^{-1}$ just to illustrate:

x = array([0.0] + sorted(np.random.rand(16)) + [1.0])
y = 1/(1+x*x)
f = CubicSpline(x, y)
u = f.antiderivative()

In [34]: u(0.5) - np.math.atan(0.5)
Out[34]: -5.874574474096228e-07

So first you interpolate the r.h.s. function $f$, then its antiderivative $u$ is computed exactly for that interpolant, solving $u'(x)=f(x)$. It's a reasonably common basic feature for such a library.

  • $\begingroup$ Thank you, I will try your solution. Would you know what to do if $f$ was a function of $y$ too? $\endgroup$ Sep 13 '18 at 13:29
  • 1
    $\begingroup$ This idea is only for the special case. Do you mean if at each time you know the function $y\mapsto f(y,x_i)$? I'm not quite sure what form that would take, it seems like an unusual setup. You'd need a proper ODE solver and a more precise idea how you'd interpolate between the $x_i$'s. $\endgroup$
    – Kirill
    Sep 13 '18 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.