# Simultaneous minimization with respect to two parameters

I am struggling with the minimization of a solution of an ODE with respect to two parameters. Here is the code I write:

ε = 10^-6;

solw = NDSolve[{w''[t] + (w[t] + w[t]^2) w'[t] == 1/2 (1 + Tanh[100 t]), w == 0,
w' == 0}, w, {t, 0, 2 π}, Method -> "MethodOfLines"];
wsol[t_] := Evaluate[w[t] /. solw]

solG[s2_] :=
NDSolve[{G''[t] + (G[t] + G[t]^2) G'[t] == s2 1/(Sqrt[π] ε) Exp[-(t/ε)^2], G == 0,
G' == 0}, G, {t, 0, 2 π}, Method -> "MethodOfLines"]
Gsol[t_] := Evaluate[G[t] /. solG[s2]]
approx[t_] :=
s1 NIntegrate[Gsol[t - τ] 1/2 (1 + Tanh[100 τ]), {τ, 0, t}, Method -> "DoubleExponential"]

tab = Table[Abs[wsol[t] - approx[t]], {t, 0, 1, 0.01}];
NMinimize[Max[tab], s1, s2]


However, this works only if I fix s2 in the definition of solG and minimize tab with respect to s1. Any idea how can I extend the algorithm to minimize tab with respect to both s1 and s2?

Edit

I have modified the code a bit using ParametricNDSolve. Now it looks like:

ε = 10^-6;

solw = NDSolve[{w''[t] + (w[t] + w[t]^2) w'[t] == 1/2 (1 + Tanh[100 t]), w == 0,
w' == 0}, w, {t, 0, 2 π}, Method -> "MethodOfLines"];
wsol[t_] := Evaluate[w[t] /. solw]

solG=
ParametricNDSolve[{G''[t] + (G[t] + G[t]^2) G'[t] == s2 1/(Sqrt[π] ε) Exp[-(t/ε)^2], G == 0,
G' == 0}, G, {s2}, {t, 0, 2 π}, Method -> "MethodOfLines"]
Gsol[t_,s2_] := Evaluate[G[s2][t] /. solG]
approx[t_,s1_,s2_] :=
s1 NIntegrate[Gsol[τ,s2] 1/2 (1 + Tanh[100 (t-τ)]), {τ, 0, t}, Method -> "LocalAdaptive"]

• What is the point of defining approx? tab does not depend on s1 or approx. – Carl Woll Mar 22 '18 at 14:42
• Good point, thanks. Fixed – Asatur Khurshudyan Mar 22 '18 at 15:37

\[Epsilon] = 10^-6;
Delta[t_] := 1/(Sqrt[\[Pi]] \[Epsilon]) Exp[-(t/\[Epsilon])^2]
f[t_] := 1/2 (1 + Tanh[100 t])

solw = NDSolve[{w''[t] + Exp[w[t]] == f[t], w == 0, w' == 0},
w, {t, 0, 2 \[Pi]}, Method -> "MethodOfLines"];
wsol[t_] := Evaluate[w[t] /. solw]

solG = ParametricNDSolve[{G''[t] + Exp[G[t]] == s2 Delta[t],
G == 0, G' == 0}, G, {t, 0, 2 \[Pi]}, {s2},
Method -> "MethodOfLines"];
GGreen[t_, s2_] := Evaluate[G[s2][t] /. solG]
Gsol[t_, s1_, s2_] :=
s1 NIntegrate[GGreen[\[Tau], s2] f[t - \[Tau]], {\[Tau], 0, t},

However, one needs to be careful with choosing the range of s2 in the NMinimize. For -1 <= 2 <=2 the solution is better than for -3 <= s2 <=3. This means that there might be a better solution.