[英]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: 我正在尝试学习sympy的微积分函数,并且能够通过以下方式获得关于极值临界点的二阶导数的根:
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? 如何将从dRoots获得的值替换为原始方程式f,并使其输出值列表?
>>> 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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.