Chapter 6 : Integration And Its Applications

Example 6.1.1, page no. 199

In [37]:
import sympy,numpy

print "Indefinite integral"
x = sympy.Symbol('x')
f = sympy.integrate((sympy.sin(x))**4,(x))
print f
Indefinite integral
3*x/8 - sin(x)**3*cos(x)/4 - 3*sin(x)*cos(x)/8

Example 6.1.2, page no. 200

In [39]:
import sympy,numpy

print "Indefinite integral"
x = sympy.Symbol('x')
f = sympy.integrate((sympy.cos(x))**7,(x))
print f
Indefinite integral
-sin(x)**7/7 + 3*sin(x)**5/5 - sin(x)**3 + sin(x)

Example 6.2.1, page no. 202

In [41]:
import sympy,numpy,math

print "Definite integral"
x = sympy.Symbol('x')
f = sympy.integrate((sympy.cos(x))**6,(x,0,math.pi/2))
print float(f)
Definite integral
0.490873852123

Example 6.2.2, page no. 202

In [43]:
import sympy,numpy,math

print "Definite integral"
x = sympy.Symbol('x')
a = sympy.Symbol('a')
g = x**7/(a**2-x**2)**1/2
f = sympy.integrate(g,(x,0,a))
print f
Definite integral
zoo*a**6 + a**6*log(-2*a**2)/4 - 11*a**6/24

Example 6.2.3, page no. 203

In [45]:
import sympy,numpy,math

print "Definite integral"
x = sympy.Symbol('x')
a = sympy.Symbol('a')
g = x**3*(2*a*x-x**2)**(1/2) 
f = sympy.integrate(g,(x,0,2*a))
print f
Definite integral
4*a**4

Example 6.2.4, page no. 204

In [23]:
from sympy.abc import x,a,n
from sympy.integrals import Integral
print "Definite integral"
g = 1./(a**2+x**2)**n 
f = Integral(g,(x,0,1))
print f.as_sum(2).n()
Definite integral
0.5*(a**2 + 0.5625)**(-n) + 0.5*(a**2 + 0.0625)**(-n)

Example 6.4.1, page no. 207

In [4]:
from sympy.abc import x,a,n
from sympy.integrals import Integral
import sympy, math
print "Definite integral"
g = (sympy.sin(6*x))**3*(sympy.cos(3*x))**7
f = Integral(g,(x,0,math.pi/6))
print f.as_sum(2).n()
Definite integral
0.0532891345937363

Example 6.4.2, page no. 208

In [52]:
import sympy,numpy,math

print "Definite integral"
x = sympy.Symbol('x')
g = (sympy.sin(6*x))**3*(sympy.cos(3*x))**7
g = x**4*(1-x**2)**(3/2)
f = sympy.integrate(g,(x,0,1)) 
print float(f)
Definite integral
0.0571428571429

Example 6.5, page no. 208

In [63]:
import sympy,numpy,math

print "Definite integral"
x = sympy.Symbol('x')
m = sympy.Symbol('m')
n = sympy.Symbol('n')
n = int(raw_input("Enter n : "))
m = int(raw_input("Enter m : "))
g =(sympy.cos(x))**m*sympy.cos(n*x)
f = sympy.integrate(g,(x,0,math.pi/2))
print float(f) 
g2 =(sympy.cos(x))**(m-1)*sympy.cos((n-1)*x)
f2 = m/(m+n)*sympy.integrate(g2,(x,0,math.pi/2))
print float(f2)
print "Equal"
Definite integral
Enter n : 0
Enter m : 1
1.0
1.0
Equal

Example 6.6.1, page no. 210

In [65]:
import sympy,numpy,math

print "Definite integral"
x = sympy.Symbol('x')
a = sympy.Symbol('a')
n = int(raw_input("Enter n : "))
g = sympy.exp(a*x)*(sympy.sin(x))**n
f = sympy.integrate(g,(x))
print f
Definite integral
Enter n : 1
Piecewise((x*exp(-I*x)*sin(x)/2 - I*x*exp(-I*x)*cos(x)/2 - exp(-I*x)*cos(x)/2, a == -I), (x*exp(I*x)*sin(x)/2 + I*x*exp(I*x)*cos(x)/2 - exp(I*x)*cos(x)/2, a == I), (a*exp(a*x)*sin(x)/(a**2 + 1) - exp(a*x)*cos(x)/(a**2 + 1), True))

