7
$\begingroup$

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?

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

3 Answers 3

7
$\begingroup$

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}]

enter image description here

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.}} *)
$\endgroup$
3
  • $\begingroup$ 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]; $\endgroup$
    – Mam Mam
    Sep 12, 2022 at 11:34
  • $\begingroup$ 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. $\endgroup$
    – Mam Mam
    Dec 7, 2022 at 8:05
  • $\begingroup$ 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. $\endgroup$
    – Mam Mam
    Dec 7, 2022 at 8:05
6
$\begingroup$
  • 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]]}]

enter image description here

  • 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]]}]
$\endgroup$
1
3
$\begingroup$
$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}] /.
   expr_Root :> ToRadicals[expr] // Simplify

(* {30 - 15/2 (5/2)^(1/3), {x -> 3 - (5/2)^(1/3), y -> 0}} *)

The approximate values are

{min, arg} // N

(* {19.8209, {x -> 1.64279, y -> 0.}} *)
$\endgroup$

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.