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/141-Labs/Lab 09 - Definite Integrals, Riemann Sums, and FTC.ipynb
Views: 491
Kernel: SageMath 9.2

Lab 09 - Definite Integrals, Riemann Sums, and FTC

Overview

In this lab, we will explore the connection between the definite integral as defined and computed via Riemann sums with the indefinite integral via the Fundamental Theorem of Calculus (FTC).

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…
5.1, 5.2, 5.3, and 5.4

The definite integral of f(x)f(x) is defined as the limit of Riemann sums abf(x) dx=limnk=1nf(xk)Δx.\displaystyle \int_a^b f(x) \ dx = \lim_{n\rightarrow \infty} \sum_{k=1}^n f(x_k^*) \Delta x. To use the above definition to compute or estimate abf(x) dx,\int_a^b f(x) \ dx, we first choose nn (the number of subintervals) and set Δx=(ba)/n\Delta x = (b-a)/n (the length of each subinterval). Next, we need to choose xkx_k^* in each subinterval. Some popular choices are the left endpoint, the right endpoint, or the midpoint of each subinterval. We then increase nn to get better and better approximations. Of course, this leads to messy computations as there are nn terms in the sum and a closed form is in general very hard to find.

A completely different way to compute definite integrals is to use the FTC, $$\displaystyle \int_a^b f(x) \ dx = F(b) - F(a), \ \ \ \ \text{ where $F(x)isanantiderivativeof is an antiderivative of f(x)ParseError: KaTeX parse error: Expected 'EOF', got '}' at position 2: .}̲TheFTCrelatesdefiniteintegrals(whicharenumbersassignedareas)toindefiniteintegrals(whicharefunctionsasantiderivatives).Thisisgreatifyouknowhowtofind The FTC relates definite integrals (which are numbers as signed areas) to indefinite integrals (which are functions as antiderivatives). This is great if you know how to find F(x).Theproblemisthatsometimesitcanbeverydifficult(orevenimpossible)tofindaclosedformof. The problem is that sometimes it can be very difficult (or even impossible) to find a closed form of F(x) = \int f(x) \ dx.$ SageMath is very capable of finding indefinite integrals but don't be surprised if it fails.

Example 1

Let us first experiment with SageMath by having it calculate the Riemann sums of various functions. We will import the function from the package ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 14: \textbf{uofsc_̲calculus_labs} which will calculate the Riemann sum of f(x)f(x) from aa to bb using nn subintervals and using either left endpoints, midpoints, or right endpoints. It will also graph f(x)f(x) on [a,b][a,b] and graph the rectangles used to calculate the Riemann sum.

To import the function from the package, use the command from package import function\textbf{from} \textit{ package } \textbf{import} \textit{ function}.

from uofsc_calculus_labs import Interactive_Riemann_Sums

If SageMath returns the error stating that there is no module named ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 14: \textbf{uofsc_̲caluculus_labs}, then you will first need to install the package using pip\textbf{pip}. This can be done in SageMath by running the command below.

Note: \textbf{Note: } If you are using a lab computer or the Binder server, then you will not be able to use ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{%pip}; however, ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 14: \textbf{uofsc_̲calculus_labs} should already be installed.

%pip install uofsc_calculus_labs
from uofsc_calculus_labs import Interactive_Riemann_Sums

Once you have the function imported, call the Interactive Riemann Sums\textbf{Interactive Riemann Sums} function and use it to approximate the definite integral 2101x dx\displaystyle \int_2^{10} \dfrac{1}{x} \ dx by calculating Riemann sums. Use various values for nn to see how larger values of nn lead to closer approximations for the definite integral.

Interactive_Riemann_Sums()

Now, we will use SageMath to evaluate 2101x dx\displaystyle \int_2^{10} \dfrac{1}{x} \ dx via the FTC in two different ways and compare the results to our Riemann sum approximations above. First, let us use SageMath to find F(x)F(x), an antiderivative of f(x)f(x), using the integral(function,variable)\textbf{integral}(\textit{function}, \textit{variable}) command.

def f(x): return 1/x F(x) = integral(f(x),x) F(x)

