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?
Asked
Active
Viewed 108 times
1
1 Answers
2
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}
Carl Woll
- 130,679
- 6
- 243
- 355
-
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 – oweydd Nov 22 '17 at 13:24
_?NumericQto define your f(y). Try here for starters. But yeah, a simple, concrete example would help spur answers. – Chris K Nov 17 '17 at 18:54