Computing integration of complicated functions by hand is a challenging task, and sometimes, it almost becomes impossible, but with computational software, it is very easy to do.
The command “integrate()” and its alias “integral()” are used in sagemath for computing definite, indefinite, improper, single, double or any multiple integrals. “numerical_integral()” is used for the numerical integration.
Indefinite Integrals
In sagemath, we can easily find an indefinite integral with the “integral(f, x)” command, where \(f\) is a function and \( x \) is a differential. Differentiation in sagemath requires all variables should be declared first but here in integral we don’t need to do that.
Let us find the indefinite integral of some functions in sagemath:
1. Integrate \(\int sinx dx\).
Input:
integral(sin(x),x)
Output:
-cos(x)
2. Integrate \(\int \frac{cosx}{2-cosx} dx\).
Input:
integral(cos(x)/(2-cos(x)),x)
Output:
4/3*sqrt(3)*arctan(sqrt(3)*sin(x)/(cos(x) + 1)) - 2*arctan(sin(x)/(cos(x) + 1))
3. Sagemath is unable to compute the integral of some functions and gives error functions in output such as integral of \(\int \frac{sinx}{x}\).
Input:
integral(sin(x)/x, x)
Output:
-1/2*I*Ei(I*x) + 1/2*I*Ei(-I*x)
Definite Integrals
A definite integral is an area under the curve and between two fixed limit points on the \( x-axis\). In sagemath, we can use the command \(integral(f, x, x_{min}, x_{max}) \) for computing the definite integral of a function \(f\) between \(x_{min}\) and \(x_{max}\).
Below are some examples of definite integrals, computed with the help of sagemath.
1. Integrate \( \int_{0}^{\frac{\pi}{2}}sinx dx \).
Input:
integral(sin(x), x, 0, pi/2)
Output:
1
2. Integrate \( \int_{1}^{2} \frac{1}{x} dx \).
Input:
integral(1/x, x, 1,2)
Output:
log(2)
3. Integrate \( \int_{1}^{2} \frac{x^4 – 3x^2 + 6}{x^6 – 5x^4 + 5x^2 + 4} dx \).
Input:
h(x) = (x^4 - 3*x^2 + 6) / (x^6 - 5*x^4 + 5*x^2 + 4)
integral(h(x), x, 1, 2)
Output:
-1/2*pi + arctan(8) + arctan(5) + arctan(2) + arctan(1/2)
Sagemath can only determine convergent integrals and give value Error in case of divergent. Below is an example of the divergent integral.
4. Integrate \( \int_{1}^{0} \frac{1}{x^2}\; dx \).
Input:
integral(1/x^2, x, 1,0)
Output:
Traceback (most recent call last):
...
ValueError: Integral is divergent.
Parameters In Functions
If we integrate \(asinx\), sagemath gives an error due to undefined parameters involved in the function.
Input:
integral(a*sin(x),x)
Output:
Traceback (most recent call last):
...
NameError: name 'a' is not defined
We have to define \(a\) before the evaluation of this integral, and the correct code will be:
Input:
var('a')
integral(a*sin(x),x)
Output:
-a*cos(x)
Only defining parameters always does not work, in some cases, sagemath needs additional information about them.
The integral of \(x^a\) still gives an error although \(a\) is defined.
Input:
var('a x')
integral(1/x^a,x)
Output:
Traceback (most recent call last):
...
Is a equal to -1?
We have to assume \(a\) is either positive, or some definite value such as \(a>0, \;or\; a=-1\).
Let us compute the integral of \(x^a\) for each assumption.
1. \(\int asinx\) and \(a=-1\).
Input:
var('a')
assume(a==-1)
integral(x^a,x)
Output:
log(x)
Above we assume(a==-1), which conflicts with further assumptions. We have to add the “forget()” command for the next assumption, otherwise, sagemath will give the error “inconsistent assumption“.
2. \(\int asinx\) and \(a>0\).
Input:
forget()
var('a')
assume(a>0)
integral(x^a,x)
Output:
x^(a + 1)/(a + 1)
Double Integrals
We can also Integrate higher dimension definite and indefinite integral in sagemath with the command “f.integral(x).integral(y)” or “integral(integral(f,x),y)”, where \(f\) is a function of \(x\) and \(y\).
Now we discuss some examples of both higher dimension indefinite and definite integrals for a better understanding of the commands.
Indefinite Double Integrals
1. Find \(\iint x+y \; dy dx\)
Input:
f(x,y)=x+y
f.integral(x).integral(y)
Output:
(x, y) |--> 1/2*x^2*y + 1/2*x*y^2
2. Find \(\iint cos(x)+sin(y) \; dy dx\)
Input:
f(x,y) = cos(x)+sin(y)
f.integral(x).integral(y)
Output:
(x, y) |--> -x*cos(y) + y*sin(x)
Definite Double Integrals
1. Find \(\int_{0}^{1}\int_{1}^{2} 1\; dy dx\)
Input:
f(x,y)=1
f.integral(x,1,2).integral(y,0,1)
Output:
1
2. Find \(\int_{0}^{1}\int_{x}^{\sqrt x} y+y^3\; dx dy\)
Input:
f(x,y) = y+y^3
f.integral(y,x,sqrt(x)).integral(x,0,1)
Output:
7/60
Triple Integrals
If we have a function \(f(x,y,z)\), we can find the triple integral of this function with the command “f.integral(x).integral(y).integral(z)” or “integral(integral(integral(f,x),y),z)”.
Indefinite Triple Integrals
Find \( \iiint x+y+z \; dzdydx \)
Input:
f(x,y,z)=x+y+z
f.integral(x).integral(y).integral(z)
Output:
(x, y, z) |--> 1/2*x^2*y*z + 1/2*x*y^2*z + 1/2*x*y*z^2
Definite Triple Integrals
Find \( \int_{-4}^{4}\int_{-\sqrt{16-x^2}}^{\sqrt{16-x^2}}\int_{0}^{3} \; dx dy dz \)
Input:
f(x,y,z)=1
f.integral(z,0,3).integral(y,-sqrt(16-x^2), sqrt(16-x^2)).integral(x,-4,4)
Output:
48*pi
Multiple Integrals of Function
Multiple integrals could be found by repeating integral commands for the required time. Like in double and triple integrals, we repeated integral command two times, and three times respectively.
Improper Integrals
We can also find improper integral in sagemath with the same command “integral(f, x, -oo, oo)”, and integral could be improper from one end or from both ends. Where -oo is negative infinity, and oo is positive infinity.
1. Find \(\int_{0}^{\infty}\frac{sinx}{x} \; dx \)
Input:
integral(sin(x)/x,x,0,oo)
Output:
1/2*pi
2. Find \(\int_{1}^{\infty}\frac{1}{x^2} \; dx \)
Input:
integral(1/x^2, x, 1,oo)
Output:
1
Only convergent integral could be found in sagemath, and it returns an error in the case of the divergent integral. Below is an example of the divergent integral.
3. Find \( \int_{-\infty}^{\infty}e^x \; dx \)
Input:
integral(e^x, x, -oo,oo)
Output:
Traceback (most recent call last):
...
ValueError: Integral is divergent.
Numerical Integration
In sagemath, the command numerical_integral\((f, x_{min}, x_{max})\) is used for the computation of the numerical integral of function \(f\).
The output will be a tuple in which the first element is an approximation of the function and 2nd element is the error bound.
1. Approximate \(\int_{1}^{4}\frac{1}{x} \; dx\).
Input:
numerical_integral(1/x,1,4)
Output:
(1.3862943611198906, 1.539095918623324e-14)
2. Approximate \(\int_{0}^{1}\frac{1}{1+x^2} \; dx\).
Input:
numerical_integral(1/(1+x^2), 0,1)
Output:
(0.7853981633974484, 8.719671245021581e-15)
We can also approximate improper integral with the help of this command.
3. Approximate \(\int_{-\infty}^{\infty}\frac{1}{x^2} \; dx\).
Input:
numerical_integral(1/x^2, -oo,oo)
Output:
(-1.9999999979144973, 6.227588444929211e-09)
We may also use simple integral command and n() function in sagemath for the computation of numerical integration.
4. Approximate \(\int_{1}^{4}\frac{1}{x} \; dx\).
Input:
integral(1/x, x, 1,4).n()
Output:
1.38629436111989
Practice Exercises
1. \(\int sin^2x \; dx\)
2. \( \int \sqrt{x^2-1} \; dx\)
3. \( \int \frac{1}{\sqrt {a^2+x^2}} \;dx \)
4. \( \int x^a+x^b \;dx\) for \(a=b=1\) and \(a>0,b>0\)
5. \(\int \frac{1}{a+bsinx}\; dx \)
6. \( \int \frac{1}{a x^{n} + b x}\; dx \)
7. \( \int_{a}^{b}\frac{1}{x}\;dx\)
8. \( \int_{0}^{\frac{\pi}{2}}ln(sin x)\;dx\)
9. \(\int_{0}^{e}x^2 lnx\;dx \)
10. \( \int_{-\infty}^{2}e^{2x}\;dx\)
11. Appromixate \(\int_{1}^{2}lnx\;dx \)
12. Appromixate \( \int_{0}^{\pi}sinx\;dx\)