1
$\begingroup$

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] == 0, 
w'[0] == 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] == 0, 
   G'[0] == 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] == 0, 
    w'[0] == 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] == 0, 
       G'[0] == 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"]
$\endgroup$
  • $\begingroup$ What is the point of defining approx? tab does not depend on s1 or approx. $\endgroup$ – Carl Woll Mar 22 '18 at 14:42
  • $\begingroup$ Good point, thanks. Fixed $\endgroup$ – Asatur Khurshudyan Mar 22 '18 at 15:37
1
$\begingroup$

Here is an answer.

\[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] == 0, w'[0] == 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] == 0, G'[0] == 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}, 
   Method -> "LocalAdaptive"]

interpol = 
  ListInterpolation[
   Table[Gsol[t, s1, s2], {t, 0, 1, 0.1}, {s2, -5, 5, 0.1}], {{0, 
     1}, {-5, 5}}];

NMinimize[{Max[
   Table[Abs[wsol[t] - interpol[t, s2]], {t, 0, 1, 0.1}]], -5 <= s2 <=
    5}, {s1, s2}]

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.