# How to write a condition for FindMinimum correctly

I would like that, when finding the minimum, the FindMinimum analyzes the variables (x and y) each time and finds q, depending on the current values of x and y. Can I somehow write this condition into FindMinimum?

I have a function:

F[x_, y_] = (x - 3)^4 + y^2 + q;


where the value of q is determined from the condition:

If[x <= y, q = 10*y, q = 10*x]


How can I write this condition into a FindMinimum?

I am trying to do it in the following way:

FindMinimum[{F = F[x, y], If[x <= y, q = y, q = x]}, {{x, 1}, {y, 1}},
StepMonitor :> {Print[" Current x=", x, " y=", y, " q=", q, " F=",
F]}]


but Mathematica gives an error.

What is the correct form?

• Why don't you add the explicit form of q in the definition of F[x, y] ? Sep 12, 2022 at 10:36
• Your function does not have a minimum. If y-> -Infinity the function value goes to -Infinity Sep 12, 2022 at 10:38
• Thank you. I know the problem is in the condition record. I have corrected the function. Sep 12, 2022 at 10:51
• But q depend on x or y Sep 12, 2022 at 10:57

I would do:

f[x_, y_] = (x - 3)^4 + y^2 + 10 Max[x, y];

Plot3D[f[x, y], {x, -5, 5}, {y, -5, 5}] FindMinimum[{f[x, y]}, {{x, 1}, {y, 1}},
StepMonitor :> {Print[" Current x=", x, " y=", y, " q=", 10 Min[x, y], " F=", f[x, y]]}]
(* {19.8209, {x -> 1.64279, y -> -8.25146*10^-10}} *)


Notice you could also find an analytical solution (remove N to see it):

Minimize[f[x, y], {x, y}, Reals] // N
(* {19.8209, {x -> 1.64279, y -> 0.}} *)

• Thank you very much, it works! Is your entry equivalent to this? F[x_, y_] = If[x >= y, (x - 3)^4 + y^2 + 10*x, (x - 3)^4 + y^2 + 10*y]; Sep 12, 2022 at 11:34
• Dear, @MarcoB, could you help me with this question (mathematica.stackexchange.com/questions/276990/…) ? Sorry, but I do not know how to contact you directly, so I am writing through this post. Dec 7, 2022 at 8:05
• Dear, @b.gates.you.know.what, could you help me with this question (mathematica.stackexchange.com/questions/276990/…) ? Sorry, but I do not know how to contact you directly, so I am writing through this post. Dec 7, 2022 at 8:05
• Add a variable q and change the condition from = to ==.
Clear[F, x, y, q];
F[x_, y_] = (x - 3)^4 + y^2 + q;
FindMinimum[{F[x, y],
If[x <= y, q == 10*y, q == 10*x]}, {{x, 1}, {y, 1}, q},
StepMonitor :> {Print[" Current x=", x, " y=", y, " q=", q, " F=",
F[x, y]]}] • Test another complex cases.
Clear[F, x, y, q];
F[x_, y_] = (x - 3)^4 + y^2 + q;
FindMinimum[{F[x, y],
If[x <= 1 + y, Exp@q == 10*y + q*x,
Sin@q + Cos@q == 10*x + q*y]}, {{x, 1}, {y, 1}, q},
StepMonitor :> {Print[" Current x=", x, " y=", y, " q=", q, " F=",
F[x, y]]}]

\$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global*"]

q[x_, y_] = Piecewise[{{10 y, x <= y}}, 10 x];

F[x_, y_] = (x - 3)^4 + y^2 + q[x, y];


The exact values are

{min, arg} = Minimize[F[x, y], {x, y}] /.

{min, arg} // N
`