Caution\textbf{Caution}: We know that 1x dx=lnx+C\displaystyle \int \dfrac{1}{x} \ dx = \ln|x| + C. However, SageMath says that log(x)\log(x) is an antiderivative of 1x.\dfrac{1}{x}. This is because in higher level mathematics, log(x)\log(x) represents the logarithm with base ee instead of the logarithm with base 10. We can check that this is the case in SageMath by evaluating log(e)\log(e), which should return 1 if it really is the natural logarithm. Also, even though SageMath uses log(x)\log(x) to mean the natural logarithm, it still recognizes ln(x)\ln(x) as the natural logarithm.

print(log(e)) print(ln(e))

Therefore, we see that SageMath is telling us that ln(x)\ln(x) is an antiderivative of 1x.\dfrac{1}{x}. Now, we use the FTC to evaluate 2101x dx\displaystyle \int_2^{10} \dfrac{1}{x} \ dx by calculating F(10)F(2)F(10) - F(2).

F(10) - F(2)

Remember that we can get a decimal approximation of log(10)log(2)\log(10) - \log(2) by either making each term involve floating point numbers, (add a decimal point), or by using the round(expression,n)\textbf{round}(\textit{expression},n) command.

print(F(10.) - F(2.)) print(round(F(10) - F(2), 10))

Thus, we have that 2101x dx=ln(10)ln(2)1.609.\displaystyle \int_2^{10} \dfrac{1}{x} \ dx = \ln(10) - \ln(2) \approx 1.609.

Another way in which SageMath can evaluate the definite integral is to use the same integral\textbf{integral} command, but with the added parameters of aa and bb. Therefore, we will use the command integral(function,variable,a,b)\textbf{integral}(\textit{function}, \textit{variable}, a, b).

integral(f(x), x, 2, 10)

Again, we see that 2101x dx=ln(10)ln(2).\displaystyle \int_2^{10} \dfrac{1}{x} \ dx = \ln(10) - \ln(2).

Note: We could make SageMath return the answer in decimal form by using 2.02.0 and 10.010.0 instead of 22 and 1010.

integral(f(x), x, 2.0, 10.0)

Notes / Remarks:

As stated in the introduction, it may be impossible to find a closed form of F(x)=f(x) dxF(x) = \int f(x) \ dx. Try the example f(x)=(ln(x))ex2f(x) = (\ln(x))e^{-x^2}.

integrate(ln(x)*e^(-x^2),x)
integrate(ln(x)*e^(-x^2),x, 2, 10)

Both outputs contain the functions erf(x)\textbf{erf}(x) and hypergeometric()\textbf{hypergeometric}(\dots) which are not functions with closed form but instead an error function and an infinite series. The good news, however, is that if we use 2.02.0 and 10.010.0 as our bounds instead of 22 and 1010, then SageMath will use Riemann sums to give an approximation of what the actual answer is.

integrate(ln(x)*e^(-x^2), x, 2.0, 10.0)

Therefore, even if it is impossible to find a closed form for an antiderivative of a function, SageMath can still give us a close approximation with very little trouble.

Example 2

For the following functions, use the Interactive Riemann Sums\textbf{Interactive Riemann Sums} function to approximate the definite integral using various values of nn and various endpoint rules. Then, use SageMath to find the exact value for the definite integral and compare with the approximations.

  1. 26x3 dx\displaystyle \int_2^6 x^3 \ dx

  2. 05x dx\displaystyle \int_0^5 \sqrt{x} \ dx

  3. 04xx+1 dx\displaystyle \int_0^4 \dfrac{x}{x+1} \ dx

  4. 04xx4+1 dx\displaystyle \int_0^4 \dfrac{x}{x^4 + 1} \ dx

  5. 01cos(x) dx\displaystyle \int_0^1 \cos(x) \ dx

  6. 01cos(sin(x2)) dx\displaystyle \int_0^1 \cos(\sin(x^2)) \ dx

  7. 13ex dx\displaystyle \int_{-1}^3 e^{-x} \ dx

  8. 13xex dx\displaystyle \int_{-1}^3 xe^{-x} \ dx