如何在python中求解3个非线性方程 [英] how to solve 3 nonlinear equations in python
问题描述
我有以下三个需要求解的非线性方程组:
I have the following system of 3 nonlinear equations that I need to solve:
-xyt + HF = 0
-xyt + HF = 0
-2xzt + 4yzt-xyt + 4z ^ 2t-M1F = 0
-2xzt + 4yzt - xyt + 4z^2t - M1F = 0
-2xt + 2yt + 4zt-1 = 0
-2xt + 2yt + 4zt - 1 = 0
其中x,HF和M1F是已知参数.因此,y,z和t是要计算的参数.
where x, HF, and M1F are known parameters. Therefore, y,z, and t are the parameters to be calculated.
尝试解决该问题:
def equations(p):
y,z,t = p
f1 = -x*y*t + HF
f2 = -2*x*z*t + 4*y*z*t - x*y*t + 4*t*z**2 - M1F
f3 = -2*x*t + 2*y*t + 4*z*t - 1
return (f1,f2,f3)
y,z,t = fsolve(equations)
print equations((y,z,t))
但是问题是,如果我想使用scipy.optimize.fsolve
,那么我应该输入一个初始猜测.就我而言,我没有任何初始条件.
But the thing is that if I want to use scipy.optimize.fsolve
then I should input an initial guess. In my case, I do not have any initial conditions.
还有另一种方法可以在python中求解具有3个未知数的3个非线性方程吗?
Is there another way to solve 3 nonlinear equations with 3 unknowns in python?
原来,我有病!条件是HF> M1F,HF> 0和M1F> 0.
It turned out that I have a condition! The condition is that HF > M1F, HF > 0, and M1F > 0.
推荐答案
@Christian,与您建议的帖子不同,我认为方程组不容易线性化.
@Christian, I don't think the equation system can be linearize easily, unlike the post you suggested.
鲍威尔的混合方法(optimize.fsolve()
)对初始条件非常敏感,因此,如果您能提出一个很好的初始参数猜测,这将非常有用.在下面的示例中,我们首先使用Nelder-Mead方法(optimize.fmin()
,对于像OP这样的小问题,这已经足够了)最小化所有三个方程的平方和.然后将所得的参数向量用作optimize.fsolve()
的初始猜测,以获得最终结果.
Powell's Hybrid method (optimize.fsolve()
) is quite sensitive to initial conditions, so it is very useful if you can come up with a good initial parameter guess. In the following example, we firstly minimize the sum-of-squares of all three equations using Nelder-Mead method (optimize.fmin()
, for small problem like OP, this is probably already enough). The resulting parameter vector is then used as the initial guess for optimize.fsolve()
to get the final result.
>>> from numpy import *
>>> from scipy import stats
>>> from scipy import optimize
>>> HF, M1F, x=1000.,900.,10.
>>> def f(p):
return abs(sum(array(equations(p))**2)-0)
>>> optimize.fmin(f, (1.,1.,1.))
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 131
Function evaluations: 239
array([ -8.95023217, 9.45274653, -11.1728963 ])
>>> optimize.fsolve(equations, (-8.95023217, 9.45274653, -11.1728963))
array([ -8.95022376, 9.45273632, -11.17290503])
>>> pr=optimize.fsolve(equations, (-8.95023217, 9.45274653, -11.1728963))
>>> equations(pr)
(-7.9580786405131221e-13, -1.2732925824820995e-10, -5.6843418860808015e-14)
结果非常好.
这篇关于如何在python中求解3个非线性方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!