# Solving differential equation in Python with discretized variable coefficients

I am trying to solve a differential equation with discretized variable coefficients which are calculated from a time serie. In this case the Runge-Kutta step size is fixed by the frequency in the time serie.

Is there any Python Runge-Kutta RK4, RK5 solvers suitable for fixed step?

• Why not use interpolation on the coefficients and just set the max. step size to the sampling interval? The stages of the method need values at intermediate points, so you need to do some interpolation anyway, so why not do something more sensible than a step function. – Lutz Lehmann Dec 1 '19 at 20:25
• It is not necessary because the frequency of the time serie is one millisecond which gives a very good numerical accuracy. – Bouarfa Mahi Dec 1 '19 at 20:45
• Then contemplate the use of a multi-step method, as that uses only data from the sampling points. In a one-step method, you introduce errors of $O(h)$ when using piecewise constant functions, which contributes an $O(h)$ error in the solution. – Lutz Lehmann Dec 1 '19 at 21:02
• Could you add an example situation? As I understand it, you have an ODE like $y'(t)=a(t)y(t)$ where $a(t)$ is given as a function table $a(t_0+k\Delta t)$ without knowing the expression for $a(t)$. This severely restricts the packaged methods you can use, as they all assume that the ODE function depends only on $t$ and the state, so that you need to translate $t$ into an index into the function table. Or you code your method yourself, adapted to the function table. – Lutz Lehmann Dec 2 '19 at 7:39
• Thank you i am going with diffeqpy. – Bouarfa Mahi Dec 2 '19 at 15:26

If you use diffeqpy you can use the commands adaptive=false,dt=... to specify fixed time stepping. The following is for using the Dormand-Prince RK45 method with fixed time stepping on the Lorenz equation:

from diffeqpy import de
import matplotlib.pyplot as plt

def f(u,p,t):
x, y, z = u
sigma, rho, beta = p
return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]

u0 = [1.0,0.0,0.0]
tspan = (0., 100.)
p = [10.0,28.0,8/3]
prob = de.ODEProblem(f, u0, tspan, p)

For a multistep method, one can use de.VCABM() where de.DP5() sits.