I am trying to find the transmission coefficient of a potential using a direct integration technique. So I start the integration left of the barrier and end to the right of it. One can then calculate the transmission coefficient by comparing the the value of the solution at the right hand side to its initial value.
I have a lot of parameters for the potential. Call them, say, $a, b, c, d, e, f$ and $g$. For a large set of values of $a-f$, I want to find the value of $g$ which maximizes the transmission and the value of that transmission.
The speed is acceptable if I fix $g$, but calculating many values of $g$ to manually search for this maximum is incredibly slow, so I wish to use some numerical optimization method to minimize the number of $g$ values that need to be used, however the obvious FindMaximum and NMaximize do not work for me at all.
Here is a minimal (non) working example, which is effectively using NDSolve as a very silly way to square a number and then trying to find the minimum of(x^2-1)^2: this feature, and the use of multiple NDSolves to avoid indeterminate points, are a key parts of the real problem though:
Calcλsquared[λ_, ϵ_] := (temp =
f[x] /. NDSolve[{f'[x] == 2 f[x]/x, f[-λ] == λ^2},
f[x], {x, -λ, -ϵ}][[1]];
temp = f[x] /.
NDSolve[{f'[x] == 2 f[x]/x,
f[ϵ] == (temp /. x -> -ϵ)},
f[x], {x, ϵ, λ}][[1]];
temp /. x -> λ)
Followed by either
FindMinimum[(Calcλsquared[λ, 10^(-3)] - 1)^2, {λ, 0.99}]
or
NMinimize[(Calcλsquared[λ, 10^(-3)] - 1)^2, λ]
Edit: I have made some progress using the ?NumericQ as mentioned here: optimization problem with NDSolve. However this does not completely solve my problem. I have managed to find a new minimal example. In the real case the function returns a list and this breaks it:
Calcλsquared[λ_?NumericQ, ϵ_?NumericQ] := (temp =
f[x] /. NDSolve[{f'[x] == 2 f[x]/x, f[-λ] == λ^2},
f[x], {x, -λ, -ϵ}][[1]];
temp = f[x] /. NDSolve[{f'[x] == 2 f[x]/x,
f[ϵ] == (temp /. x -> -ϵ)},
f[x], {x, ϵ, λ}][[1]];
{λ, temp /. x -> λ})
and
FindMinimum[(Calcλsquared[λ[[2]], 10^(-3)] - 1)^2, {λ, 0.99}]
WhenEvent[]to detect when the derivative is zero is great for this, but you'll have to postprocess to filter out anything that isn't a maximum. – J. M.'s missing motivation Jun 04 '20 at 11:56FindMinimum? – Alex Trounev Jun 04 '20 at 17:51MinimalBy, For examplelst = Table[{la, (Calc\[Lambda]squared[la, 10^-3][[2]] - 1)^2}, {la, .01, 1.5, .01}]; MinimalBy[lst, Last]gives out{{0.99, 0.0000650146}}– Alex Trounev Jun 04 '20 at 18:09