Lab 09 - Definite Integrals, Riemann Sums, and FTC
\n",
" \n",
"## Overview\n",
"\n",
"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).\n",
"\n",
"### Important SageMath Commands Introduced in this Lab\n",
"\n",
"\\begin{array}{|l|l|l|}\n",
"\\hline \n",
"\\hfill \\textbf{Command} \\hfill & \\hfill \\textbf{Description} \\hfill & \\hfill \\textbf{Example} \\hfill \\\\\n",
"\\hline\n",
"\\textbf{from} \\textit{ package } \\textbf{import} \\textit{ function} & \\text{Imports the } \\textit{function} \\text{ from the }\\textit{package.} & \\\\\n",
"\\hline\n",
"\\textbf{integrate}(\\textit{function}, \\textit{variable}) & \\text{Finds an antiderivative of the function} & \\textbf{integrate}(x^3, x) \\\\\n",
"\\hline\n",
"\\textbf{integrate}(\\textit{function}, \\textit{variable}, a, b) & \\text{Calculates $\\displaystyle \\int_a^b f(x) \\ dx$} & \\textbf{integrate}(x^3, x, 0, 5) \\\\\n",
"\\hline\n",
"\\end{array}\n",
"\n",
"### Related Course Material\n",
" 5.1, 5.2, 5.3, and 5.4\n",
"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. \n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"## Example 1\n",
"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.\n",
"\n",
"To import the function from the package, use the command $\\textbf{from} \\textit{ package } \\textbf{import} \\textit{ function}$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from uofsc_calculus_labs import Interactive_Riemann_Sums"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"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.\n",
"\n",
"$\\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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install uofsc_calculus_labs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from uofsc_calculus_labs import Interactive_Riemann_Sums"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Interactive_Riemann_Sums()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return 1/x\n",
"F(x) = integral(f(x),x)\n",
"F(x)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"$\\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. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(log(e))\n",
"print(ln(e))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"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)$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"F(10) - F(2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(F(10.) - F(2.))\n",
"print(round(F(10) - F(2), 10))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Thus, we have that $\\displaystyle \\int_2^{10} \\dfrac{1}{x} \\ dx = \\ln(10) - \\ln(2) \\approx 1.609.$\n",
"\n",
"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)$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"integral(f(x), x, 2, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Again, we see that $\\displaystyle \\int_2^{10} \\dfrac{1}{x} \\ dx = \\ln(10) - \\ln(2).$"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Note: We could make SageMath return the answer in decimal form by using $2.0$ and $10.0$ instead of $2$ and $10$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"integral(f(x), x, 2.0, 10.0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"## Notes / Remarks:\n",
"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}$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"integrate(ln(x)*e^(-x^2),x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"integrate(ln(x)*e^(-x^2),x, 2, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"integrate(ln(x)*e^(-x^2), x, 2.0, 10.0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"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."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"## Example 2\n",
"\n",
"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.\n",
"\n",
"1. $\\displaystyle \\int_2^6 x^3 \\ dx$\n",
"2. $\\displaystyle \\int_0^5 \\sqrt{x} \\ dx$\n",
"3. $\\displaystyle \\int_0^4 \\dfrac{x}{x+1} \\ dx$\n",
"4. $\\displaystyle \\int_0^4 \\dfrac{x}{x^4 + 1} \\ dx$\n",
"5. $\\displaystyle \\int_0^1 \\cos(x) \\ dx$\n",
"6. $\\displaystyle \\int_0^1 \\cos(\\sin(x^2)) \\ dx$\n",
"7. $\\displaystyle \\int_{-1}^3 e^{-x} \\ dx$\n",
"8. $\\displaystyle \\int_{-1}^3 xe^{-x} \\ dx$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 9.2",
"language": "sage",
"name": "sagemath"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}