# How to put solutions of mutiple DEs with NDSolve in table

I have $$N$$ differential equation for each $$s \in {1,2,...,N}$$ on the domain $$x \in [1,2]$$ of the form (e.g.)

$$xf_s'(x) + \log(xs) f_s(x)+1 = 0$$, with the boundary condition $$f_s(1) = e^{-s}$$

Now I want to solve this equation with NDSolve. Put the values for the solutions at $$x = 2$$, i.e. $$f_s(2)$$ in a table enumerated by the index $$s$$. Then I want to interpolate between these values in the table to get a function $$g(s) = f_s(2)$$.

However, since I am new to mathematica, I have no Idea how to do this. I can solve the equation for some $$s$$ with NDSolve:

NDSolve[{x*D[f[x], x] + Log[x*s]*f[x] + 1 == 0, f == Exp[-s]}, f, {x, 1, 2}]

and this seems to work. However my main issue right now is putting the values of the output function in the table. How can I do this?

n = 100(* the number of equation,N*);
sols = {};(*table*)
Do[
AppendTo[sols,
f /. Last@
NDSolve[{x*D[f[x], x] + Log[x*s]*f[x] + 1 == 0, f == Exp[-s]},
f, {x, 1, 2}]],
{s, 1, n}]; // AbsoluteTiming


=>

{0.144813, Null}


sols // ListLinePlot Try this

Clear[s, f, sol];
sol = DSolve[{x*D[f[x], x] + Log[x*s]*f[x] + 1 == 0, f == Exp[-s]},f, x][[1, 1, 2, 2]] Then make the table and interpolate it:

f = Interpolation[Table[{s, sol /. x -> 2}, {s, 0.01, 1, 0.01}], InterpolationOrder -> 2]


and plot it:

Plot[f[x], {x, 0.01, 1}] Have fun!

• I'm afraid that $s$ is Integer? May 28 '19 at 13:20
• @Xminer Nothing to be afraid of. Just take those its values you need. May 28 '19 at 13:48
eqn = {x*D[f[x], x] + Log[x*s]*f[x] + 1 == 0, f == Exp[-s]};


The example can be solved exactly with DSolve

sol = DSolve[eqn, f, x][]

(* {f -> Function[{x},
1/2 E^(-s -
1/2 Log[s x]^2) (2 E^(Log[s]^2/2) +
E^s Sqrt[2 π] Erfi[Log[s]/Sqrt] -
E^s Sqrt[2 π] Erfi[Log[s x]/Sqrt])]} *)


Verifying the solution,

And @@ eqn /. sol // Simplify

(* True *)


For a table of the exact solutions for integer values of s:

Grid[
Prepend[
{#, f[x] /. sol /. s -> #} & /@ Range // FullSimplify,
Style[#, 12, Bold] & /@ {s, f[x]}],
Frame -> All] For a Plot for integer values of s

Plot[
Evaluate@
Table[Tooltip[f[x] /. sol, s], {s, 1, 5}],
{x, 1, 2},
PlotLegends -> Range] Or plot in 3D

Show[
Plot3D[f[x] /. sol, {x, 1, 2}, {s, 1, 5},
PlotStyle -> Opacity[0.25]],
ParametricPlot3D[
Evaluate@
Table[{x, s, f[x] /. sol}, {s, 1, 5}],
{x, 1, 2}],
AxesLabel -> (Style[#, 12, Bold] & /@ {x, s, f}),
BoxRatios -> {1, 1, 1/2}] For a numeric approach use ParametricNDSolve or ParametricNDSolveValue

fp = ParametricNDSolveValue[eqn, f, {x, 1, 2}, {s}];


In 2 D

Plot[
Evaluate@
Table[Tooltip[fp[s][x], s], {s, 1, 5}],
{x, 1, 2},
PlotLegends -> Range]


Or in 3D

Show[
Plot3D[
fp[s][x], {x, 1, 2}, {s, 1, 5},
PlotStyle -> Opacity[0.25]],
ParametricPlot3D[
Evaluate@Table[{x, s, fp[s][x]}, {s, 1, 5}],
{x, 1, 2}],
AxesLabel -> (Style[#, 12, Bold] & /@ {x, s, f}),
BoxRatios -> {1, 1, 1/2}]


Try ParametricNDSolve

F = ParametricNDSolveValue[{x*D[f[x], x] +Log[x*s]*f[x] + 1 == 0,f == Exp[-s]}, f, {x, 1, 2}, s]


which returns a function F[s][x] .

F[s]} is the function you're looking for

Plot[F[s], {s, 1, 100}] 