8

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.

nbro
  • 15,395
  • 32
  • 113
  • 196

4 Answers4

12

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.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • 3
    +1. I especially liked the `num(%)` step. It is not strictly necessary but it helps maxima not to choke with the `solve` step. I got to remember that (I often end up copying the numerator by hand... silly me - thanks) – Rolazaro Azeveires Mar 21 '16 at 21:49
1

Use the function lbfgs like this :

lbfgs(-f(x), [x], [1.0], 1e-4, [-1,0]);

The code above gives the position [x = 2] of the maximum of the function.

nbro
  • 15,395
  • 32
  • 113
  • 196
wimmer
  • 11
  • 1
  • 5
    Please, explain why should this work, what's the purpose of `lbfgs`, etc. – nbro Mar 21 '16 at 18:09
  • the answer would be better with a bit of description, but together with [the doc](http://maxima.sourceforge.net/docs/manual/de/maxima_56.html) it helped, found zero of Bessel function (first kind, index 0) like this: `lbfgs((bessel_j(0,x))^2, [x], [2.4], 1e-4, [-1,0]);` – xealits Aug 26 '18 at 17:54
  • plus 1 for function, minus 1 for no explanation – endolith Mar 11 '19 at 17:28
  • Also I'm having better luck with `fmin_cobyla` as you can increase `maxfun` etc. – endolith Mar 12 '19 at 16:51
1

enter image description here

Identify when the derivative is increasing or decreasing

nightcrawler
  • 275
  • 5
  • 16
  • Please, at least, roughly explain your picture/code. It may also be useful to share the code/commands if one wants to easily reproduce your picture. – nbro Nov 04 '17 at 18:10
0

lbfgs finds the minimum value.

Tim's code finds the minimum value of -f(x), and thus max value of f(x). [x] = the variable. [1.0] = initial estimate. [-1,0] = finding the max value between x = -1 and x = 0. 1e-4 = epsilon, I think basically this ends up being the level of accuracy or step value.

nbro
  • 15,395
  • 32
  • 113
  • 196
Nate
  • 1