I am working on a little side project and building a calculator for different numerical analysis methods. What I am struggling with is calculating derivatives of trigonometric functions. For calculations of derivatives I am using sympy and math Python library.
print(sym.diff(math.cos(x)))
The correct answer for this should be -sin(x)
, however I get:
TypeError: can't convert expression to float
Is there any way I can correctly calculate derivatives of trigonometric functions?
SymPy has its own functions which know how to be differentiated. math
versions only know how to give a numerical answer for numerical input.
>>> from sympy import sin
>>> from sympy.abc import x
>>> sin(x).diff(x)
cos(x)
This should give you an idea of how to go about it.
import sympy as sym
import math
x=sym.symbols('x')
def f(x):
return x**2
print(sym.diff(f(x)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.