如何使用Python求解一对非线性方程?
使用 Python 求解一对非线性方程的(最佳)方法是什么。(Numpy、Scipy 或 Sympy)
例如:
- x+y^2 = 4
- e^x+ xy = 3
解决上述问题的代码片段会很棒
What's the (best) way to solve a pair of non linear equations using Python. (Numpy, Scipy or Sympy)
eg:
- x+y^2 = 4
- e^x+ xy = 3
A code snippet which solves the above pair will be great
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对于数值求解,您可以使用 fsolve:
http://docs.scipy.org/doc/scipy/reference/ generated/scipy.optimize.fsolve.html#scipy.optimize.fsolve
for numerical solution, you can use fsolve:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html#scipy.optimize.fsolve
简短回答:使用 fsolve
正如其他答案中提到的,针对您提出的特定问题,最简单的解决方案是使用类似
fsolve
的东西:输出:
解析解?
你说如何“解决”,但有不同种类的解决方案。既然你提到了 SymPy,我应该指出这可能意味着分析和数字解决方案之间的最大区别。您给出的特定示例没有(简单的)解析解,但其他非线性方程组有。当有现成的分析解时,SymPY 通常可以为您找到它们:
输出:
请注意,在此示例中,SymPy 找到所有解,并且不需要给出初始估计。
您可以使用
evalf
对这些解进行数值评估:数值解的精度
但是,大多数非线性方程组都没有合适的解析解,因此使用上面的 SymPy 非常有用,但并不普遍适用。这就是为什么我们最终寻找数值解,即使有数值解:
1)我们不能保证我们已经找到了所有解决方案,或者当有很多解决方案时,我们不能保证找到“正确”的解决方案。
2)我们必须提供一个初步的猜测,这并不总是那么容易。
接受我们想要数字解决方案后,像
fsolve
这样的东西通常可以满足您的所有需求。对于此类问题,SymPy 可能会慢得多,但它可以提供其他功能,更精确地找到(数字)解决方案:具有更高的精度:
Short answer: use fsolve
As mentioned in other answers the simplest solution to the particular problem you have posed is to use something like
fsolve
:Output:
Analytic solutions?
You say how to "solve" but there are different kinds of solution. Since you mention SymPy I should point out the biggest difference between what this could mean which is between analytic and numeric solutions. The particular example you have given is one that does not have an (easy) analytic solution but other systems of nonlinear equations do. When there are readily available analytic solutions SymPY can often find them for you:
Output:
Note that in this example SymPy finds all solutions and does not need to be given an initial estimate.
You can evaluate these solutions numerically with
evalf
:Precision of numeric solutions
However most systems of nonlinear equations will not have a suitable analytic solution so using SymPy as above is great when it works but not generally applicable. That is why we end up looking for numeric solutions even though with numeric solutions:
1) We have no guarantee that we have found all solutions or the "right" solution when there are many.
2) We have to provide an initial guess which isn't always easy.
Having accepted that we want numeric solutions something like
fsolve
will normally do all you need. For this kind of problem SymPy will probably be much slower but it can offer something else which is finding the (numeric) solutions more precisely:With greater precision:
如果您更喜欢 sympy,可以使用 nsolve。
第一个参数是方程列表,第二个参数是变量列表,第三个参数是初始猜测。
If you prefer sympy you can use nsolve.
The first argument is a list of equations, the second is list of variables and the third is an initial guess.
fsolve
的替代方法是 < code>root:则会打印此信息。
如果您随后检查
并
确认解决方案正确,
An alternative to
fsolve
isroot
:This will print
If you then check
you obtain
confirming that the solution is correct.
试试这个,我向你保证它会完美地工作。
供参考。如上所述,您还可以通过将“fsolve”替换为“broyden1”来使用“布罗伊登近似”。有用。我做到了。
我不知道 Broyden 近似是如何工作的,但它花了 0.02 秒。
而且我建议你不要使用Sympy的功能<-确实方便,但是就速度而言,它相当慢。你会看到的。
Try this one, I assure you that it will work perfectly.
FYI. as mentioned above, you can also use 'Broyden's approximation' by replacing 'fsolve' with 'broyden1'. It works. I did it.
I don't know exactly how Broyden's approximation works, but it took 0.02 s.
And I recommend you do not use Sympy's functions <- convenient indeed, but in terms of speed, it's quite slow. You will see.
我在 IDL 中使用了 Broyden 的方法来处理耦合非线性方程(通常涉及多项式和指数),但我还没有在 Python 中尝试过:
http://docs.scipy.org/doc/scipy/reference/ generated/scipy.optimize.broyden1.html#scipy.optimize.broyden1
I got Broyden's method to work for coupled non-linear equations (generally involving polynomials and exponentials) in IDL, but I haven't tried it in Python:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.broyden1.html#scipy.optimize.broyden1
您可以使用 openopt 包及其 NLP 方法。它有许多动态规划算法来求解非线性代数方程,包括:
goldenSection、scipy_fminbound、scipy_bfgs、scipy_cg、scipy_ncg、amsg2p、scipy_lbfgsb、scipy_tnc、bobyqa、ralg、ipopt、scipy_slsqp、scipy_cobyla、lincher、algencan可供选择。
后面的一些算法可以解决约束非线性规划问题。
因此,您可以使用如下函数将方程组引入 openopt.NLP():
lambda x: x[0] + x[1]**2 - 4、np.exp(x[0]) + x[0]*x[1]
You can use openopt package and its NLP method. It has many dynamic programming algorithms to solve nonlinear algebraic equations consisting:
goldenSection, scipy_fminbound, scipy_bfgs, scipy_cg, scipy_ncg, amsg2p, scipy_lbfgsb, scipy_tnc, bobyqa, ralg, ipopt, scipy_slsqp, scipy_cobyla, lincher, algencan, which you can choose from.
Some of the latter algorithms can solve constrained nonlinear programming problem.
So, you can introduce your system of equations to openopt.NLP() with a function like this:
lambda x: x[0] + x[1]**2 - 4, np.exp(x[0]) + x[0]*x[1]
您可以使用
sympy
的nsolve
,意思是数值求解器
。下面是一个使用
sympy
求解方程的示例片段:其中
(1123, -1231, -1000)
是求根的初始向量。它给出:虚部非常小,均为 10^(-20),因此我们可以将它们视为零,这意味着根都是实数。 Re~13602.938,D~0.047922,f~0.0057。
您可以编辑代码来求解您想要的方程,即
是
Eq(..., ...)
部分。不要忘记声明元素首先使用
symbols('...')
。You can use
nsolve
ofsympy
, meaningnumerical solver
.Here is an example snippet which solves a equation with
sympy
:where
(1123, -1231, -1000)
is the initial vector to find the root. And it gives out:The imaginary part are very small, both at 10^(-20), so we can consider them zero, which means the roots are all real. Re ~ 13602.938, D ~ 0.047922 and f~0.0057.
You can edit the code to solve the equations you want, that
is the
Eq(..., ...)
part. Don't forget to declare elementswith
symbols('...')
first.