Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python求闭区间上函数的最小值_Python_Sympy - Fatal编程技术网

用Python求闭区间上函数的最小值

用Python求闭区间上函数的最小值,python,sympy,Python,Sympy,更新:如何在Python中找到闭合区间[0,3.5]上函数的最小值?到目前为止,我找到了最大值和最小值,但不确定如何从这里过滤出最小值 import sympy as sp x = sp.symbols('x') f = (x**3 / 3) - (2 * x**2) + (3 * x) + 1 fprime = f.diff(x) all_solutions = [(xx, f.subs(x, xx)) for xx in sp.solve(fprime, x)] print (al

更新:如何在Python中找到闭合区间[0,3.5]上函数的最小值?到目前为止,我找到了最大值和最小值,但不确定如何从这里过滤出最小值

import sympy as sp

x = sp.symbols('x')

f = (x**3 / 3) - (2 * x**2) + (3 * x) + 1

fprime = f.diff(x)

all_solutions = [(xx, f.subs(x, xx)) for xx in sp.solve(fprime, x)]

print (all_solutions)

以下是使用Symphy的可能解决方案:

import sympy as sp

x = sp.Symbol('x', real=True)

f = (x**3 / 3) - (2 * x**2) - 3 * x + 1
#f = 3 * x**4 - 4 * x**3 - 12 * x**2 + 3

fprime = f.diff(x)

all_solutions = [(xx, f.subs(x, xx)) for xx in sp.solve(fprime, x)]
interval = [0, 3.5]
interval_solutions = filter(
    lambda x: x[0] >= interval[0] and x[0] <= interval[1], all_solutions)

print(all_solutions)
print(interval_solutions)
将sympy作为sp导入
x=sp.Symbol('x',实=真)
f=(x**3/3)-(2*x**2)-3*x+1
#f=3*x**4-4*x**3-12*x**2+3
fprime=f.diff(x)
所有_解=[(xx,f.subs(x,xx))对于sp.solve中的xx(fprime,x)]
区间=[0,3.5]
区间解=滤波器(

lambda x:x[0]>=区间[0]和x[0]这里有一个使用sympy的可能解决方案:

import sympy as sp

x = sp.Symbol('x', real=True)

f = (x**3 / 3) - (2 * x**2) - 3 * x + 1
#f = 3 * x**4 - 4 * x**3 - 12 * x**2 + 3

fprime = f.diff(x)

all_solutions = [(xx, f.subs(x, xx)) for xx in sp.solve(fprime, x)]
interval = [0, 3.5]
interval_solutions = filter(
    lambda x: x[0] >= interval[0] and x[0] <= interval[1], all_solutions)

print(all_solutions)
print(interval_solutions)
将sympy作为sp导入
x=sp.Symbol('x',实=真)
f=(x**3/3)-(2*x**2)-3*x+1
#f=3*x**4-4*x**3-12*x**2+3
fprime=f.diff(x)
所有_解=[(xx,f.subs(x,xx))对于sp.solve中的xx(fprime,x)]
区间=[0,3.5]
区间解=滤波器(
λx:x[0]>=区间[0]和x[0]

f.subs命令显示两种显示给定函数在x=3.5时的值的方式,第一种是有理近似,第二种是精确分数

f.subs命令显示两种显示给定函数在x=3.5时的值的方式,第一种是有理近似,第二种是精确分数


也许是这样的

from sympy import solveset, symbols, Interval, Min
x = symbols('x')

lower_bound = 0
upper_bound = 3.5
function = (x**3/3) - (2*x**2) - 3*x + 1

zeros = solveset(function, x, domain=Interval(lower_bound, upper_bound))
assert zeros.is_FiniteSet # If there are infinite solutions the next line will hang.
ans = Min(function.subs(x, lower_bound), function.subs(x, upper_bound), *[function.subs(x, i) for i in zeros])

也许是这样的

from sympy import solveset, symbols, Interval, Min
x = symbols('x')

lower_bound = 0
upper_bound = 3.5
function = (x**3/3) - (2*x**2) - 3*x + 1

zeros = solveset(function, x, domain=Interval(lower_bound, upper_bound))
assert zeros.is_FiniteSet # If there are infinite solutions the next line will hang.
ans = Min(function.subs(x, lower_bound), function.subs(x, upper_bound), *[function.subs(x, i) for i in zeros])
因为您应该能够执行以下操作:

from sympy.calculus.util import *
f = (x**3 / 3) - (2 * x**2) - 3 * x + 1
ivl = Interval(0,3)
print(minimum(f, ivl))
print(maximum(f, ivl))
print(stationary_points(f, ivl))
因为您应该能够执行以下操作:

from sympy.calculus.util import *
f = (x**3 / 3) - (2 * x**2) - 3 * x + 1
ivl = Interval(0,3)
print(minimum(f, ivl))
print(maximum(f, ivl))
print(stationary_points(f, ivl))

这个解决方案是如何解释时间间隔的?@TashayGreen编辑了我的答案,为您提供了更多的线索。您能给我解释一下这个“#f=3*x4-4*x3-12*x**2+3”的位置吗来自?@TashayGreen这是另一个测试代码的函数,对吗?这个解决方案如何解释时间间隔?@TashayGreen编辑了我的答案,给你更多的线索。你能给我解释一下这个“#f=3*x4-4*x3-12*x**2+3”来自哪里吗?@TashayGreen这是另一个测试代码的函数,对吗?
[0,3,5]
自变量点?是否为
[0,3,5]
自变量点?您为该解决方案导入了哪些库?这里的所有内容都来自Symphy。您为该解决方案导入了哪些库?这里的所有内容都来自Symphy。