Message Boards Message Boards


Bad behavior of NMinimize for discontinuous functions

Posted 9 years ago
10 Replies
0 Total Likes


I have encountered a weird behavior of NMinimize for discontinuous functions. Consider the following piece of code:

f[x_] := Piecewise[{{0, 0 <= x <=  Pi/3}, {Pi/3, 
    Pi/3 <  x <=  2 Pi/3}, {2 Pi/3, 
    2 Pi/3 <  x <=  3 Pi/3} , {3 Pi/3, 
    3 Pi/3 <  x <= 4 Pi/3}, {4 Pi/3, 4 Pi/3 <  x <= 5 Pi/3}, {5 Pi/3, 
    5 Pi/3 <  x <= 6 Pi/3} }]
parameters = Flatten[Table[c[i], {i, 1, 5}]];
constraints = Flatten[Table[0 <= c[i] <=  2 Pi, {i, 1, 5}]];
CycleConstr = {c[1] == c[5]};

   Cos[c[i]] (Cos[f[c[i]]] - Cos[f[c[i + 1]]]) + 
    Sin[c[i]] (Sin[f[c[i]]] - Sin[f[c[i + 1]]]), {i, 1, 4}], 
  constraints, CycleConstr}, parameters]

The output: {0., {c[1] -> 3.47562, c[2] -> 3.58266, c[3] -> 4.15874, c[4] -> 3.52307, c[5] -> 3.47562}}

However, it is easy to check that the minimum is -1, which the direct substitution shows

   Cos[c[i]] (Cos[f[c[i]]] - Cos[f[c[i + 1]]]) + 
    Sin[c[i]] (Sin[f[c[i]]] - Sin[f[c[i + 1]]]), {i, 1, 4}] /. {
   c[1] -> Pi/3,
   c[2] -> 2 Pi/3,
   c[3] -> 3 Pi/3,
   c[4] -> 5 Pi/3,
   c[5] -> Pi/3

I would like to play with the above example. So if anyone has any thoughts how to find the minimum with Mathematica functions, I would very much appreciate.

POSTED BY: Alexey Kushnir
10 Replies

It should be!

POSTED BY: Alexey Kushnir

I've often wondered why the info in that reference isn't in the NMinimize documentation.

POSTED BY: Frank Kampas

The two methods have a number of options which you can adjust for better results.

POSTED BY: Frank Kampas

Thank you. This references is VERY helpful!

POSTED BY: Alexey Kushnir

You might get better results with Method -> "SimulatedAnnealing" or Method -> "DifferentialEvolution"

POSTED BY: Frank Kampas

Thank you! Results are not fantastic, but it gets closer to the minimum.

POSTED BY: Alexey Kushnir

It would be better if you put your Mathematica code into a code sample block, so it can be copied and pasted into Mathematica without error.

POSTED BY: Frank Kampas

Thank you for your interest. That is my first post and I did not knew about a code sample block. I updated post accordingly.

POSTED BY: Alexey Kushnir

Try to apply NMinimize separately to each piece of f[x]. Then choose global minimum result.

POSTED BY: S M Blinder

I am not sure, what you mean. My function depends on 5 parameters that should be within [0,2Pi]. I am not sure how you propose to minimize for each interval separately

POSTED BY: Alexey Kushnir
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract