用python求解非线性方程组 [英] Solving system of nonlinear equations with python

查看:181
本文介绍了用python求解非线性方程组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以根据python中的参数求解非线性方程组吗?有例子或教程吗?我可以在maple中轻松地做到这一点,但是我特定系统的表达式很大,将它们复制过来非常困难.

Can I solve a system of nonlinear equations in terms of parameters in python? Is there a example or tutorial? I can do this easily in maple, but the expressions for my particular system are pretty big and copying them over is quite hard.

示例:

sigma*(y-x) = 0
x*(rho-z)-y = 0
x*y-beta*z = 0

您应该获得解决方案:

[[x = 0, y = 0, z = 0], [x = sqrt(beta*rho-beta), y = sqrt(beta*rho-beta), z = rho-1],
[x = -sqrt(beta*rho-beta), y = -sqrt(beta*rho-beta), z = rho-1]]

我问的原因:我有一个大型的非线性ODE系统.我想求解不动点(这是可行的,已经在maple中完成,但是它们又大又丑陋).我想从固定点创建更多表达式,然后在scipy中使用优化包.我宁愿用python来做所有事情,也不愿来回翻译事情,因为它效率很低而且会出错.

The reason I ask: I have a large system of nonlinear ODEs. I want to solve for the fixed points (this is doable, it's been done in maple, but they are large and ugly). I want to create further expressions from the fixed points and then use optimisation package in scipy. I'd rather do it all in python than translate things back and forth since it is very inefficient and mistakes can be made.

推荐答案

重申@Russ的答案,可以在sympy中轻松实现.例如:

Reiterating @Russ's answer, this can be easily accomplished in sympy. For example:

In [1]: import sympy as sp
In [2]: x, y, z = sp.symbols('x, y, z')
In [3]: rho, sigma, beta = sp.symbols('rho, sigma, beta')
In [4]: f1 = sigma * (y - x)
In [5]: f2 = x * (rho - z) - y
In [6]: f3 = x * y - beta * z
In [7]: sp.solvers.solve((f1, f2, f3), (x, y, z))
Out[7]: 
[(0, 0, 0),
 (-sqrt(beta*rho - beta), -sqrt(beta*(rho - 1)), rho - 1),
 (sqrt(beta*rho - beta), sqrt(beta*(rho - 1)), rho - 1)]

,其中输出格式为(x, y, z)的可能值的3个可能的元组.

where the output format is 3 possible tuples of the possible values for (x, y, z).

这篇关于用python求解非线性方程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