How to find the maximum and minimum of a function using Maxima?

nbro picture nbro · Apr 17, 2015 · Viewed 9.9k times · Source

I have trying to find a way to obtain the maximum and the minimum value of a function using Maxima (wxMaxima), but until now I have not found how to do it.

Could you please tell me how would you do it?

For example, suppose I have the following code:

f(x) := (3*x)/(x^2 - 2*x + 4);

And then I plot this function in the range -10, 10, and I obtain:

enter image description here

I know that the maximum is 3/2 and the minimum should be -1/2.

Answer

Robert Dodier picture Robert Dodier · Apr 17, 2015

My advice is to find the extreme values the same way you would do it by hand: compute the derivative, solve for derivative = 0, and substitute any values found back into the original function. E.g.:

(%i1) f(x) := (3*x)/(x^2 - 2*x + 4);
                                         3 x
(%o1)                        f(x) := ------------
                                      2
                                     x  - 2 x + 4
(%i2) diff (f(x), x);
                             3          3 x (2 x - 2)
(%o2)                   ------------ - ---------------
                         2               2           2
                        x  - 2 x + 4   (x  - 2 x + 4)
(%i3) ratsimp (%);
                                       2
                                    3 x  - 12
(%o3)                   - -----------------------------
                           4      3       2
                          x  - 4 x  + 12 x  - 16 x + 16
(%i4) num (%);
                                           2
(%o4)                              12 - 3 x
(%i5) solve (%, x);
(%o5)                          [x = - 2, x = 2]
(%i6) map (lambda ([e], subst (e, f(x))), %);
                                      1  3
(%o6)                              [- -, -]
                                      2  2

If I were being careful, I would have verified that x = -2 and x = 2 are indeed extreme values and not just inflection points, and I would have verified that the denominator of %o3 is nonzero at x = -2 and x = 2 before trying to evaluate f(x) at those points.