如何做自然日志的派生类型错误:无法将表达式转换为浮点数

时间:2019-08-03 00:08:58

标签: python derivative

我正在运行此python代码以导出方程式。  R(x)= 50 * ln(5x +1)

衍生物

我尝试了numpy.log和math.log

from sympy import Symbol, Derivative
import numpy as np
import math
x= Symbol('x')

function = 50*(math.log(5*x+1))

deriv= Derivative(function, x)
deriv.doit()

我期望得到微分后的方程,但是我得到了错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-107-e41161e3f329> in <module>()
      5 x= Symbol('x')
      6 
----> 7 function = 50*(math.log(5*x+1))
      8 
      9 deriv= Derivative(function, x)

~/anaconda3/lib/python3.7/site-packages/sympy/core/expr.py in __float__(self)
    254         if result.is_number and result.as_real_imag()[1]:
    255             raise TypeError("can't convert complex to float")
--> 256         raise TypeError("can't convert expression to float")
    257 
    258     def __complex__(self):

TypeError: can't convert expression to float

1 个答案:

答案 0 :(得分:2)

请勿将mathsympy混合使用。使用log中的sympy

import sympy as sp

x= sp.Symbol('x')

y = 50*(sp.log(5*x+1))

deriv= sp.Derivative(y, x)

deriv.doit()

print(deriv.doit()) #250/(5*x + 1)