grid.ode module

Linear ordinary differential equation solver.

Solves a linear ordinary differential equation of order \(K\) of the form

\[\sum_{k=0}^{K} a_k(x) \frac{d^k y(x)}{dx^k} = f(x)\]

with either boundary conditions on the two end-points for some unknown function \(y(x)\) with independent variable \(x\) or as an initial value problem on a interval.

It also supports the ability to transform the independent variable \(x\) to another domain \(g(x)\) for some \(K\)-th differentiable transformation \(g(x)\). This is particularly useful to convert infinite domains, e.g. \([0, \infty)\), to a finite interval.

solve_ode_bvp(x, fx, coeffs, bd_cond, transform=None, tol=0.0001, max_nodes=5000, initial_guess_y=None, no_derivatives=True)[source]

Solve a linear ODE with boundary conditions.

Parameters
  • x (np.ndarray(N,)) – Points of the independent variable/domain. If transform is provided, then these are the points that are to be transformed.

  • fx (callable) – Right-hand function \(f(x)\).

  • coeffs (list[callable or float] or ndarray(K + 1,)) – Coefficients \(a_k\) of each term \(\frac{d^k y(x)}{d x^k}\) ordered from 0 to K. Either a list of callable functions \(a_k(x)\) that depends on \(x\) or array of constants \(\{a_k\}_{k=0}^{K}\).

  • bd_cond (list[list[int, int, float]]) – Boundary condition specified by list of size \(K\) of three entries [i, j, C], where \(i \in \{0, 1\}\) determines whether the lower or upper bound is being constrained, \(j \in \{0, \cdots, K\}\) determines which derivative \(\frac{d^j y}{dx^j}\) is being constrained, and \(C\) determines the boundary constraint value, i.e. \(\frac{d^j y}{dx^j} = C\). Size needs to be the same as the order \(K\) of the ODE. If transform is given, then the constraint of the derivatives is assumed to be with respect to the new coordinate i.e. \(\frac{d^j y}{dr^j} = C\).

  • transform (BaseTransform, optional) – Transformation from one domain \(x\) to another \(r := g(x)\).

  • tol (float, optional) – Tolerance of the ODE solver and for the boundary condition residuals. See scipy.integrate.solve_bvp for more info.

  • max_nodes (int, optional) – The maximum number of mesh nodes that determine termination. See scipy.integrate.solve_bvp function for more info.

  • initial_guess_y (ndarray(K, N), optional) – Initial guess for \(y(x), \cdots, \frac{d^{K} y}{d x^{K}}\) at the points x. If not provided, then random set of points from 0 to 1.

  • no_derivatives (bool, optional) – If true, when transform is used then it only returns the solution \(y(x)\) rather than its derivative. If false, it includes the derivatives up to \(P-1\).

Returns

Interpolate function (scipy.interpolate.PPoly) instance whose input is the original domain \(x\) and output is an array of the function \(y(x)\) evaluated on the points and its derivatives wrt to \(x\) up to \(K - 1\).

Return type

callable

solve_ode_ivp(x_span, fx, coeffs, y0, transform=None, method='DOP853', no_derivatives=False, rtol=1e-08, atol=1e-06)[source]

Solve a linear ODE as an initial value problem.

Parameters
  • x_span ((int, int)) – The interval of integration \((t_0, t_1)\) from the first point to the second point.

  • fx (callable) – Right-hand function \(f(x)\).

  • coeffs (list[callable or float] or ndarray(K + 1,)) – Coefficients \(a_k\) of each term \(\frac{d^k y(x)}{d x^k}\) ordered from 0 to K. Either a list of callable functions \(a_k(x)\) that depends on \(x\) or array of constants \(\{a_k\}_{k=0}^{K}\).

  • y0 (list[K] or ndarray(K)) – The initial value conditions \(\frac{d^k y(t_0)}{d x^k} = c_k\) at the initial point \(t_0\) from \(k=0,\cdots,K-1\).

  • transform (BaseTransform, optional) – Transformation from one domain \(x\) to another \(r := g(x)\).

  • method (str) – The method used to solve the ode by scipy. See scipy.integrate.solve_ivp function for more info.

  • no_derivatives (bool, optional) – If true, when transform is used then it only returns the solution \(y(x)\) rather than its derivative. If false, it includes the derivatives up to \(P-1\).

  • rtol ((float, float), optional) – The relative and absolute tolerance. See scipy.integrate.solve_ivp for more info.

  • atol ((float, float), optional) – The relative and absolute tolerance. See scipy.integrate.solve_ivp for more info.

Returns

Interpolate function (scipy.interpolate.PPoly) instance whose input is the original domain \(x\) and output is an array of the function \(y(x)\) evaluated on the points and its derivatives wrt to \(x\) up to \(K - 1\).

Return type

callable