# Symbolic calculus of extrema

Hi!

Is there any way in Sagemath to find symbolically the local extrema of a given function?

EDIT: As an example one may be interested in the extrema of the following differentiable function:

K=var('K');
l=var('l');
x=var('x');
z(x)=(cosh(-1/2*K*l+K*x)-cosh(1/2*K*l))/K; #Given function

edit retag close merge delete

Sort by » oldest newest most voted

Given the example, it is pretty easy:

sage: K=var('K');
....: l=var('l');
....: x=var('x');
....: z(x)=(cosh(-1/2*K*l+K*x)-cosh(1/2*K*l))/K;


The derivative of z (with respect to the variable x) is:

sage: diff(z(x),x)
sinh(-1/2*K*l + K*x)


Since the only zeros of the sinh function is zero, solving $z'(x)=0$ is equivalent to solving $-1/2Kl + K*x=0$:

sage: solve(-1/2*K*l + K*x == 0, x)
[x == 1/2*l]


EDIT Actually Sage knows about sinh:

sage: solve(diff(z(x),x)==0,x)
[x == 1/2*l]


So, if there is an extrema, it can only be at $x=l/2$, but it is not sure yet.

Now, since sinh changes its sign at 0, and since -1/2*K*l + K*x is affine, z' changes its sign at l/2 (when K is nonzero), hence the point $x=l/2$ is an extrema (hence, the only one). The nature of the extrema depends on the sign of K.

You can check with different values of K and l, e.g.:

sage: z.subs(K=2,l=2).plot([-1,2])
sage: z.subs(K=-2,l=2).plot([-1,2])
sage: z.subs(K=1,l=3).plot([-1,3])
sage: z.subs(K=-1,l=3).plot([-1,3])

more

(Or describe somehow the class of functions which are relevant for the intended application. Is e.g. the function a polynomial / a rational function?)

If it is differentiable, then sage can calculate the first relevant derivatives. The human hand can finally simply solve the puzzle.

For instance:

sage: f(x) = x^3 + 6*x^2 + 10*x + 6
sage: extList = diff(f(x),x).roots( ring=AA, multiplicities=0 )
sage: extList
[-2.816496580927726?, -1.183503419072274?]
sage: for ext in extList:
....:     print """x=%s f(x)=%s f"(x)=%s""" % (ext, f(ext), diff(f(x),x,2)(x=ext))
....:
x=-2.816496580927726? f(x)=3.088662107903635? f"(x)=-4.898979485566356?
x=-1.183503419072274? f(x)=0.911337892096366? f"(x)=4.898979485566356?

more