CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
calculuslab

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: calculuslab/Calculus_Lab
Path: blob/main/142-Labs/Lab 06 - Improper Integrals.ipynb
Views: 491
Kernel: SageMath 9.2

Lab 06 - Improper Integrals

Overview

In this lab, we will use SageMath to demonstrate the individual steps involved in evaluating improper integrals.

Important SageMath Commands Introduced in this Lab

ParseError: KaTeX parse error: Undefined control sequence: \hfill at position 32: …|l|l|} \hline \̲h̲f̲i̲l̲l̲ ̲\textbf{Command…
Section 8.8

Example 1

First, we will evaluate an improper integral which has an unbounded integrand. Consider the integral 05ln(x) dx.\int_0^5 \ln(x) \ dx. We can determine if the integrand is unbounded in this interval by graphing it.

plot(ln(x), xmin = 0, xmax = 5)

Note that the integrand is unbounded as x0+.x \rightarrow 0^+. We can confirm this in SageMath by using the limit\textbf{limit} command.

limit(ln(x), x=0, dir="+")

We will reformulate this integral using limits as lima0+a5ln(x) dx.\lim_{a \rightarrow 0^+} \int_a^5 \ln(x) \ dx. First, we use SageMath to evaluate the integral. In order for SageMath to allow the variable aa as a bound, we must tell SageMath that aa is a number greater than 00 so that we avoid the infinite discontinuity. Also, since aa is a number close to 00, we also tell SageMath that a<1a < 1. We can do this using the assume\textbf{assume} command. Also, to see the integral, we use the option hold = true\textbf{hold = true} to keep SageMath from evaluating the integral and the show\textbf{show} command.

a = var('a') assume(a>0, a<1) show(integrate(ln(x),x, a, 5, hold = true))

Recall that SageMath uses log(x)\log(x) to mean ln(x)\ln(x), so this is the correct integral. Now we can evaluate the integral, assuming our assumptions are correct, by removing the hold = true\textbf{hold = true} statement.

f(a) = integrate(ln(x),x, a, 5) f(a)

Lastly, we use the limit\textbf{limit} command to compute the limit of the integral.

limit(f(a), a=0, dir="+")

It follows that 05ln(x) dx=5ln(5)5.\int_0^5 \ln(x) \ dx = 5\ln(5) - 5. SageMath is capable of handling improper integrals directly, so we can check our answer in SageMath by immediately evaluating the improper integral.

integrate(ln(x), x, 0, 5)

Example 2

Now, let's use SageMath to evaluate an improper integral over an infinite interval. Consider the integral xx2+1 dx.\int_{-\infty}^\infty \dfrac{x}{x^2 + 1} \ dx. Since both bounds of the integral are infinite, we need to first split the integral into two integrals which have one finite bound. Therefore, we first rewrite the integral as 0xx2+1 dx+0xx2+1 dx.\int_{-\infty}^0 \dfrac{x}{x^2 + 1} \ dx + \int_0^\infty \dfrac{x}{x^2 + 1} \ dx. Now, we can rewrite each integral in terms of limits as limaa0xx2+1 dx+limb0bxx2+1 dx.\lim_{a\rightarrow -\infty} \int_a^0 \dfrac{x}{x^2 + 1} \ dx + \lim_{b \rightarrow \infty} \int_0^b \dfrac{x}{x^2 + 1} \ dx. Let us create both of these integrals in SageMath. We have to be careful when using aa again since SageMath still remembers the assumptions about it that we made in the last problem. We can check this by using the assumptions()\textbf{assumptions()} command.

assumptions()

We can remove all current assumptions on our variables by using the forget()\textbf{forget()} command.

forget() assumptions()

Now that SageMath no longer has any assumptions on aa, we can use aa and add new assumptions as needed.

a,b = var('a', 'b') assume(a < 0) assume(b > 0) def f(x): return x/(x^2 + 1) show(integrate(f(x), x, a, 0, hold = true) + integrate(f(x), x, 0, b, hold = true))

Now, we will use SageMath to evaluate the limit of each of the integrals and add the results together.

Int1 = integrate(f(x), x, a, 0) Lim1 = limit(Int1, a=-infinity) Lim1

Note that we got that limaa0xx2+1 dx=.\lim_{a \rightarrow -\infty} \int_a^0 \dfrac{x}{x^2 + 1} \ dx = -\infty. Since one of our integrals diverges, we have that the entire integral diverges. Therefore, xx2+1 dx \displaystyle \int_{-\infty}^\infty \dfrac{x}{x^2 + 1} \ dx diverges.

Again, we can check this directly in SageMath. (Read the last line of the error message.)

integrate(f(x), x, -infinity, infinity)

Example 3

For our last example, we will use SageMath to determine for what values of pp does 11xp dx\displaystyle \int_1^\infty \dfrac{1}{x^p} \ dx converge. First, we rewrite the improper integral using limits as limb1b1xp dx\lim_{b\rightarrow \infty} \int_1^b \dfrac{1}{x^p} \ dx and use SageMath to get simplify the integral.

forget() b,p = var('b','p') assume(b>1) show(integrate(1/(x^p), x, 1, b, hold=true))

Note that if we try to evaluate this integral as is, SageMath will throw an error since it needs more information on the variable pp.

integrate(1/(x^p), x, 1, b)

We know that the integrand 1xp\dfrac{1}{x^p} has two different antiderivatives depending on whether p=1p = 1 or p1p \neq 1. Also, we can see that if p0p \leq 0, then the integrand is a non-decreasing function so the integral will diverge. Therefore, we will look at three cases: p>1p > 1, p=1p = 1, and 0<p<10 < p < 1. First, let's see what happens when p>1p>1.

assume(p>1) integrate(1/(x^p), x, 1, b)

We get an expression for the integral. Now let us calculate the limit and see if it converges or diverges.

limit(integrate(1/(x^p), x, 1, b), b=infinity)

Since we get an actual expression for the limit, we know that the improper integral converges when p>1p > 1. Now, let's see what happens when p=1p = 1.

assume(p == 1) ## Remember that when typing an equation, you must use == instead of =

SageMath tells us that this assumption is inconsistent since it is still assumping p>1p>1 and it is impossible for p>1p>1 and p=1p=1. Therefore, we need to first forget the assumption p>1p>1 and then assume p=1p=1.

forget(p > 1) assume(p == 1)

Now, let us evaluate the integral and limit when p=1p = 1.

integrate(1/(x^p), x, 1, b)
limit(integrate(1/(x^p), x, 1, b), b=infinity)

Since the limit diverges, we know that the improper integral diverges when p=1p = 1. Lastly, we will try 0<p<10 < p < 1.

forget(p == 1) assume(0 < p, p < 1) integrate(1/(x^p), x, 1, b)
limit(integrate(1/(x^p), x, 1, b), b = infinity)

Therefore, we see that the integral also diverges when 0<p<1.0 < p < 1. To summarize, we have that $$\int_1^\infty \dfrac{1}{x^p} \ dx = \left \{ \begin{array}{rl} \dfrac{1}{p-1} & \text{if $p > 1$} \ \text{diverges} & \text{if p1p \leq 1} \end{array} \right . .$$

Example 4

Repeat the above three examples for the following 3 integrals:

  1. 0π/2sec(x) dx\displaystyle \int_0^{\pi/2} \sec(x) \ dx

  2. 0xex dx\displaystyle \int_0^\infty xe^{-x} \ dx

  3. e1x(ln(x))p dx\displaystyle \int_e^\infty \dfrac{1}{x(\ln(x))^p} \ dx