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).
5.1, 5.2, 5.3, and 5.4
The definite integral of $f(x)$ is defined as the limit of Riemann sums $$\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 $\int_a^b f(x) \ dx,$ we first choose $n$ (the number of subintervals) and set $\Delta x = (b-a)/n$ (the length of each subinterval). Next, we need to choose $x_k^*$ in each subinterval. Some popular choices are the left endpoint, the right endpoint, or the midpoint of each subinterval. We then increase $n$ to get better and better approximations. Of course, this leads to messy computations as there are $n$ 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)$ is an antiderivative of $f(x)$.}$$ 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)$. 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.
Let us first experiment with SageMath by having it calculate the Riemann sums of various functions. We will import the function from the package $\textbf{uofsc_calculus_labs}$ which will calculate the Riemann sum of $f(x)$ from $a$ to $b$ using $n$ subintervals and using either left endpoints, midpoints, or right endpoints. It will also graph $f(x)$ on $[a,b]$ and graph the rectangles used to calculate the Riemann sum.
To import the function from the package, use the command $\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 $\textbf{uofsc_caluculus_labs}$, then you will first need to install the package using $\textbf{pip}$. This can be done in SageMath by running the command below.
$\textbf{Note: } $If you are using a lab computer or the Binder server, then you will not be able to use $\textbf{%pip}$; however, $\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 $\textbf{Interactive Riemann Sums}$ function and use it to approximate the definite integral $$\displaystyle \int_2^{10} \dfrac{1}{x} \ dx$$ by calculating Riemann sums. Use various values for $n$ to see how larger values of $n$ lead to closer approximations for the definite integral.
Interactive_Riemann_Sums()
Now, we will use SageMath to evaluate $\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)$, an antiderivative of $f(x)$, using the $\textbf{integral}(\textit{function}, \textit{variable})$ command.
def f(x):
return 1/x
F(x) = integral(f(x),x)
F(x)
$\textbf{Caution}$: We know that $\displaystyle \int \dfrac{1}{x} \ dx = \ln|x| + C$. However, SageMath says that $\log(x)$ is an antiderivative of $\dfrac{1}{x}.$ This is because in higher level mathematics, $\log(x)$ represents the logarithm with base $e$ instead of the logarithm with base 10. We can check that this is the case in SageMath by evaluating $\log(e)$, which should return 1 if it really is the natural logarithm. Also, even though SageMath uses $\log(x)$ to mean the natural logarithm, it still recognizes $\ln(x)$ as the natural logarithm.
print(log(e))
print(ln(e))
Therefore, we see that SageMath is telling us that $\ln(x)$ is an antiderivative of $\dfrac{1}{x}.$ Now, we use the FTC to evaluate $\displaystyle \int_2^{10} \dfrac{1}{x} \ dx$ by calculating $F(10) - F(2)$.
F(10) - F(2)
Remember that we can get a decimal approximation of $\log(10) - \log(2)$ by either making each term involve floating point numbers, (add a decimal point), or by using the $\textbf{round}(\textit{expression},n)$ command.
print(F(10.) - F(2.))
print(round(F(10) - F(2), 10))
Thus, we have that $\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 $\textbf{integral}$ command, but with the added parameters of $a$ and $b$. Therefore, we will use the command $\textbf{integral}(\textit{function}, \textit{variable}, a, b)$.
integral(f(x), x, 2, 10)
Again, we see that $\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.0$ and $10.0$ instead of $2$ and $10$.
integral(f(x), x, 2.0, 10.0)
As stated in the introduction, it may be impossible to find a closed form of $F(x) = \int f(x) \ dx$. Try the example $f(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 $\textbf{erf}(x)$ and $\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.0$ and $10.0$ as our bounds instead of $2$ and $10$, 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.
For the following functions, use the $\textbf{Interactive Riemann Sums}$ function to approximate the definite integral using various values of $n$ and various endpoint rules. Then, use SageMath to find the exact value for the definite integral and compare with the approximations.