gpt4 book ai didi

python - fsolve 对任何方程组都有效吗?

转载 作者:行者123 更新时间:2023-11-30 21:57:50 24 4
gpt4 key购买 nike

我没有太多使用 Python 的经验,但我决定尝试用它来求解以下方程组:

x = A * exp (x+y)

y = 4 * exp (x+y)

我想求解这个系统并绘制 x 和 y 作为 A 的函数。

我看到了一些类似的question并尝试一下 fsolve:

`from scipy.optimize import fsolve
def f(p):
x, y = p
A = np.linspace(0,4)
eq1= x -A* np.exp(x+y)
eq2= y- 4* np.exp(x+y)
return (eq1,eq2)
x,y = fsolve(f,(0, 0))
print(x,y)
plt.plot(x,A)
plt.plot(y,A)
`

我收到这些错误:使用序列设置数组元素。函数调用的结果不是正确的 float 组。

最佳答案

将 A 的值作为参数传递给函数,并分别对 A 的每个值运行 fsolve。以下代码有效。

from scipy.optimize import fsolve
import matplotlib.pyplot as plt
import numpy as np
def f(p,*args):
x, y = p
A = args[0]

return (x -A* np.exp(x+y),y- 4* np.exp(x+y))
A = np.linspace(0,4,5)
X = []
Y =[]
for a in A:
x,y = fsolve(f,(0.0, 0.0) , args=(a))
X.append(x)
Y.append(y)
print(x,y)

plt.plot(A,X)
plt.plot(A,Y)

4.458297786441408e-17 -1.3860676807976662
-1.100088440495758 -0.5021704548996653
-1.0668987418054918 -0.7236105952221454
-1.0405000943788385 -0.9052366768954621
-1.0393471472966025 -1.0393471472966027
/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py:163: RuntimeWarning: The iteration is not making good progress, as measured by the
improvement from the last ten iterations.
warnings.warn(msg, RuntimeWarning)
/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py:163: RuntimeWarning: The iteration is not making good progress, as measured by the
improvement from the last five Jacobian evaluations.
warnings.warn(msg, RuntimeWarning)
[<matplotlib.lines.Line2D at 0x7f4a2a83a4e0>]

enter image description here

关于python - fsolve 对任何方程组都有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55128989/

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