如何找到功能的最小值和最大值。我仅限于使用numpy,sympy和matplotlib

发布于 2025-02-12 14:04:41 字数 381 浏览 1 评论 0原文

我被要求找到少数功能的最大值和最小值。函数之一是y =(9x^3) - (7x^2) +(3x) + 10 到目前为止,我可以编写的代码是:

from sympy import*
import matplotlib.pyplot as plt

x = symbols ('x')
f = (9*x**3) - (7*x**2) + (3*x) + 10

intervals = np.arange(-5, 7)

df = diff(f, x)
df2 = diff(f, x, 2)

f = lambdify (x, f)
y = f(intervals)

print (intervals)
print (y)

我是使用这三个库的新手,所以我不知道如何使用这3个库找到答案

I am asked to find the maxima and minima of few functions. One of the functions is y = (9x^3) - (7x^2) + (3x) + 10
The code I could write so far is:

from sympy import*
import matplotlib.pyplot as plt

x = symbols ('x')
f = (9*x**3) - (7*x**2) + (3*x) + 10

intervals = np.arange(-5, 7)

df = diff(f, x)
df2 = diff(f, x, 2)

f = lambdify (x, f)
y = f(intervals)

print (intervals)
print (y)

I am new to using these 3 libraries so I dont know how to find the answer using these 3 libraries

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷夜 2025-02-19 14:04:41

Sympy可以告诉您派生和f,可以告诉您何时函数为零。由于最大/分钟发生在衍生物为零时,您可以找到零,然后确定这些值是否使第二个衍生物正阳性或负面,例如

>>> from sympy import real_roots
>>> from sympy.abc import x
>>> f = -x**2 + 1
>>> d1 = f.diff(x)
>>> d2 = d1.diff(x)  # = f.diff(x,2)
>>> extrema = real_roots(d1)
>>> for i in extrema:
...   if d2.subs(x, i).is_positive:
...      print('minimum',i)
...   else:
...      print('maxima',i)

另请参见在这里。 (如果没有真正的根,那么x的真实值就没有极点。)

SymPy can tell you the derivative and f and can tell you when a function is zero. Since max/min occur when the derivative is zero you can find the zeros and then determine if those values make the 2nd derivative positive or negative, e.g.

>>> from sympy import real_roots
>>> from sympy.abc import x
>>> f = -x**2 + 1
>>> d1 = f.diff(x)
>>> d2 = d1.diff(x)  # = f.diff(x,2)
>>> extrema = real_roots(d1)
>>> for i in extrema:
...   if d2.subs(x, i).is_positive:
...      print('minimum',i)
...   else:
...      print('maxima',i)

See also here. (If there are no real roots then there are no extrema for real values of x.)

满身野味 2025-02-19 14:04:41

如果要将间隔的值传递到公式中,然后获取最小值,则可以首先指定Intervals,然后将此数组放入公式中而不是将其放入公式<代码> x :

intervals = np.random.permutation(10)
# [8 4 3 0 2 7 9 1 6 5]

f = (9 * intervals ** 3) - (7 * intervals ** 2) + (3 * intervals) + 10
# [4194  486  199   10   60 2775 6031   15 1720  975]

f.argmin()    # --> will get index of the minimum value
# 3

f.min()       # --> will get minimum value resulted by the formula
# 10

或者,如果要多次使用公式,则可以将其定义为函数,然后每次需要将其调用,而是再次编写为:

def formula_1(x):
    return (9 * x ** 3) - (7 * x ** 2) + (3 * x) + 10


results = formula_1(intervals)
results.min()

If you want to pass the values of intervals into the formula and then get the minimum value, you can specify the intervals firstly and then just put this array into the formula instead x:

intervals = np.random.permutation(10)
# [8 4 3 0 2 7 9 1 6 5]

f = (9 * intervals ** 3) - (7 * intervals ** 2) + (3 * intervals) + 10
# [4194  486  199   10   60 2775 6031   15 1720  975]

f.argmin()    # --> will get index of the minimum value
# 3

f.min()       # --> will get minimum value resulted by the formula
# 10

or if you want to use the formula many times, you can define it as a function and just call it every time you need instead writing it again as:

def formula_1(x):
    return (9 * x ** 3) - (7 * x ** 2) + (3 * x) + 10


results = formula_1(intervals)
results.min()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文