Example 6.7.1, page no. 212

In [66]:
import sympy,numpy,math

x = sympy.Symbol('x')
print sympy.integrate(sympy.tan(x)**5,(x))
(4*sin(x)**2 - 3)/(4*sin(x)**4 - 8*sin(x)**2 + 4) - log(sin(x)**2 - 1)/2

Example 6.8, page no. 215

In [11]:
import numpy,sympy,math

n = int(raw_input("Enter the value of n : "))
p = sympy.integrate((sympy.tan(x))**(n-1),('x',0,math.pi/4))
q = sympy.integrate((sympy.tan(x))**(n+1),('x',0,math.pi/4))
print "n(p+q)= ",
print n*(p+q)
Enter the value of n : 3
n(p+q)=  0.999999999999999

Example 6.9.1, page no. 217

In [69]:
import sympy,numpy,math

x = sympy.Symbol('x')
g = sympy.sec(x)**4
print sympy.integrate(g,('x',0,math.pi/4))
1.33333333333333

Example 6.9.2, page no. 217

In [36]:
import sympy,numpy,math

x = sympy.Symbol('x')
print sympy.integrate('1/sin(x)**3',('x',math.pi/3,math.pi/2))
0.607986405500361

Example 6.10, page no. 220

In [20]:
import sympy,math
from sympy.abc import x
from sympy.integrals import Integral
import sympy, math
g = x*sympy.sin(x)**6*sympy.cos(x)**4
f = Integral(g,(x,0,math.pi/6))
print float(f)
0.000362418686373

Example 6.12, page no. 221

In [17]:
import sympy,math
from sympy.abc import x
from sympy.integrals import Integral
x = sympy.Symbol('x')
a = math.pi/2
f = (sympy.sin(x)**0.5)/(sympy.sin(x)**0.5+sympy.cos(x)**0.5)
f = Integral(f,(x, 0, a))
print float(f)
0.785398163397

Example 6.13, page no. 223

In [4]:
import sympy,numpy,math

x = sympy.Symbol('x')
print "The summation is equivalent to integration of 1/(1+xˆ2) from 0 to 1"
g = 1/(1+x**2)
f = sympy.integrate(g,(x,0,1))
print float(f)
The summation is equivalent to integration of 1/(1+xˆ2) from 0 to 1
0.785398163397

Example 6.14, page no. 223

In [5]:
import sympy,numpy,math

x = sympy.Symbol('x')
print "The summation is equivalent to integration of log(1+x) from 0 to 1"
g = sympy.log(1+x)
f = sympy.integrate(g,(x,0,1))
print float(f) 
The summation is equivalent to integration of log(1+x) from 0 to 1
0.38629436112

Example 6.15, page no. 225

In [31]:
import sympy,math
from sympy.integrals import Integral
f = Integral(x*sympy.sin(x)**8*sympy.cos(x)**4,(x,0,math.pi))
print float(f)
0.0337339994178

Example 6.16, page no. 226

In [30]:
import numpy,sympy,math
from sympy.integrals import Integral
x = sympy.Symbol('x')
#f = sympy.integrate(sympy.log(sympy.sin(x)),('x',0,math.pi/2))
f = Integral(sympy.log(sympy.sin(x)), (x, 0, 1.5707963267949))
print float(f)
-1.08879304515

Example 6.24, page no. 234

In [20]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy
import math

x = numpy.linspace(-5,10,70)
y1 = (x+8)/2
y2 = x**2/8

plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('My Graph')
plt.plot(x, y1, "o-" )
plt.plot(x, y2, "+-" )
plt.legend(["(x+8)/2","x**2/8"])
print "from the graph, it is clear that the points of intersection are x=−4 and x =8."
print "So, our region of integration is from x=−4 to x=8"
print sympy.integrate('(x+8)/2-x**2/8',('x',-4,8))
from the graph, it is clear that the points of intersection are x=−4 and x =8.
So, our region of integration is from x=−4 to x=8
36