How to do a derivative of a natural log getting TypeError: can't convert expression to float
How to do a derivative of a natural log getting TypeError: can't convert expression to float
我运行这个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
不要将 math
与 sympy
混用。从 sympy
:
使用 log
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)
我运行这个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
不要将 math
与 sympy
混用。从 sympy
:
log
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)