gpt4 book ai didi

python - 在Python中求解动态数非线性方程

转载 作者:太空宇宙 更新时间:2023-11-03 19:20:06 30 4
gpt4 key购买 nike

Scipy 中的

Fsolve 似乎是合适的人选,我只需要动态传递方程的帮助。我很感激提前的任何想法。

动态我的意思是每次运行的方程数量不同,例如我遇到的一种情况:

alpha*x + (1-alpha)*x*y - y = 0
beta*x + (1- beta)*x*z - z = 0
A*x + B*y + C*z = D

还有我遇到的另一种情况:

alpha*x + (1-alpha)*x*y - y = 0
beta*x + (1- beta)*x*z - z = 0
gama*x + (1 -gama)*x*w - w =0
A*x + B*y + C*z + D*w = E

alphabetaABC >DE 都是常量。 xyzw 是变量。

最佳答案

我自己没有使用过 Fsolve,但根据其文档,它需要一个可调用函数。像这样的东西可以处理具有未知数量变量的多个函数。请记住,此处的参数必须正确排序,但每个函数只需要一个列表。

def f1(argList):
x = argList[0]
return x**2
def f2(argList):
x = argList[0]
y = argList[1]
return (x+y)**2
def f3(argList):
x = argList[0]
return x/3

fs = [f1,f2,f3]
args = [3,5]
for f in fs:
print f(args)

对于 Fsolve,您可以尝试这样的操作(未经测试):

def func1(argList, constList):
x = argList[0]
y = argList[1]
alpha = constList[0]
return alpha*x + (1-alpha)*x*y - y
def func2(argList, constList):
x = argList[0]
y = argList[1]
z = argList[2]
beta = constList[1]
return beta*x + (1- beta)*x*z - z
def func3(argList, constList):
x = argList[0]
w = argList[1] ## or, if you want to pass the exact same list to each function, make w argList[4]
gamma = constList[2]
return gama*x + (1 -gama)*x*w - w
def func4(argList, constList):

return A*x + B*y + C*z + D*w -E ## note that I moved E to the left hand side


functions = []
functions.append((func1, argList1, constList1, args01))
# args here can be tailored to fit your function structure
# Just make sure to align it with the way you call your function:
# args = [argList, constLit]
# args0 can be null.
functions.append((func1, argList2, constList2, args02))
functions.append((func1, argList3, constList3, args03))
functions.append((func1, argList4, constList4, args04))

for func,argList, constList, args0 in functions: ## argList is the (Vector) variable you're solving for.
Fsolve(func = func, x0 = ..., args = constList, ...)

关于python - 在Python中求解动态数非线性方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10032396/

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