简体   繁体   中英

Wrong numpy integral calculation result

I am trying to calculate integral of sin(x)*sin(2x) in - pi,pi range but İ can't find 0. I tried with math.randiand(math.pi) but still wrong.

import scipy as sp
import numpy as np

def integrand(x):
  return math.sin(x)*math.sin(2*x)

def int_test(a,b):
  return sp.integrate.quad(integrand,-math.pi,math.pi)

I had to play around with how I imported the integrate library:

import scipy.integrate as integrate
import numpy as np
import math

def integrand(x):
  return math.sin(x)*math.sin(2*x)

def int_test(a, b):
  return integrate.quad(integrand, a, b)

x = int_test(-math.pi, math.pi)
print(x[0])
→ python3 test.py 
1.5156111913114e-16

which is pretty close to 0.

Note that you get a tuple back as the response. You can read here what the return values are: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM