TransWikia.com

Optimization problem over two variables

Mathematica Asked on February 25, 2021

Given the function fun1[a, b, x] , I want minimize this function over $a$ and $b$ such that $0le a le 2pi$ and $0le ble 2pi$, 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}];
    ListPlot[tab]

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

One Answer

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

Answered by flinty on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP