简体   繁体   中英

Python - Sympy Minima and Maxima

I'm trying to learn sympy's calculus functions and I'm able to get as far as getting the roots of the second derivative for the critical points of the extrema via:

import numpy as np
    from numpy import linspace, math, arange, linspace
    from sympy import *
    import sympy as sp
    import math

    x = Symbol('x')
    f = (x**4) - (24*x**2) + 80  
    fd = diff(f)
    fdd = diff(fd)
    print(fd)
    print(fdd)

    polyRoots = solveset(f,x)
    dRoots = solveset(fd,x) #gets critical x values
    ddRoots = solveset(fdd,x)

How do I substitute the values I get from dRoots into the original equation, f, and have it output a list of values?

>>> from sympy import *
>>> x = Symbol('x')
>>> f = x**4 - 24*x**2 + 80
>>> fd = diff(f)
>>> fdd = diff(fd)
>>> polyRoots = solveset(f, x)
>>> dRoots = solveset(fd, x)
>>> ddRoots = solveset(fdd, x)
>>> dRoots
{0, -2*sqrt(3), 2*sqrt(3)}
>>> [_ for _ in dRoots]
[0, -2*sqrt(3), 2*sqrt(3)]
>>> [f.subs(x, _) for _ in dRoots]
[80, -64, -64]

You can verify that this makes sense by doing:

>>> [f.subs(x, _) for _ in polyRoots]
[0, 0, 0, 0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM