简体   繁体   English

如何通过 Python 重新排列一个复杂的方程

[英]How to rearrange a complicated equation by Python

I want to rearrange the below equation for the variable r by using Python.我想使用 Python 为变量r重新排列下面的等式。

P = C * ((1-(1+r)**(-n)))/r + fv*(1+r)**(-n)
to
r = blabla...

I understood that sympy is related to a rearranging task like this.我知道 sympy 与这样的重新安排任务有关。 So, I wrote the below codes.所以,我写了下面的代码。

# Solve the equation for r
import sympy
from sympy import symbols

P, C, r, n, fv = sympy.symbols('P C r n fv')
eq = sympy.Eq(P, C * ((1-1/(1+r)**n))/r + fv/(1+r)**n)
sympy.solve(eq, r)

However, I got the error like this.但是,我得到了这样的错误。

NotImplementedError                       Traceback (most recent call last)
<ipython-input-47-a183add313da> in <module>
      3 P, C, r, n, fv = sympy.symbols('P C r n fv')
      4 eq = sympy.Eq(P, C * ((1-1/(1+r)**n))/r + fv/(1+r)**n)
----> 5 sympy.solve(eq, r)

~\Anaconda3\lib\site-packages\sympy\solvers\solvers.py in solve(f, *symbols, **flags)
   1169     ###########################################################################
   1170     if bare_f:
-> 1171         solution = _solve(f[0], *symbols, **flags)
   1172     else:
   1173         solution = _solve_system(f, symbols, **flags)

~\Anaconda3\lib\site-packages\sympy\solvers\solvers.py in _solve(f, *symbols, **flags)
   1740 
   1741     if result is False:
-> 1742         raise NotImplementedError('\n'.join([msg, not_impl_msg % f]))
   1743 
   1744     if flags.get('simplify', True):

NotImplementedError: multiple generators [r, (r + 1)**n]
No algorithms are implemented to solve equation -C*(1 - (r + 1)**(-n))/r + P - fv*(r + 1)**(-n)

I guess the calculation for power is not available as to sympy.我猜想 sympy 无法计算功率。 Do you know how to execute this kind of complicated rearrangement for a equation?你知道如何对方程执行这种复杂的重排吗? I am using Python==3.7, sympy==1.4.我正在使用 Python==3.7,sympy==1.4。

This is not a trivial equation to solve.这不是一个简单的方程来解决。 It doesn't have to do with the calculation for power though, it's just that the equation is too complicated for sympy to solve for r.不过,这与功率计算无关,只是方程太复杂,sympy 无法求解 r。

However, if there are specific values for the other variables and you need to solve for r (ie find a zero for a nontrivial equation), you can use the numerical solver: nsolve但是,如果其他变量有特定值并且您需要求解 r(即为非平凡方程找到零),则可以使用数值求解器:nsolve

# Solve the equation for r
from sympy import var, Eq, solve

var('C, r, n, fv, P', positive = True)

# this throws an error: no algorithms are implemented to solve equation
equation = Eq(P, C * ((1-1/(1+r)**n))/r + fv/(1+r)**n)

# a simple calculation for power works fine
equation = Eq(P, (1+r)**n)
solve(equation, r)

The equation you are trying to solve is:您要解决的方程式是:

In [23]: eq                                                                                                                       
Out[23]: 
      ⎛           -n⎞               
    C⋅⎝1 - (r + 1)  ⎠             -n
P = ───────────────── + fv⋅(r + 1)  
            r 

We can rearrange this into a polynomial like so我们可以将它重新排列成这样的多项式

In [24]: eq2 = Eq(eq.lhs * (1+r)**n * r, eq.rhs * (1+r)**n * r).expand()                                                          

In [25]: eq2                                                                                                                      
Out[25]: 
           n            n           
P⋅r⋅(r + 1)  = C⋅(r + 1)  - C + fv⋅r

Now we see that this is a polynomial except that the exponent n is symbolic.现在我们看到这是一个多项式,只是指数n是符号的。 In general this kind of equation will not have a solution expressible in closed form - that is why sympy has no algorithms for this particular case (it is not a limitation of sympy itself).一般来说,这种方程不会有一个可以用封闭形式表达的解——这就是为什么 sympy 没有针对这种特殊情况的算法(它不是 sympy 本身的限制)。

It is possible to solve this equation numerically but numerical solution only works if we have numerical values for each of the parameters.可以对这个方程进行数值求解,但只有当我们对每个参数都有数值时,数值求解才有效。 If we substitute numbers for the parameters then nsolve can find the solution numerically:如果我们用数字代替参数,那么nsolve可以用数字找到解:

In [26]: eq3 = eq.subs({P:1, C:2, fv:1, n:100})                                                                                   

In [27]: eq3                                                                                                                      
Out[27]: 
                   ⎛        1     ⎞
                 2⋅⎜1 - ──────────⎟
                   ⎜           100⎟
        1          ⎝    (r + 1)   ⎠
1 = ────────── + ──────────────────
           100           r         
    (r + 1)                        

In [28]: nsolve(eq3, r, 1)                                                                                                        
Out[28]: 2.00000000000000

But note that the solutions to this equation are not unique for example -2 is also a solution here:但请注意,此方程的解不是唯一的,例如 -2 也是这里的解:

In [52]: nsolve(eq3, r, -1.9)                                                                                                     
Out[52]: -2.00000000000000

This particular equation has something like 100 roots although not necessarily all real.这个特殊的方程有大约 100 个根,但不一定都是实数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM