CoCalc Public Files13 - Symbolic Integration Assignment / Symbolic Integration Notes.sagewsOpen with one click!
Author: Aaron Tresham
Views : 113
License: Other -- explicitly state in your code
Compute Environment: Ubuntu 20.04 (Default)
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


  • Intro to Sage

Symbolic Integration

In this lab we will learn how to compute both definite integrals and indefinite integrals (antiderivatives) in Sage.

The Sage command is called "integral." The number of arguments depends on whether the integral is definite or indefinite.

Indefinite Integrals

To compute an indefinite integral in Sage, use the "integral" command with two arguments: integral(function, variable). In other words,

f(x)dx=integral(f(x),x)\displaystyle\int f(x)\, dx=\verb|integral(f(x),x)|

Note: Sage does not add the constant of integration (the +C+C).

Example 1

x+1x2+9dx\int\frac{x+1}{x^2+9}\, dx

You can type the expression directly:

integral((x+1)/(x^2+9),x) #Don't forget the parentheses around the numerator and denominator. show(_)
1/3*arctan(1/3*x) + 1/2*log(x^2 + 9)
13arctan(13x)+12log(x2+9)\displaystyle \frac{1}{3} \, \arctan\left(\frac{1}{3} \, x\right) + \frac{1}{2} \, \log\left(x^{2} + 9\right)

You can also use a function name:

f(x)=(x+1)/(x^2+9) integral(f(x),x) show(_)
1/3*arctan(1/3*x) + 1/2*log(x^2 + 9)
13arctan(13x)+12log(x2+9)\frac{1}{3} \, \arctan\left(\frac{1}{3} \, x\right) + \frac{1}{2} \, \log\left(x^{2} + 9\right)

Think about how you would do this one by hand.

You could break up the fraction: x+1x2+9=xx2+9+1x2+9\frac{x+1}{x^2+9}=\frac{x}{x^2+9}+\frac{1}{x^2+9}

You can compute xx2+9dx\displaystyle\int\frac{x}{x^2+9}\, dx using substitution (this gives you 12log(x2+9)\frac{1}{2}\log(x^2 + 9)).

You can compute 1x2+9dx\displaystyle\int\frac{1}{x^2+9}\, dx using a little algebra (this gives you 13arctan(x3)\frac{1}{3}\arctan(\frac{x}{3})).

[Remember, "log" in Sage is the natural logarithm, often denoted "ln." Also, we would normally write 12ln(x2+9)\frac{1}{2}\ln(|x^2+9|), but Sage leaves out the absolute value.]

Example 2

x43x2dx\int x\sqrt{4-3x^2}\, dx

integral(x*sqrt(4-3*x^2),x) show(_)
-1/9*(-3*x^2 + 4)^(3/2)
19(3x2+4)32-\frac{1}{9} \, {\left(-3 \, x^{2} + 4\right)}^{\frac{3}{2}}

This is a fairly simply substitution question.

What if we take away the xx in front?

Example 3

43x2dx\int\sqrt{4-3x^2}\, dx

integral(sqrt(4-3*x^2),x) show(_)
1/2*sqrt(-3*x^2 + 4)*x + 2/3*sqrt(3)*arcsin(1/2*sqrt(3)*x)
123x2+4x+233arcsin(123x)\frac{1}{2} \, \sqrt{-3 \, x^{2} + 4} x + \frac{2}{3} \, \sqrt{3} \arcsin\left(\frac{1}{2} \, \sqrt{3} x\right)

This is more complicated, but you could do it by hand with trig substitution.

Note: Sage's output is somewhat misleading here. Notice the xx right after the 4 under the square root? Is the xx inside or outside the square root?

Definite Integrals

To compute a definite integral in Sage, use the "integral" command with four arguments: integral(function, variable, lower limit, upper limit). In other words,

abf(x)dx=integral(f(x),x,a,b)\displaystyle\int_a^b f(x)\, dx=\verb|integral(f(x),x,a,b)|

Example 4

01x4exdx\int_0^1 x^4e^{-x}\, dx

integral(x^4*e^(-x),x,0,1) show(_)
-65*e^(-1) + 24
65e(1)+24\displaystyle -65 \, e^{\left(-1\right)} + 24

Here Sage has returned the exact answer. This integral could be computed by hand using integration by parts.

Let's convert the answer to a decimal:

N(-65*e^(-1) + 24)

Example 5

Sage can also handle definite integrals involving variables (the answer is not a number).

01ax2+bx+cdx\int_0^1 ax^2+bx+c\, dx

%var a,b,c show(integral(a*x^2+b*x+c,x,0,1))
13a+12b+c\displaystyle \frac{1}{3} \, a + \frac{1}{2} \, b + c

Example 6

1xcos(t)5dt\int_{-1}^x \cos(t)^5\, dt

%var t show(integral(cos(t)^5,t,-1,x))
15sin(1)5+15sin(x)523sin(1)323sin(x)3+sin(1)+sin(x)\displaystyle \frac{1}{5} \, \sin\left(1\right)^{5} + \frac{1}{5} \, \sin\left(x\right)^{5} - \frac{2}{3} \, \sin\left(1\right)^{3} - \frac{2}{3} \, \sin\left(x\right)^{3} + \sin\left(1\right) + \sin\left(x\right)

Example 7

Some functions do not have elementary antiderivatives. For example, consider 01ex2dx\int_0^1 e^{-x^2}\, dx

show(integral(e^(-x^2),x,0,1)) N(_)
12πerf(1)\displaystyle \frac{1}{2} \, \sqrt{\pi} \text{erf}\left(1\right)

Notice that Sage gives an exact answer involving a function called "erf." This is the "error function," defined as follows: erf(x)=2π0xet2dterf(x)=\frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\, dt

In other words, when we try to integrate ex2\displaystyle e^{-x^2}, Sage gives us an answer in terms of this very integral. Since we are computing a definite integral, Sage is able to give us a numerical approximation.

Numerical Approximation of Definite Integrals

For this and other examples where Sage can't find an exact answer, there is another command, numerical_integral, which provides a numerical approximation.

This command requires three arguments: the function to integrate, the lower bound of integration, and the upper bound of integration.

[Note: you do not specify the variable of integration, since there can be only one variable.]

Here is an example:

(0.746824132812427, 8.291413475940723e-15)

Notice that Sage returns an ordered pair. The first element is the numerical approximation of the integral, and the second is an estimate of the error in the approximation. Note the scientific notation in the error. In this example, the error is around 8.29×101508.29\times 10^{-15}\approx0.

Example 8

Here is one that the integral command can't do at all: 11sin(cos(x))dx\int_{-1}^1\sin(\cos(x))\, dx

integrate(sin(cos(x)), x, -1, 1)

So we'll try numerical_integral.

(1.4772859960737805, 1.6401169267974196e-14)

So the answer is approximately 1.4773.

Of course, numerical_integral can only be used for a definite integral.

Applied Examples

You will see many applications of integrals in Calculus 2. For now, we can talk about area and motion.

Example 9

Find the area under f(x)=tan(x2)f(x)=\tan(x^2) on the interval [0,1][0,1].

Solution: This area is 01f(x)dx0.3984\displaystyle\int_0^1 f(x)\, dx\approx 0.3984.

(0.39841444459716535, 4.423288897350168e-15)

Example 10

The net change in position is the integral of the velocity function. Suppose an object is traveling with velocity v(t)=3t4+2tv(t)=3t^4+2t meters per second. How far does the object travel from t=0t=0 to t=5t=5 seconds?

Solution: The distance traveled is 05v(t)dt=1900\displaystyle\int_0^5 v(t)\, dt=1900 meters.

%var t integral(3*t^4+2*t,t,0,5)