gpt4 book ai didi

python - 使用python求解嵌套函数(循环方程)?

转载 作者:行者123 更新时间:2023-12-04 14:53:54 24 4
gpt4 key购买 nike

是否可以使用 python 查找嵌套函数的局部最大值/最小值?

例如f(a,b) = a*5/(a-b)并嵌套它们 -> f( f( f(a, b), b ), b)

嵌套需要动态创建,例如在 for 循环中:

def f(a,b):
return a*5 / (a-b)

input = 1
for x in range(3):
input = f(input, b)

这很简单,但我可以从中得到一个方程式,然后使用 Numpy/Sympy/SciPy 或其他东西来找到给定输入范围的局部 maxima/minima

谢谢!

最佳答案

首先得到你的等式很简单

import sympy

a, b = sympy.symbols('a, b')

def f(a, b):
return a*5 / (a-b)

g = a
for x in range(3):
g = f(g, b)

g = sympy.simplify(g)

给予

simplified nested function

然后您可以像这样计算导数并找到它为零的位置

dg_a = sympy.diff(function, a)
dg_b = sympy.diff(function, a)

sols = sympy.solve([dg_a, dg_b],[a,b])

对我来说,这给出了 (a,0)。不幸的是,当像这样计算 Hessian 矩阵时:

sympy.lambdify([a,b],sympy.matrices.Matrix([[dg_a, dg_b]]).jacobian([a,b]))(a,0)

我们得到零矩阵所以

  1. If D(a, b) = 0 then the second derivative test is inconclusive, andthe point (a, b) could be any of a minimum, maximum or saddle point.

参见 https://en.wikipedia.org/wiki/Second_partial_derivative_test

因此,如果您想证明这些是最大值/最小值,则必须进一步调查。但我相信这超出了您的要求。

关于python - 使用python求解嵌套函数(循环方程)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68518002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com