Python求解一个变量的方程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4449110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python solve equation for one variable
提问by hah
I'm trying to solve an equation in python using SymPy. I have a generated equation (something like function = y(8.0-(y**3.0))which I use with SymPy to create a new equation like this: eq = sympy.Eq(function, 2)which outputs y(8.0-(y**3.0)) == 2. but sympy.solve(eq)doesn't seem to work.
我正在尝试使用 SymPy 在 python 中求解方程。我有一个生成的方程(类似于function = y(8.0-(y**3.0))我与 SymPy 一起使用以创建这样的新方程:eq = sympy.Eq(function, 2)输出y(8.0-(y**3.0)) == 2. 但sympy.solve(eq)似乎不起作用。
>>> from sympy import Eq, Symbol as sym, solve
>>> y = sym('y')
>>> eqa = Eq(y(8.0-(y**3.0)), 8)
>>> solve(eqa)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.6/sympy/solvers/solvers.py", line 332, in solve
result = tsolve(f, *symbols)
File "/usr/lib/pymodules/python2.6/sympy/solvers/solvers.py", line 716, in tsolve
raise NotImplementedError("Unable to solve the equation.")
NotImplementedError: Unable to solve the equation.
thanks for reading.
谢谢阅读。
回答by duffymo
This is a non-linear equation. What you need to look for is a root finding algorithm in SciPy.
这是一个非线性方程。您需要寻找的是 SciPy 中的寻根算法。
回答by duffymo
回答by Gary Kerr
(I don't know why you mention scipy in your question when you use sympy in your code. I'll assume you are using sympy.)
(当您在代码中使用 sympy 时,我不知道您为什么在问题中提到 scipy。我假设您使用的是 sympy。)
Sympy can solve this equation if you specify an integer power for y(ie y**3.0changed to y**3).
如果您为 指定整数幂y(即y**3.0更改为y**3),Sympy 可以解决这个方程。
The following works for me using Sympy 0.6.7.
以下使用 Sympy 0.6.7 对我有用。
from sympy import Eq, Symbol, solve
y = Symbol('y')
eqn = Eq(y*(8.0 - y**3), 8.0)
print solve(eqn)
回答by glarrain
For nonlinear equations, you should use sympy.solvers.nsolveto solve it numerically, except for some special cases where a more specific and appropriate solver may exist (e.g. tsolve).
对于非线性方程,您应该使用sympy.solvers.nsolve它进行数值求解,除非在某些特殊情况下可能存在更具体和更合适的求解器(例如tsolve)。
For example, the following script should output 1.2667664310254.
例如,以下脚本应输出 1.2667664310254。
from sympy import Symbol
from sympy.solvers import nsolve
from sympy import sin, tan
theta = Symbol('theta')
print nsolve(tan(theta)/(1+1*sin(theta)) - 4.0**2/9.81, theta, (1.2,))
回答by Pierz
Assuming you mean you were trying to use sympy, as opposed to scipy, then you can get Sympy (works with v0.7.2+) to solve it by making a small adjustment to way you defined your equation - you just need to put a multiplication operator (*) in between the first 'y' and the '('. It doesn't appear to matter whether you specify the power as a float or not (but it's possible it was required in 0.6.7).
假设您的意思是您尝试使用 sympy,而不是 scipy,那么您可以通过对定义方程的方式进行小幅调整来获得 Sympy(适用于 v0.7.2+)来解决它 - 您只需要进行乘法运算运算符 (*) 位于第一个 'y' 和 '(' 之间。是否将幂指定为浮点数似乎无关紧要(但在 0.6.7 中可能需要它)。
from sympy import Eq, var, solve
var('y')
eq = Eq(y*(8.0-(y**3.0)), 8)
solve(eq)

