I have a system of differential equations, where $$ \mathbf{y}'(x)=\mathbf{f}(\mathbf{y})$$ but I don't have an explicit expression for $f$, in fact I have to solve a linear system of equations to obtain $\mathbf{f}(\mathbf{y})$ for any $\mathbf{y}(x)$. I know I can't do this with NDSolve which just requires a simple expression for the ODE system. I want a method, much like the Scipy ODE solver, that gets $\mathbf{y}'(x)$ by calling a Mathematica function (that I have defined) that gives $\mathbf{f}(\mathbf{y})$. Is there such a thing?

  • 4
    $\begingroup$ I don't see why NDSolve couldn't do this. It would be helpful if you provided a simple example of what you're trying to do. $\endgroup$
    – Carl Woll
    Commented Nov 17, 2017 at 17:57
  • $\begingroup$ You might want to read up on using _?NumericQ to define your f(y). Try here for starters. But yeah, a simple, concrete example would help spur answers. $\endgroup$
    – Chris K
    Commented Nov 17, 2017 at 18:54

1 Answer 1


Since the OP didn't provide an example, here is a made up function f:

A = RandomReal[1, {4,4}];
f[y:{__?NumericQ}] := LinearSolve[A, y]

Here's an ODE that uses f:

sol = NDSolve[{y'[x] == f[y[x]], y[0] == {1,2,3,4}}, y, {x,0,1}];
sol //OutputForm

{{y -> InterpolatingFunction[{{0., 1.}}, <>]}}

Finally, here is the value at $x=1$:

y[1] /. First @ sol

{-13.846, 3.39417, 17.1691, -3.79776}

  • $\begingroup$ Sorry for the late reply. This is exactly the solution I was looking for. I'm surprised this isn't mentioned in the documentation for NDSolve $\endgroup$
    – oweydd
    Commented Nov 22, 2017 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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