Given the function fun1[a, b, x] , I want minimize this function over $a$ and $b$ such that $0\le a \le 2\pi$ and $0\le b\le 2\pi$, and plot the resulting function w.r.t. variable $x$. The following attempt doesn't seem to work:

  fun1[a_, b_, x_] = -2 + Sqrt[1 + x^2 - 2 x Cos[a]] + Sqrt[
       2 - 2 Cos[b]] + Sqrt[1 + x^2 + 2 x Cos[a + b]];
   fun2[x_] := fun1[a, b, x] /. Last[NMinimize[fun1[a, b, x], {a, b}]]
    tab = Table[fun2[x], {x, 0, 1, 0.001}];

Further, I need to find the values of $a$ and $b$ for which fun1[a, b, x]is less or equal to zero.


1 Answer 1


You should pattern test the arguments for NumericQ. The replacement is unnecessary - the first element of the result of NMinimize is this minimum of the function. In your plot you probably want to plot {x,fun2[x]} if you want to pick up the x-coordinates too. I used ParallelTable to generate the results in parallel. And finally, I noticed also that NMinimize has trouble converging, so I added MaxIterations -> 200:

fun1[a_?NumericQ, b_?NumericQ, x_?NumericQ] := 
   -2 + Sqrt[1 + x^2 - 2 x Cos[a]] + Sqrt[2 - 2 Cos[b]] + 
   Sqrt[1 + x^2 + 2 x Cos[a + b]];

fun2[x_?NumericQ] := First[NMinimize[fun1[a, b, x], {a, b}, MaxIterations -> 200]]

tab = ParallelTable[{x, fun2[x]}, {x, 0, 1, 0.001}];
ListPlot[tab, PlotRange -> Full]

fun2 minimize plot


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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