# [Maxima] find minima and maxima points of a function

Wolfgang Lindner LindnerW at t-online.de
Mon Feb 2 14:35:57 CST 2009

```hi Luigi,

thanks for sharing your code with us. I enjoy it.
Find attached some code examples of Maxima friends (Nelson and Mario) for
the same job.
Maybe you find it interesting, too.
My class and me like it ..

hth
wolfgang

/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/

/* [wxMaxima: input   start ] */
/* -------------------------------------------------------------------------
-
this is file critpts.mac:
as you can see, comments in maxima are like comments in C

Nelson Luis Dias, nldias at simepar.br
created 20000707
updated 20000707
The program is written outside of MAXIMA with a text editor,
and then loaded into MAXIMA with the BATCH command.
Here is the program listing:
-------------------------------------------------------------------------
-- */

critpts():=(
print("program to find critical points"), /* asks for a function  */
f:read("enter f(x,y)"),                   /* echoes it, to make sure */
print("f = ",f),  /* produces a list with the two partial derivatives of
f */
eqs:[diff(f,x),diff(f,y)],                /* produces a list of unknowns
*/
unk:[x,y],                                /* solves the system */
solve(eqs,unk) )\$

print("z.B.: %e^(x^3+y^2)*(x+y);")\$

critpts();
/* [wxMaxima: input   end   ] */

/* [wxMaxima: input   start ] */
%e^(x^3+y^2)*(x+y);
/* [wxMaxima: input   end   ] */

/* Maxima can't load/batch files which end with a comment! */
"Created with wxMaxima"\$

---------

/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created by wxMaxima version 0.7.6 ] */

/* [wxMaxima: comment start ]
I am trying to figure out how to find the min/max of a
threee-dimensional function with Maxima.
For instance, I am playing with the function f(x,y)=x^3+3*x*y-6*x*y
On paper I can easily find f'x f''xx f'y f''yy f''xy and find the
critical points, then check each one. Is there a simpler way to do
(though there is a lot of good info about Maxima floating around the
'net).
Just to prove that I'm not asking someone to do my homework (and yes,
this _is_ a homework question), the solution set is {(0,0,), (0,2),
(1,1), (-1,1)}.
[wxMaxima: comment end   ] */

/* [wxMaxima: comment start ]
/*************  begin Maxima Session  ********************/
/* Analiza los puntos criticos de una funcion de  */
/* variables independientes x e y.                */
/* Ejemplos de uso:                               */
/*     extremos(exp(x-y)*x*y);                    */
/*     extremos(y^2 + (x + 1)^2*y + (x + 1)^4);   */
/* Mario Rodriguez */
[wxMaxima: comment end   ] */

/* [wxMaxima: input   start ] */
/* [wxMaxima: input   end   ] */

/* [wxMaxima: input   start ] */
extremos(expr):=
block([gr,cr,he,x,y,mn,de],

/* vector gradiente de la funcion */

/* hessiano */
he: hessian(expr, [x,y]),

/* Resuelve para x e y el gradiente nulo */
cr: solve(ev(express(gr),diff), [x,y]),

/* s recorre las soluciones encontradas por solve */
for s in cr do (

/* valor numerico del hessiano para la solución s */
mn: subst(s, he),

/* determinante del hessiano */
de: determinant(mn),

/* controla el tipo de punto critico */
if de > 0
then if mn[1,1] > 0
then print("Minimo relativo en ", s)
else print("Maximo relativo en ", s)
else if de < 0
then print("Punto silla en ", s)
else print("Determinante nulo en ", s) ) )\$
/* [wxMaxima: input   end   ] */

/* [wxMaxima: input   start ] */
f(x, y) := x^3+3*x*y-6*x*y \$
/* [wxMaxima: input   end   ] */

/* [wxMaxima: input   start ] */
extremos(f(x,y));
/* [wxMaxima: input   end   ] */

/* [wxMaxima: input   start ] */
/* [wxMaxima: input   end   ] */

/* [wxMaxima: input   start ] */
draw3d(
explicit(x^3+3*x*y-6*x*y, x,-3,3,y,-3,3),
point_size = 3, color = red, point_type = filled_circle,
points([[0,0,f(0, 0)],[0,2,f(0, 2)],[1,1,f(1, 1)],[-1,1,f(-1, 1)]]))\$
/* [wxMaxima: input   end   ] */

/* Maxima can't load/batch files which end with a comment! */
"Created with wxMaxima"\$

-----Ursprüngliche Nachricht-----
Von: Luigi Marino <luigi_marino2 at alice.it>
An: maxima at math.utexas.edu <maxima at math.utexas.edu>
Datum: Montag, 2. Februar 2009 15:52
Betreff: [Maxima] find minima and maxima points of a function

Last my work for Maxima, the block
find minima and maxima points of a function.
The block use solve of Maxima and points are
discovered if solve works.

stud_funz(f):=block([p,fs,fp],
p:solve(diff(f,x,1),x),
fs:diff(f,x,2),
fp:ev(fs,x:p),
for i:1 thru length(float(p)) do
if rhs(fp[i])>0 then print("minimo",[p[i],rhs(ev(f,x:p[i]))])
else print("massimo",[p[i],rhs(ev(f,x:p[i]))]))\$

Example:

f:x^3/(1-x^2);
stud_funz(f);
minimo[x=-sqrt(3),(3*sqrt(3))/2]
massimo[x=sqrt(3),-(3*sqrt(3))/2]
massimo[x=0,0]

f:x*sqrt(1-x^2);
stud_funz(f);
minimo[x=-1/sqrt(2),-1/2]
massimo[x=1/sqrt(2),1/2]

f:trigexpand(cos(2*x)+2*sin(x));
stud_funz(f);
`solve' is using arc-trig functions to get a solution.
Some solutions will be lost.
massimo[x=%pi/6,0]
massimo[x=%pi/2,0]

Best wishes.
Luigi Marino

```