# How to find absolute minimum with NMinimize

So I have a trefoil and twisted cubic that are very close to intersecting but do not. I know NMinimize can be used to best approximate the minimum, but it supposedly isn't perfect.

How can I precisely find where the absolute minimum occurs how close these two are to intersecting. What else would I want to use in addition to NMinimize?

Function: $$f(a,b)=b^6 \sin ^2\left(\frac{3 a}{2}\right)+\left(\sin (a) \left(\cos \left(\frac{3 a}{2}\right)+2\right)-b^2\right)^2+\cos (a) \cos \left(\frac{3 a}{2}\right)-b^2+2$$

NMinimize output: {1.53724, {a -> 1.69939, b -> 1.04861}} (how do I interpret this???)

Complete Code:

F[a_, b_] := ((2 + Cos[3 a/2] Cos[a]) - (b)^2
+ (((2 + Cos[3 a/2]) Sin[a]) - (b^2))^2)
+ (Sin[3 a/2] (b^3))^2 ;

ContourPlot[
F[a, b], {a, -1, 1}, {b, 0, 4Pi},
Contours -> 20]

NMinimize[(((2 +
Cos[3 a/2] Cos[a]) - (b))^2 + (((2 + Cos[3 a/2]) Sin[
a]) - (b^2))^2) + (Sin[3 a/2] (b^3))^2, {a, b}]

• Do you have any sort of initial code or minimum working example written in Wolfram Language for Mathematica?
– demm
May 18, 2021 at 23:32
• f[a_,b_] (2 - b + Cos[a] Cos[(3a)/2])^2 + (-b^2 + (2 + Cos[(3a)/2]) Sin[a])^2 + b^6 Sin[(3a)/2]^2 May 19, 2021 at 0:08
• I now edited it May 19, 2021 at 1:48
• What is your complete code? May 19, 2021 at 1:54
• I just added it in May 19, 2021 at 2:04

Do you want to consider the distance of two parametric curves u[a] and v[b] as below?

u[a_] := {2 + Cos[3 a/2] Cos[a], (2 + Cos[3 a/2]) Sin[a], Sin[3 a/2]};
v[b_] := {b, b^2, b^3};
sol = NMinimize[{EuclideanDistance[u[a], v[b]],
0 <= a <= 4 π, -2 <= b <= 2}, {a, b}];
dist = Graphics3D[{Red, PointSize[.02], Point[u[a]], Point[v[b]],
PointSize[.02], Thickness[.01], Blue, Line[{u[a], v[b]}]} /. sol[[2]]];

Show[{ ParametricPlot3D[u[a], {a, 0, 4 π}, PlotStyle -> Yellow],
ParametricPlot3D[v[b], {b, -2, 2}, PlotStyle -> Green], dist},
PlotRange -> All]


• Yes, except with the constraints of the trefoil being -1 to 1 and not -2 to 2. How do I find that blue line value? May 19, 2021 at 3:22
• @user80088 sol[[1]] is the distance what you want. May 19, 2021 at 3:26
• Yes, I want the distance May 19, 2021 at 6:45

Each such situation has its particulars; when the function is non-convex you have to do some prior investigation. Here you can start with

F[a_, b_] := ((2 + Cos[3 a/2] Cos[a]) - (b)^2 + (((2 + Cos[3 a/2]) Sin[a])
-(b^2))^2) + (Sin[3 a/2] (b^3))^2;
{FunctionPeriod[F[a, b], a], FunctionPeriod[F[a, b], b]}
Limit[F[a, b], b -> Infinity]

(*
{4 \[Pi], 0}
\[Infinity] Sin[(3 a)/2]^2
*)


which is a starting point for a first plot:

Plot3D[F[a, b], {a, -2 Pi, 2 Pi}, {b, -10, 10}, PlotPoints -> 50, AxesLabel -> {"a", "b"}]


That tells you the minimum is likely close to 0. Now you change the PlotRange to have a better idea.

Plot3D[F[a, b], {a, -2 Pi, 2 Pi}, {b, -2, 2},
PlotRange -> {-2, 10}, PlotPoints -> 50, AxesLabel -> {"a", "b"}]


That shows you that your function is rather "un-convex" in that it has a number of local minimae. You can then move this graphic around to get a feel for where the absolute minimum is located and use that as a starting point of a local search. The point $$(-4.5,\,1.8)$$ seems about right. Then perform the local search using FindMinimum (a better choice because you can set a starting point, as opposed to NMinimize that has no such option):

sol = FindMinimum[F[a, b], {{a, -4.5}, {b, 1.8}}]
{a, b, F[a, b]} /. Last@sol
p = Point[{a, b, F[a, b]} /. Last@sol]
Show[g, Graphics3D[{Red, PointSize[.03], p}]]
*(
{-1.3495, {a -> -4.19328, b -> -1.76185}}
*)


(this is the value of $$F$$ and the optimal point). Then move the graphic around to make sure the local minimum is a global one (here you can trust the graphic because the function is regular enough):

Approach based on NMinimize

To add to my other answer, an option is to go with NMinimize using a method that performs well with rather non-convex functions such as RandomSearch.

F[a_, b_] := ((2 + Cos[3 a/2] Cos[a]) - (b)^2 + (((2 + Cos[3 a/2]) Sin[a])
-(b^2))^2) + (Sin[3 a/2] (b^3))^2;
NMinimize[F[a, b], {a, b}, Method -> "RandomSearch"]
(* {-1.3495, {a -> 20.9395, b -> 1.76185}} *)


Comment. As you can see this method yields the same optimal minimal value (-1.3495). The $$(a,b)$$ solution is different in $$a$$ but this is due to the fact that $$F$$ is $$a$$-periodic. However a search method with random starting point is only probably-effective, so there is no garantee that the solution is a global minimum. For that you need to examine the 3d-plot (see my other answer).

• NMinimize[F[a, b], {a, b}, Method -> {"DifferentialEvolution", "ScalingFactor" -> 3}] performs {-1.3495,{a->3552.09,b->-1.76185}}. May 19, 2021 at 18:47