Sympy: integrate () weird output

I am just learning how to use sympy and I have tried simple integration of the sin function. When the sin() argument has a constant constant phase, the integrate() output gives the same value as the phase: 0

 from sympy import * w = 0.01 phi = 0.3 k1 = integrate(sin(w*x), (x, 0.0, 10.0)) k2 = integrate(sin(w*x + 0.13), (x, 0.0, 10.0)) k3 = integrate(sin(w*x + phi),(x, 0.0, 10.0)) k1, k2, k3 (0.499583472197429, 0, 0) 

Can someone explain to me why?

+5
source share
3 answers

This seems to be a mistake. A workaround is to get the first character of your integral (which seems to work fine), and then evaluate it for each set of parameters at the upper and lower bounds and calculate the difference:

 import sympy as sp x, w, phi = sp.symbols('xw phi') # integrate function symbolically func = sp.integrate(sp.sin(w * x + phi), x) # define your parameters para = [{'w': 0.01, 'phi': 0., 'lb': 0., 'ub': 10., 'res': 0.}, {'w': 0.01, 'phi': 0.13, 'lb': 0., 'ub': 10., 'res': 0.}, {'w': 0.01, 'phi': 0.3, 'lb': 0., 'ub': 10., 'res': 0.}] # evaluate your function for all parameters using the function subs for parai in para: parai['res'] = func.subs({w: parai['w'], phi: parai['phi'], x: parai['ub']}) -func.subs({w: parai['w'], phi: parai['phi'], x: parai['lb']}) 

After that, para looks like this:

 [{'lb': 0.0, 'phi': 0.0, 'res': 0.499583472197429, 'ub': 10.0, 'w': 0.01}, {'lb': 0.0, 'phi': 0.13, 'res': 1.78954987094131, 'ub': 10.0, 'w': 0.01}, {'lb': 0.0, 'phi': 0.3, 'res': 3.42754951227208, 'ub': 10.0, 'w': 0.01}] 

which seems to give reasonable integration results that are stored in res

0
source

I just ran your code in the SymPy development version and I got (0.499583472197429, 1.78954987094131, 3.42754951227208) . Thus, it seems that the error will be fixed in the next version.

It also looks like this error is only in Python 2. When I use Python 3, even with the latest stable version (0.7.6.1), I get the same answer.

0
source

Can I recommend using numpy for numerical integration?

 >>> import numpy as np >>> w = 0.01 >>> phi = 0.3 >>> dt = 0.01 >>> t = 2*np.pi*np.arange(0,1,dt) >>> np.sum( np.sin(t)*dt) -1.0733601507606494e-17 >>> np.sum( np.sin(t+ phi)*dt) 2.5153490401663703e-17 

These numbers are mostly close to 0. The exact number is an artifact of our choice of the grid dt and shift phi (as well as the accuracy of np.sin )

To fit your example:

 >>> t = np.arange(0,10,dt) >>> w = 0.01 >>> phi = 0.3 >>> np.sum( np.sin(w*t)*dt) 0.4990843046978698 >>> np.sum( np.sin(w*t + phi)*dt) 3.4270800187375658 >>> np.sum( np.sin(w*t + 0.13)*dt) 1.7890581525454512 

As pointed out in Python Integration using Sympy It's a bad idea to use a symbolic library for numerical work

0
source

Source: https://habr.com/ru/post/1240809/


All Articles