Question: Optimization, function and differentiability

Hello,

I am experiencing some trouble with the function Minimize of the Optimization package. I have some trouble understanding what Maple is doing with nice functions already.

f := (x-2)^2+1;
                                 2    
                          (x - 2)  + 1
Minimize(f, {x >= -2, x <= 2});
             [1., [x = HFloat(1.9999999999999998)]]
Minimize(f(x), {x >= -2, x <= 2});
                    [1., [x = HFloat(2.0)]]
g := proc (x) options operator, arrow; (x-2)^2+1 end proc;
            2    
x -> (x - 2)  + 1
Minimize(g, {x >= -2, x <= 2});
Error, (in Optimization:-NLPSolve) constraints must be specified as a set or list of  procedures
Minimize(g(x), {x >= -2, x <= 2});
             [1., [x = HFloat(1.9999999999999998)]]


I don't quite understand why the second one would even work as I'm feeding f(x) while f is an expression, and I don't understand why it would give a different result with f and f(x). If someone has an explanation, that's my first question!

Second, I wanted to work with say, piecewise functions that might not be differentiable, and it gets even weirder.

f := piecewise(x < -2, 1/x, x >= -2, x+2-1/2);
                       /        1               3\
              piecewise|x < -2, -, -2 <= x, x + -|
                       \        x               2/
Minimize(f, {x >= -2, x <= 2});
Error, (in Optimization:-NLPSolve) no improved point could be found
Minimize(f(x), {x >= -2, x <= 2});
          [-0.500000000000000000, [x = HFloat(-2.0)]]
g := proc (x) options operator, arrow; piecewise(x < -2, 1/x, -2 <= x, x+3/2) end proc;
              /        1               3\
x -> piecewise|x < -2, -, -2 <= x, x + -|
              \        x               2/
Minimize(g, {x >= -2, x <= 2});
Error, (in Optimization:-NLPSolve) constraints must be specified as a set or list of  procedures
Minimize(g(x), {x >= -2, x <= 2});
Error, (in Optimization:-NLPSolve) no improved point could be found

 

Only the expression, used as a function would work. I'm baffled, does anyone have an explanation?

Sincerely

Please Wait...