3

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:

$$\frac{dy}{dx}=f(x_i).$$

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)$.

nicoguaro
  • 8,500
  • 6
  • 23
  • 49
  • 2
    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. – nicoguaro Sep 12 '18 at 02:42
  • Thinking a little bit, collocation methods might work well for your problem. – nicoguaro Sep 12 '18 at 20:05

1 Answers1

5

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.

Kirill
  • 11,438
  • 2
  • 27
  • 51
  • Thank you, I will try your solution. Would you know what to do if $f$ was a function of $y$ too? – David Leonardo Ramos Sep 13 '18 at 13:29
  • 1
    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. – Kirill Sep 13 '18 at 14:22