cmcgrath
cmcgrath

Reputation: 87

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?

Upvotes: 5

Views: 6169

Answers (1)

Bill Bell
Bill Bell

Reputation: 21663

>>> 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]

Upvotes: 5

Related Questions