I want to use NDSolve within a Module and output the solution at some times. (Mathematica 10)
All works fine with global variables (symbols)
p[times_List, {k1_, k2_, k3_, k4_}] := Module[{eq},
eq = {
m'[t] == -(k2 + k3) m[t] + k1 i[t] + k4 p[t],
i'[t] == k2 m[t] - k1 i[t],
p'[t] == k3 m[t] - k4 p[t],
m[0] == 1, i[0] == 0, p[0] == 0};
sol = NDSolve[eq, {i[t], m[t], p[t]}, {t, 0, tlist[[-1]]}][[1]];
pop[t_] = {i[t], m[t], p[t]} /. sol;
pop /@ times
]
Here an example:
p[Range[0, 5], {1, 1, 2, 3}]
The output is:
{{0., 1., 0.}, {0.312093, 0.404352, 0.283556}, {0.360173, 0.381508,
0.25832}, {0.371482, 0.376543, 0.251976}, {0.374165, 0.375366,
0.250469}, {0.374802, 0.375087, 0.250111}}
But with local variables (symbols)
p[[times_List, {k1_, k2_, k3_, k4_}] := Module[{eq, t, pop, m, i, p, sol},
eq = {
m'[t] == -(k2 + k3) m[t] + k1 i[t] + k4 p[t],
i'[t] == k2 m[t] - k1 i[t],
p'[t] == k3 m[t] - k4 p[t],
m[0] == 1, i[0] == 0, p[0] == 0};
sol = NDSolve[eq, {i[t], m[t], p[t]}, {t, 0, tlist[[-1]]}][[1]];
pop[t_] = {i[t], m[t], p[t]} /. sol;
pop /@ times
]
it does not work anymore:
p[Range[0, 5], {1, 1, 2, 3}]
The output is:
{{i$18625[0], m$18625[0], p$18625[0]}, {i$18625[1], m$18625[1],
p$18625[1]}, {i$18625[2], m$18625[2], p$18625[2]}, {i$18625[3],
m$18625[3], p$18625[3]}, {i$18625[4], m$18625[4],
p$18625[4]}, {i$18625[5], m$18625[5], p$18625[5]}}
Any ideas why this is not working like that? Thank you.
sol = NDSolve[eq, {i, m, p}, {t, 0, 5}][[1]]
, i.e.,m
insteadm[t]
. $\endgroup$