My problem
I have a large system of differential equations which is automatically created, but to give you an easy example let's just say it is
eqs = {f'[x] == a[x]*g[x], g'[x] == -a[x]*f[x]};
As you can see, the system of equations contains the still undetermined function a[x]
.
I want to numerically solve these equations for different functions a[x]
, what I wrote is something like
solveMyEquation[a_] := Module[{foo, bar},
foo = Flatten[{Evaluate[eqs], f[0] == 0, g[0] == 1}];
bar = NDSolve[foo, {f, g}, {x, 0, 10}];
Plot[Evaluate[{f[x], g[x]} /. bar], {x, 0, 10}]]
If I try to solve this using e.g. solveMyEquation[1 &]
, I get a huge load of error messages, starting with
NDSolve::underdet: There are more dependent variables, {a[x],f[x],g[x]}, than equations, so the system is underdetermined.
ReplaceAll::reps: {NDSolve[{(f^\[Prime])[x]==a[x] g[x],(g^\[Prime])[x]==-a[x] f[x],f[0]==0,g[0]==1},{f,g},{x,0,10}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.
NDSolve::dsvar: 0.0002042857142857143` cannot be used as a variable.
What I have tried
The problem seems to be that the function a[x]
is not inserted into eqs
. That's what I was trying to achieve with the Evaluate
, but apparently it doesn't work the way I thought it did.
The following obviously works fine:
solveMyEquation[a_] := Module[{foo, bar},
foo = Flatten[{{f'[x] == a[x]*g[x], g'[x] == -a[x]*f[x]}, f[0] == 0, g[0] == 1}];
bar = NDSolve[foo, {f, g}, {x, 0, 10}];
Plot[Evaluate[{f[x], g[x]} /. bar], {x, 0, 10}]]
solveMyEquation[1 &]
But in my actual problem, the expression for eqs
fills several pages and I don't like copy-pasting it (even though it does work).