Python求解一个变量的方程式 [英] Python solve equation for one variable

查看:227
本文介绍了Python求解一个变量的方程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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.

感谢阅读.

解决方案

(我不知道您为什么在代码中使用sympy时在问题中提到scipy.我假设您使用的是sympy.)

如果为y指定整数幂(即y**3.0更改为y**3),Sympy可以求解此方程.

以下内容适用于我使用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)

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.

>>> 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.

解决方案

(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 can solve this equation if you specify an integer power for y (ie y**3.0 changed to y**3).

The following works for me using 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)

这篇关于Python求解一个变量的方程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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