Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating trigonometric expressions in sympy

Using python 2.7 with PyCharm Community Edition 2016.2.3 + Anaconda distribution.

I have an input similar to :

from sympy import *

x = symbols('x')
f = cos(x)
print (f.subs(x, 25))

The output is cos(25), . Is there a way to evaluate trigonometric identities such as sin/cos, at a certain angle ? I've tried cos(degrees(x)), but nothing differs. Am I missing some crucial part of documentation or there really isn't a way to do this ? Ty for your help :)

like image 917
borgmater Avatar asked Oct 20 '25 13:10

borgmater


1 Answers

Perform a numerical evaluation using function N:

>>> from sympy import N, symbols, cos
>>> x = symbols('x')
>>> f = cos(x)
>>> f.subs(x, 25)
cos(25)
>>> N(f.subs(x, 25)) # evaluate after substitution 
0.991202811863474

To make the computation in degrees, convert the angle to radians, using mpmath.radians, so the computation is performed on a rad value:

>>> import mpmath
>>> f.subs(x, mpmath.radians(25))
0.906307787036650

Importing with * (wildcard imports) isn't a very good idea. Imagine what happens if you equally did from math import *, then one of the cos functions from both modules will be out in the wild.

See the PEP 8 guideline on imports.

like image 112
Moses Koledoye Avatar answered Oct 23 '25 09:10

Moses Koledoye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!