Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Appendix F — Calculus tutorial

Calculus is the study of the properties of functions. We use calculus to describe the limit behaviour of functions, calculate their rates of change, and calculate the areas under their graphs. In this tutorial, you’ll learn about all these operations through math formulas, interactive visualizations, and code examples.

Click the binder button Binder or this link bit.ly/calctut3 to run this notebooks interactively.

Notebook setup

Note: you may need to restart the kernel to use updated packages.

Introduction

Example: car trip

Suppose you’re driving to see a friend in a neighbouring town. The total length of the journey is 27000 m (27 km) along a straight road. We can describe your position over time as a function x(t)x(t), and use the car’s trip odometer to measure the distance travelled. Right before you leave, which we’ll call t=0t=0, you reset the trip odometer so it reads x(0)=0x(0)=0 m.

Velocity

The derivative function x′ ⁣(t)x^{\prime\!}(t), pronounced “xx prime,” describes how the function x(t)x(t) changes over time. In this example x′ ⁣(t)x^{\prime\!}(t) is the car’s velocity. If your current velocity is x′ ⁣(t)=15x^{\prime\!}(t)=15 m/s, then the car’s position x(t)x(t) is increasing by 15 m each second. If you maintain this velocity, the position will increase at a constant rate: x(0)=0x(0)=0 m, x(1)=15x(1)=15 m, x(2)=30x(2)=30 m, and so on until t=1800t=1800 s when you’ll have travelled 1800×15=270001800\times 15 = 27000 m and reached your destination.

To estimate the time remaining in the trip, assuming the velocity x′ ⁣(t)x^{\prime\!}(t) stays constant, you can divide the remaining distance by the current velocity:

time remaining at t    =  27000−x(t)x′ ⁣(t)      s.\text{time remaining at } t \; \; =\; \frac{ 27000 - x(t) }{ x^{\prime\!}(t) } \;\;\; \textrm{s}.

The bigger the derivative x′ ⁣(t)x^{\prime\!}(t), the faster you’ll arrive. If you drive two times faster, the time remaining will be halved.

Inverse problem

Imagine that the car’s odometer is busted and you don’t have any way to measure the distance x(t)x(t) you have travelled over time. The car’s speedometer is still working though, so you know the velocity x′ ⁣(t)x^{\prime\!}(t) at all times. Is there a way to calculate the distance travelled using only the information from the speedometer?

There is! You can infer the position at time tt from the velocity x′ ⁣(t)x^{\prime\!}(t). Think about it---if the speedometer reports x′ ⁣(t)=15x^{\prime\!}(t)=15 m/s, then you know that the car’s position is increasing at the rate of 15 m each second. We can describe the total distance travelled until time t=τt=\tau (the Greek letter tau) as the integral of the velocity function x′ ⁣(t)x^{\prime\!}(t) between t=0t=0 and t=τt=\tau:

x(τ)  =  ∫t=0t=τx′ ⁣(t) dt.x(\tau) \; = \; \int_{t=0}^{t=\tau} x^{\prime\!}(t)\, dt.

The integral symbol ∫\int is an elongated SS that stands for sum. To calculate the total distance travelled, we imagine splitting the time between t=0t=0 and t=τt=\tau into many short time intervals dtdt. During each instant, the position increases by x′ ⁣(t)dtx^{\prime\!}(t)dt m, where x′ ⁣(t)x^{\prime\!}(t) is the velocity (measured in m/s), and dtdt is the time interval (measured in seconds). The integral ∫0τx′ ⁣(t) dt\int_{0}^{\tau} x^{\prime\!}(t)\, dt calculates the “sum” of these x′ ⁣(t) dtx^{\prime\!}(t)\,dt contributions accumulated between t=0t=0 and t=τt=\tau.

The situation described in the car example shows that calculus concepts are not theoretical constructs reserved only for math specialists, but common ideas you encounter every day. The derivative q′ ⁣(t)q^{\prime\!}(t) describes the rate of change of the quantity q(t)q(t). The integral ∫abq(t)dt\int_a^b q(t)dt measures the total accumulation of the quantity q(t)q(t) during the time period from t=at=a to t=bt=b.

Doing calculus: then and now

Symbolic calculations using pen and paper

The pen-and-paper approach is a good way to learn calculus because manipulating math symbols “by hand” develops your intuitive understanding of calculus procedures. Writing math on paper allows you to use high-level abstractions and arrive at exact symbolic answers.

Symbolic calculations using SymPy

The Python library SymPy allows you to do symbolic math calculations on a computer. We’ll now illustrate the main SymPy capabilities through some examples.

To use SymPy, we start by importing the sympy module under the alias sp, then define the symbolic variable x.

The symbols function creates SymPy symbolic variables. Unlike ordinary Python variables that hold a particular value, SymPy variables act as placeholders that can take on any value. We use symbolic variables to write expressions, just like we do in pen-and-paper calculations.

Loading...

SymPy is very useful when learning math because it provides functions for common math operations. For example, to factor the expression expr, we can call the function sp.factor.

Loading...

You can substitute a particular <value> for the variable x\tt{x} into the expression expr by calling the method expr.subs() and passing in a python dictionary object {x:<value>}.

Loading...

We can also also ask SymPy to solve the equation expr = 0, which means to find the values of xx that satisfy the equation 4−x2=04 - x^2 = 0.

Loading...

The equation has two solutions so solve returns a list.

Here is another example, where we use sp.solve to solve the quadratic equation x2+2x−8=0x^2 + 2x - 8 = 0.

Loading...

We can check that x=2x = 2 and x=−4x = -4 satisfy the equation x2+2x−8=0x^2 + 2x - 8 = 0:

  • When x=2x = 2, we get x2+2x−8=(2)2+2(2)−8=4+4−8=0x^2 + 2x - 8 =(2)^2 + 2(2) - 8 = 4 + 4 - 8 = 0.

  • When x=−4x = -4, we get x2+2x−8=(−4)2+2(−4)−8=16−8−8=0x^2 + 2x - 8 =(-4)^2 + 2(-4) - 8 = 16 - 8 - 8 = 0.

Another useful function is sp.simplify, which you can use to simplify any math expression.

Loading...

SymPy combined the 5x5x term with the −3x-3x term to obtain the 2x2x term in the simplified output expression.

Sum{y can do many other math operations. We’ll discuss the SymPy functions for doing calculus in the next sections. To learn more about SymPy, I invite you to check out the SymPy tutorial.

Numerical computing using NumPy

The Python library NumPy makes it easy to do numerical calculations on a computer. Let’s start by importing the numpy module under the alias np, which is the usual convention.

Her is how e can compute a numerical approximation to 2\sqrt{2}.

Loading...

NumPy makes it easy to work with arrays of numbers, which are conceptually similar to Python lists, but enable powerful parallel calculations. To give you some idea of this power, we’ll now show create an array of 1001 numbers, and apply a function to each input in this array, to obtain an array of 1001 outputs using just a few lines of code.

We start by calling the NymPy function np.linspace to create the array of smoothly varying inputs. We specify the starting point 0, the end point 2, and number of values we want in the array.

array([0. , 0.002, 0.004, ..., 1.996, 1.998, 2. ])

Next, we define a Python function h that corresponds to the math function h(x)=4−x2h(x) = 4 - x^2.

We can now apply this function to the array of inputs xs to obtain an array of outputs hxs.

CPU times: user 34 μs, sys: 17 μs, total: 51 μs
Wall time: 42.9 μs
array([4. , 3.999996, 3.999984, ..., 0.015984, 0.007996, 0. ])

Note that single call to h(xs) applied the function h to all 1001 inputs in the array xs at once. This is called vectorization, and one of the great powers of NumPy: we can write simple math expression that describe what we want to do for each input x, and then NumPy performs these mathematical operations on entire arrays at once, eliminating the need for writing for-loops.

Using NumPy vectorization not only makes numeric code easier to read, but also also makes it run faster! To illustrate this speed up, let’s look at the equivalent Python code for applying h to the list of inputs using Python.

4.0 3.999996 3.999984 ... 0.015983999999999998 0.007995999999999892 0.0
CPU times: user 244 μs, sys: 18 μs, total: 262 μs
Wall time: 258 μs

Comparing the timing information output of this cell to the timing information shown earlier, we see that the basic Python version took roughly 10 times longer than the NymPy code.

Scientific computing using SciPy

The Python module SciPy is a toolbox of scientific computing helper functions. For example, computing the integral ∫02h(x)dx\int_{0}^2 h(x)dx, which corresponds to the area under the graph of the function h(x)=4−x2h(x) = 4 - x^2 between x=0x=0 and x=2x=2 requires only two lines of code.

Loading...

The helper function quad is a powerful tool that the ancients could only dream of.

Applications of calculus

We use calculus concepts to describe various quantities in physics, chemistry, biology, engineering, machine learning, business, economics and other domains where quantitative analysis is used. Many laws of nature are expressed in terms of derivatives and integrals, so it’s essential that you learn the language of calculus if you want to study science.

Math prerequisites

Let’s start with a review of some key ideas from high school math, which we’ll need as building blocks for calculus.

Sets notation

Sets are collections of math objects. Many math ideas are expressed in the language of sets, so it’s worth knowing the notation for sets.

  • { definition }\{ \textrm{ definition } \}: we use curly brackets to define sets. The definition in the curly brackets is either a description of the set’s contents, or a list of the elements in the set.

  • N\mathbb{N}: the set of natural numbers N={0,1,2,3,4,5,…}\mathbb{N} = \{0,1,2,3,4,5,\ldots\}.

  • N+\mathbb{N}_+: the positive natural numbers N+={1,2,3,4,5,…}\mathbb{N}_+ = \{1,2,3,4,5,\ldots\}.

  • R\mathbb{R}: the set of real numbers.

  • R+\mathbb{R}_+: the set of nonnegative real numbers.

  • x∈Sx \in S: this statement is read “xx is an element of SS.” We use this notation to indicate the “type” of the variable xx. For example, writing “x∈Rx \in \mathbb{R}” tells us xx is a real number.

We can use the set-builder notation { ⋅  ∣  ⋅ }\{\, \cdot \; | \; \cdot \, \} to define new sets. Inside the curly brackets, we first describe the general kind of mathematical objects we are talking about, followed by the symbol “∣|” (which stands for “such that”), followed by the conditions that identify the elements of the set. For example, the set of nonnegative real numbers R+\mathbb{R}_+ is defined as “all real numbers xx such that x≥0x \geq 0,” which is expressed compactly as R+={x∈R  ∣  x≥0}\mathbb{R}_+ = \{ x \in \mathbb{R} \; | \; x \geq 0 \} using the set-builder notation.

Python sets

The statement x∈Sx \in S means xx is one of the elements of the set SS.

In Python, we can use the in operator to check if a value is inside the set.

True

The in statement returns False for a value that are not in the set.

False

We can perform set operations like union, intersection, and set difference as follows:

S ∪ T = {1, 2, 3, 4, 5, 6}
S ∩ T = {3}
S \ T = {1, 2}

The number line

The number line is a visual representation of the set of real numbers R\mathbb{R}

The number line showing equally spaced notches

The set of real numbers includes the natural numbers {0,1,2,3,…}\{0,1,2,3,\ldots\}, the integers {…,−3,−2,−1,0,1,2,3,…}\{\ldots, -3,-2,-1,0,1,2,3,\ldots\}, rational numbers like −32-\frac{3}{2}, 0.5, and 92\frac{9}{2}, as well as irrational numbers like 2\sqrt{2}, ee, and π\pi. All the numbers you will run into when doing math can be visualized as a point on the number line.

Infinity

The math symbol ∞\infty describes the concept of infinity. We use ∞\infty to represent an infinitely large quantity, that is greater than any number you can think of. Geometrically speaking, you can imagine the number line extends to the right forever towards infinity, as illustrated in the number line figure above. The number line also extends forever to the left, which we denote as negative infinity −∞-\infty.

Infinity is not a number but a process. When we use the symbol +∞+\infty, we’re describing the process of moving to the right on the number line forever. We go past larger and larger positive numbers and never stop.

Infinity is a key concept in calculus, so it’s important that we develop a precise language to talk about infinitely large numbers and procedures with an infinite number of steps, which we’ll do in the section on limits below.

Functions

In Python, we define functions using the def keyword. For example, the code cell below defines the function f(x)=x2f(x) = x^2, then evaluate it for the input x=3x=3.

Loading...

Function graphs

The graph of the function f(x)f(x) is obtained by plotting a curve that passes through the set of input-output coordinate pairs (x,f(x))(x, f(x)), as xx varies over a range of inputs.

The function linspace(-3,3,1001) creates an array of 1001 points in the between -3 and 3. We store this sequence of inputs into the variable xs. Next, we compute the output value f(x)f(x) for each of the inputs xx in the array xs and store the result in the array fxs.

We can now use the function sns.lineplot() to generate the plot.

<Figure size 500x170 with 1 Axes>

The helper function plot_func from the ministats library performs the same three steps and produces the same output.

<Figure size 500x170 with 1 Axes>

Inverse functions

The inverse function f−1f^{-1} performs the inverse operation of the function ff. If you start from some number xx, apply ff, then apply f−1f^{-1}, you’ll arrive—full circle—back to the original number xx:

f−1 ⁣(  f(x)  )=x.f^{-1}\!\big( \; f(x) \; \big) = x.

The inverse of the function f(x)=x2f(x) = x^2 is the square root function f−1(x)=xf^{-1}(x) = \sqrt{x}. Earlier we computed f(3)=9f(3) = 9. If we apply the inverse function to 9, we get f−1(9)=9=3f^{-1}(9) = \sqrt{9} = 3.

Loading...

Two other important inverse functions are the exponential function exe^x and logarithmic functions log⁡(x)\log(x). The logarithmic function log⁡(x)\log(x) is the inverse of the exponential function exe^x, and vice versa. If we compute the exponential of any number, then apply the logarithmic function, we get back the original number.

Loading...

Similarly, if we take the logarithm of any positive number, then apply the exponential function on the result, we get back to the original number.

Loading...

Function properties

We use the notation f ⁣:A→Bf \colon A \to B to denote a function from the input set AA to the output set BB.

  • AA is the domain of ff: the set of allowed inputs for the function.

  • BB is the image of ff: the set of possible outputs of the function.

For example, the domain of the function f(x)=x2f(x) = x^2 is R\mathbb{R} (any real number) and its image is R+\mathbb{R}_+ (nonnegative real numbers), so we say ff is a function of the form f ⁣:R→R+f \colon \mathbb{R} \to \mathbb{R}_+.

Inventory of functions

We’ll now go over a few commonly used math functions and look at their graphs.

Constant function

<Figure size 500x170 with 1 Axes>

Linear function

<Figure size 500x170 with 1 Axes>

Quadratic function

<Figure size 500x170 with 1 Axes>

Polynomial functions

<Figure size 500x170 with 1 Axes>

Exponential function

<Figure size 500x170 with 1 Axes>

Absolute value function

<Figure size 500x170 with 1 Axes>

Square root function

<Figure size 500x170 with 1 Axes>

Logarithmic function

<Figure size 500x170 with 1 Axes>

Functions with discrete inputs

Later in this tutorial, we’ll study functions with discrete inputs, ak:N→Ra_k : \mathbb{N} \to \mathbb{R}, which are called sequences. We can express sequences by writing the first few values the sequence [a0,a1,a2,a3,…][a_0, a_1, a_2, a_3, \ldots], which correspond to evaluating aka_k for k=0k=0, k=1k=1, k=2k=2, k=3k=3, etc.

Geometry of rectangles and triangles

The area of a rectangle of base bb and height hh is A=bhA = bh.

The area of a triangle is equal to 12\frac{1}{2} times the length of its base bb times its height hh: A=12bhA = \tfrac{1}{2} b h.

Trigonometric functions

The trigonometric functions sin⁡θ\sin\theta and cos⁡θ\cos\theta are used in physics and engineering.

Sine function

The trigonometric functions np.sin and np.cos take inputs in radians. For example, if you need to compute sin⁡(π6  rad)\sin(\frac{\pi}{6}\;\textrm{rad}) you can simplay call the function np.sin on the input np.pi/6:

Loading...

To compute sin⁡(30∘)\sin(30^\circ), you need to fist use the conversion factor of π  rad180∘\frac{\pi\;\textrm{rad}}{180^\circ} to convert the angle from degrees to radians. In angle θ=30∘\theta = 30^\circ is θ=30∘π  rad180∘\theta = 30^\circ \frac{\pi\;\textrm{rad}}{180^\circ} in radians.

Loading...

Let’s plot the graph of f(θ)=sin⁡θf(\theta) = \sin\theta over two full cycles.

<Figure size 500x170 with 1 Axes>

Cosine function

The cosine function is the same as the sine function shifted by π2\frac{\pi}{2} to the left: f(θ)=cos⁡(θ)=sin⁡(θ+π2)f(\theta) = \cos(\theta) = \sin(\theta+\frac{\pi}{2}).

<Figure size 500x170 with 1 Axes>

Overview and a look ahead

The concept map below shows an overview of the calculus ideas you’ll learn in the rest of the tutorial.

Concept maps showing calculus topics

Limits

We use limits to describe infinitely large quantities, infinitely small quantities, and procedures with infinitely many steps. Limits are an essential theoretical building block for calculus. We use limits to define derivatives, integrals, and series, so this is where we’ll start.

Example 1: Archimedes’ approximation to π\pi

Suppose we want to calculate the area of a circle of radius r=1r=1. Today we know the formula for the are of the circle is A∘=πr2A_{\circ} = \pi r^2, and the area of this circleis π\pi, but let’s imagine that we’re in ancient times and don’t know the formula.

Sometime around 250 BCE, Archimedes of Syracuse came up with a clever idea to approximate the area of the circle using a regular polygon with nn sides inscribed in the circle. The figure below shows the hexagonal (6-sides), octagonal (8-sides), and dodecagonal (12-sides) approximations to the circle.

Approximations to the area of circle using a hexagon, an octagon, and a dodecagon inscribed inside a circle of radius r.

Part (b) of the figure highlights one of the 16 identical triangular slices for the case n=8n=8. The hypotenuse of this triangle has length rr, the angle θ\theta is 360∘16=2π16=π8 \frac{360\circ}{16} = \frac{2\pi}{16} = \frac{\pi}{8}\, rad, and its sides have length a=rcos⁡(π8)a=r\cos(\frac{\pi}{8}) and b=rsin⁡(π8)b=r\sin(\frac{\pi}{8}). The area of this triangle is therefore At=12ab=12rcos⁡(π8)rsin⁡(π8)A_t = \frac{1}{2}ab=\frac{1}{2}r\cos(\frac{\pi}{8})r\sin(\frac{\pi}{8}), and the area of the octagon is 16At=8r2cos⁡(π8)sin⁡(π8)16A_t = 8r^2\cos(\frac{\pi}{8})\sin(\frac{\pi}{8}).

More generally, if we draw a polygon with nn sides inscribed inside of the circle, the total area of the 2n2n triangles will be:

A(n)=2n⋅12ab=n rcos⁡(πn) rsin⁡(πn)=n r2cos⁡(πn)sin⁡(πn).A(n) = 2n \cdot \tfrac{1}{2}ab = n \, r\cos(\tfrac{\pi}{n}) \, r\sin(\tfrac{\pi}{n}) = n \, r^2 \cos(\tfrac{\pi}{n})\sin(\tfrac{\pi}{n}).

The nn-sided-polygon approximation to the area of the circle becomes more and more accurate as nn grows. Here is the code for computing the approximations to the area of a circle with an increasing number of sides.

n=     6,  A(n)= 2.5980762114,   error=-0.54351644
n=     8,  A(n)= 2.8284271247,   error=-0.31316553
n=    12,  A(n)= 3.0000000000,   error=-0.14159265
n=    50,  A(n)= 3.1333308391,   error=-0.00826181
n=   100,  A(n)= 3.1395259765,   error=-0.00206668
n=  1000,  A(n)= 3.1415719828,   error=-0.00002067
n= 10000,  A(n)= 3.1415924469,   error=-0.00000021

In the limit as n→∞n \to \infty, the nn-sided-polygon approximation to the area of the circle will becomes exactly equal to the area

lim⁡n→∞A(n)=πr2.\lim_{n \to \infty} A(n) = \pi r^2.

Example 2: Euler’s number

A load with annual interest rate of 100% and compounding twice per year will grow by (1+50%)(1+50%)=(1+12)(1+12)=2.25(1+50\%)(1+50\%) = (1+\frac{1}{2})(1+\frac{1}{2}) = 2.25 by the end of the year:

Loading...

If the bank computes the interest three times per year, the amount owed after one year is (1+13)(1+13)(1+13)=2.370(1+\frac{1}{3})(1+\frac{1}{3})(1+\frac{1}{3}) = 2.370.

Loading...

With quarterly compounding:

Loading...

Monthly compounding:

Loading...

Daily compounding:

Loading...

Compounding 1000 times per year:

Loading...

In the limit as n→∞n \to \infty, the expression (1+1n)n(1+\frac{1}{n})^n converges to Euler’s number ee:

lim⁡n→∞(1+1n)n=e=2.718281828459045235360287471352….\lim_{n\to\infty}\left(1+\frac{1}{n}\right)^n = e = 2.718281828459045235360287471352\ldots.

Limits at infinity

Limits are useful for describing the behaviour of functions.

Example 3

Consider the function f(x)=1xf(x) = \frac{1}{x}. What happens to f(x)f(x) as xx goes to infinit? As xx becomes larger and larger, the fraction 1x\frac{1}{x} becomes smaller and smaller. In the limit where xx goes to infinity, 1x\frac{1}{x} approaches zero: lim⁡x→∞1x=0\lim_{x\to\infty}\frac{1}{x} = 0. We can see this by looking at the graph of f(x)f(x).

<Figure size 500x170 with 1 Axes>

Limit formulas

The limit of the sum, difference, product, and quotient of two functions are computed as follows:

lim⁡x→∞(f(x)+g(x))=lim⁡x→∞f(x)+lim⁡x→∞g(x),lim⁡x→∞(f(x)−g(x))=lim⁡x→∞f(x)−lim⁡x→∞g(x),lim⁡x→∞f(x)g(x)=lim⁡x→∞f(x)⋅lim⁡x→∞g(x),lim⁡x→∞(f(x)/g(x))=lim⁡x→∞f(x) /lim⁡x→∞g(x) .\begin{align*} \lim_{x \to \infty} (f(x) + g(x)) & = \lim_{x \to \infty} f(x) + \lim_{x \to \infty} g(x), \\ \lim_{x \to \infty} (f(x) - g(x)) & = \lim_{x \to \infty} f(x) - \lim_{x \to \infty} g(x), \\ \lim_{x \to \infty} f(x)g(x) & = \lim_{x \to \infty} f(x) \cdot \lim_{x \to \infty} g(x), \\ \lim_{x \to \infty} (f(x) / g(x)) & = \lim_{x \to \infty} f(x) \, / \lim_{x \to \infty} g(x) \, . % \lim_{x \to \infty} \frac{f(x)}{g(x)} & = \frac{ \displaystyle \lim_{x \to \infty} f(x) } { \displaystyle \lim_{x \to \infty} g(x)} \, . \end{align*}

Example 4

Calculate lim⁡x→∞2x+1x \lim_{x\to \infty} \frac{2x+1}{x}\,. We’re given the function g(x)=2x+1xg(x)=\frac{2x+1}{x} and must determine what the function looks like for very large values of xx. We can rewrite the function as 2x+1x=2+1x\frac{2x+1}{x}=2+\frac{1}{x} then apply the sum formula for limits:

lim⁡x→∞ ⁣ ⁣2x+1x=lim⁡x→∞ ⁣(2+1x)=lim⁡x→∞2+ ⁣lim⁡x→∞1x=2+0=2.\lim_{x\to \infty}\!\! \tfrac{2x+1}{x} = \lim_{x\to \infty}\!\Big( 2 + \tfrac{1}{x} \Big) = \lim_{x\to \infty} 2 + \!\lim_{x\to \infty} \tfrac{1}{x} = 2 + 0 = 2.

As xx goes to infinity, 1x\frac{1}{x} goes to 0, so the second term vanishes, leaving only the 2. We can confirm this visually by looking at the graph of g(x)g(x).

<Figure size 500x170 with 1 Axes>

Limits at a point

We can use limit expressions to describe what happens when a function approaches some number x=ax=a. The limit expression lim⁡x→a+f(x)\lim_{x \to a^+} f(x) describes the behaviour of the function f(x)f(x) as xx approaches aa from the right. To compute this limit, we imagine a small distance δ\delta (the Greek letter delta) that that gets smaller and smaller, and compute the following equivalent limit expression

lim⁡x→a+f(x)=lim⁡δ→0f(a+δ).\lim_{x\to a^+} f(x) = \lim_{\delta \to 0} f(a + \delta).

This limit expression describes the value of the function ff for inputs like a+0.1a+0.1, a+0.01a+0.01, a+0.001a+0.001, a+0.0001a+0.0001, etc. In other words, the input xx gets closer and closer to aa (from the right) without ever touching x=ax=a.

Example

Consider the function f(x)=1xf(x) = \frac{1}{x} again. The limit lim⁡x→0+f(x)\lim_{x \to 0^+} f(x), read “the limit of f(x)f(x) as xx goes to zero,” asks us to evaluate the function ff for inputs like x=0.1x=0.1, x=0.01x=0.01, x=0.001x=0.001, x=0.0001x=0.0001, etc. The outputs of f(x)= 1xf(x) =~\frac{1}{x} get larger and larger as xx takes on smaller and smaller values, so the limit goes to infinity:

lim⁡x→0+1x=lim⁡δ→010+δ=lim⁡δ→01δ=∞.\lim_{x\to 0^+} \tfrac{1}{x} = \lim_{\delta \to 0} \tfrac{1}{0^+\delta} = \lim_{\delta \to 0} \tfrac{1}{\delta} = \infty.

In words, the function f(x)=1xf(x) = \frac{1}{x} “blows up” to infinity as xx goes to 0+0^+, as shown below.

<Figure size 500x170 with 1 Axes>

The limit of f(x)f(x) when xx approaches aa from the left is defined analogously, lim⁡x→a−f(x)=lim⁡δ→0f(a−δ)\lim_{x\to a^-} f(x) = \lim_{\delta \to 0} f(a - \delta). If both limits from the left and from the right at x=ax=a exist and are equal to each other, we can drop the +^+ and −^- annotations, and talk about the limit x→ax\to a:

lim⁡x→af(x)=lim⁡x→a+f(x)=lim⁡x→a−f(x).\lim_{x\to a} f(x) = \lim_{x\to a^+} f(x) = \lim_{x\to a^-} f(x).

For the two-sided limit of a function to exist at a point, both the limit from the left and the limit from the right must converge to the same number.

Continuity

If the function f(x)f(x) obeys, f(a)=lim⁡x→af(x)f(a) = \lim_{x\to a} f(x), we say the function f(x)f(x) is continuous at x=ax=a. Geometrically, the graph of the continuous function at x=ax=a is a smooth curve that doesn’t have a hole or a jump at x=ax=a.

Example 5

Is the function g(x)=2x+1xg(x)=\frac{2x+1}{x} continuous at x=1x=1? First, we evaluate the function g(x)g(x) for the input x=1x=1, which gives us g(1)=2(1)+11=31=3g(1) = \frac{2(1)+1}{1} = \frac{3}{1}=3. Next we compute the limit from the right at x=1x=1 which is:

lim⁡x→1+ ⁣g(x)= ⁣ ⁣lim⁡x→1+ ⁣ ⁣2x+1x=lim⁡δ→0 ⁣2(1+δ)+11+δ=lim⁡δ→0 ⁣2+2δ+11+δ=31=3.\lim_{x\to 1^+}\! g(x) = \!\!\lim_{x\to 1^+}\!\! \tfrac{2x+1}{x} = \lim_{\delta \to 0}\! \tfrac{2(1+\delta)+1}{1+\delta} = \lim_{\delta \to 0}\! \tfrac{2+2\delta+1}{1+\delta} = \tfrac{3}{1} = 3.

The δ\delta terms in the numerator and denominator go to zero. We evaluate the limit from the left using similar steps:

lim⁡x→1−g(x)=lim⁡x→1−2x+1x=lim⁡δ→02(1−δ)+11−δ=3.\lim_{x\to 1^-} g(x) = \lim_{x\to 1^-} \frac{2x+1}{x} = \lim_{\delta \to 0} \tfrac{2(1-\delta)+1}{1-\delta} = 3.

Since the limits from the left and the right are equal, lim⁡x→1+g(x)=lim⁡x→1−g(x)\lim_{x\to 1^+} g(x) = \lim_{x\to 1^-} g(x), we say that the two-sided limit exists lim⁡x→1g(x)=3\lim_{x\to 1} g(x) = 3. Since lim⁡x→1g(x)=g(3)\lim_{x\to 1} g(x) = g(3), the function g(x)g(x) is continuous at x=1x=1.

Computing limits using SymPy

Arithmetic operations with infinity

Infinity ∞\infty is denoted sp.oo (two lowercase os) in SymPy. Infinity is not a number but a process: the process of counting forever. When we use ∞\infty in math expressions and comparisons, we lead to some surprising facts, which we can illustrate using SymPy.

Adding a number to infinity still produces infinity, ∞+1=∞\infty + 1 = \infty.

Loading...

Infinity is greater than any finite number:

Loading...

A number divided by infinity is zero, 1/∞=01/\infty = 0:

Loading...

Example 1 using SymPy

We can use SymPy to calculate the limit as nn goes to infinity:

Loading...

We see the result of the limit is πr2\pi r^2, which is the formula for the area of a circle we were looking for.

Example 2 using SymPy

The number ee is defined as the limit lim⁡n→∞(1+1n)n\lim_{n\to\infty}\left(1+\frac{1}{n}\right)^n:

Loading...
Loading...

This limit expression describes the annual growth rate of a loan with a nominal interest rate of 100% and infinitely frequent compounding. Borrow $1000\$1000 in such a scheme, and you’ll owe $2718.28\$2718.28 after one year.

Example 3 using SymPy

Loading...

To evaluate the limit from the right or the left at a point, we must provide a fourth argument, either "+" or "-", when calling the function sp.limit.

Loading...
Loading...

Example 4 using SymPy

Let’s check the result lim⁡x→∞ ⁣ ⁣2x+1x=2\lim_{x\to \infty}\!\! \tfrac{2x+1}{x} = 2.

Loading...

Example 5 using SymPy

Is the function g(x)=2x+1xg(x) = \frac{2x+1}{x} continuous at x=1x=1?

Loading...
Loading...
Loading...

The fact that all three expressions exist and are equal to each other,

lim⁡x→1−g(x)=g(1)=lim⁡x→1+g(x),\lim_{x \to 1^-} g(x) = g(1) = \lim_{x \to 1^+} g(x),

tells us that the function g(x)=2x+1xg(x)=\frac{2x+1}{x} is continuous at x=1x=1. Intuitively, you can understand continuity by imagining a pen drawing the graph at x=1x=1. Th pen comes in from the left at height lim⁡x→1−g(x)= 3\lim_{x\to 1^-} g(x) =~3, goes through g(1)=3g(1) = 3, then leaves to the right at the same height lim⁡x→1+g(x)=3\lim_{x\to 1^+} g(x) = 3.

Applications of limits

Limits for derivatives

The formal definition of a function’s derivative is expressed in terms of the rise-over-run formula for an infinitely short run:

f′ ⁣(x)=lim⁡run→0riserun=lim⁡δ→0f(x+δ)  −  f(x)x+δ  −  x=lim⁡δ→0f(x+δ)  −  f(x)δ.f^{\prime\!}(x) = \lim_{\textrm{run} \to 0} \frac{\text{rise}}{\text{run}} = \lim_{\delta \to 0} \frac{f(x+\delta)\; - \; f(x)}{x+\delta \; - \; x} = \lim_{\delta \to 0} \frac{f(x+\delta)\; - \; f(x)}{\delta}.

More on derivatives in the next section.

Limit for integrals

One way to approximate the area under the graph of the function f(x)f(x) between x=ax=a and x=bx=b is to split the area into nn rectangular strips of width Δx=b−an\Delta x = \frac{b-a}{n} and height f(x)f(x), and then calculate the sum of the areas of these rectangles:

Af(a,b)≈Δxf(a+Δx) ⁣+ ⁣Δxf(a+2Δx) ⁣+ ⁣Δxf(b)⏟n terms.A_f(a,b) \approx \underbrace{ \Delta x f(a+\Delta x) \!+\! \Delta x f(a+2\Delta x) % \!+\! \Delta x f(a+2\Delta x) + \cdots \!+\! \Delta x f(b) }_{n \textrm{ terms}}.

We obtain the exact value of the area in the limit where we split the area into an infinite number of rectangles with infinitely narrow width:

Af(a,b)= ⁣ ⁣∫ab ⁣ ⁣ ⁣f(x) dx= ⁣ ⁣lim⁡n→∞Δx[f(a+Δx) ⁣+ ⁣f(a+2Δx) ⁣+ ⁣⋯ ⁣+ ⁣f(b)].A_f(a,b) = \!\!\int_a^b\!\!\!f(x) \, dx = \!\!\lim_{n \to \infty} \Delta x \left[ f(a+\Delta x) \!+\! f(a+2\Delta x) \!+\! \cdots \!+\! f(b) \right].

Limits for series

We use limits to describe the convergence properties of series. For example, the sum of the first nn terms of the geometric series rkr^k corresponds to the following expression:

∑k=0nrk=1+r+r2+r3+⋯+rn=1−rn+11−r.\sum_{k=0}^n r^k = 1 + r + r^2 + r^3 + \cdots + r^n = \frac{1-r^{n+1}}{1-r}.

The series ∑rk\sum r^k is defined as the limit n→∞n\to \infty of the above expression. When ∣r∣<1|r|<1, the geometric series converges to the a finite value:

lim⁡n→∞∑k=0nrk=1+r+r2+r3+⋯=11−r.\lim_{n \to \infty} \sum_{k=0}^n r^k = 1 + r + r^2 + r^3 + \cdots = \frac{1}{1-r}.

In each of these domains, limit expressions will help us make precise statements that describe calculus procedures with infinite small lengths and an infinite number of steps.

Derivatives

The derivative function, denoted f′(x)f'(x), ddx[f(x)]\frac{d}{dx}\big[f(x)\big], dfdx\frac{df}{dx}, or dydx\frac{dy}{dx}, describes the instantaneous rate of change of the function f(x)f(x). Geometrically, the derivative f′(x)f'(x) tells us the slope of the function ff at xx.

Let’s start by looking at the average slope calculation between two points separated by the distance Δx\Delta x, which is read “delta x.”

<Figure size 300x250 with 1 Axes>

The average slope is calculated using the formula slope=ΔyΔx\text{slope} = \frac{\Delta y}{\Delta x}, where Δx=\Delta x = step is the length of the horizontal step, and Δy\Delta y is the change in the function’s output, Δy=f(x+Δx)−f(x)\Delta y = f(x+\Delta x) - f(x).

Try changing the value of the variable step and rerun the above code. What happens when yo use a very small steps like 0.1, 0.01, and 0.001?

Derivative formula

The derivative function f′ ⁣(x)f^{\prime\!}(x) is defined as the following limit:

f′ ⁣(x)    =    lim⁡δ→0f(x+δ) − f(x)δ .f^{\prime\!}(x) \;\;= \;\; \lim_{\delta \to 0} \frac{f(x+\delta)\ - \ f(x)}{\delta}\,.

Here δ\delta plays the same role as Δx\Delta x, but we let δ\delta go to zero.

Numerical derivative calculations

The derivative is the slope calculation for a very very short step, which we’ll denote delta to match the math definition.

Loading...
<Figure size 800x300 with 1 Axes>
Loading...
<Figure size 500x170 with 1 Axes>

Derivative formulas

f(x) −derivative→ f′ ⁣(x)a−ddx→0x−ddx→1mx+b−ddx→mxn, for n≠0−ddx→nxn−11x=x−1−ddx→−1x2=−x−2x=x12−ddx→12x=12x−12ex−ddx→exln⁡(x)−ddx→1xsin⁡(x)−ddx→cos⁡(x)cos⁡(x)−ddx→−sin⁡(x)\begin{align*} f(x) & \ -\textrm{derivative}\to \ f^{\prime\!}(x) \\ a &\qquad - \tfrac{d}{dx} \rightarrow \qquad 0 \\ x &\qquad - \tfrac{d}{dx} \rightarrow \qquad 1 \\ mx+b &\qquad - \tfrac{d}{dx} \rightarrow \qquad m \\ x^n, \text{ for } n\neq 0 &\qquad - \tfrac{d}{dx} \rightarrow \qquad nx^{n-1} \\ \tfrac{1}{x} = x^{-1} &\qquad - \tfrac{d}{dx} \rightarrow \qquad \tfrac{-1}{x^2} = -x^{-2} \\ \sqrt{x} = x^{\frac{1}{2}} &\qquad - \tfrac{d}{dx} \rightarrow \qquad \tfrac{1}{2\sqrt{x}} = \tfrac{1}{2}x^{-\frac{1}{2}} \\ e^x &\qquad - \tfrac{d}{dx} \rightarrow \qquad e^x \\ \ln(x) &\qquad - \tfrac{d}{dx} \rightarrow \qquad \tfrac{1}{x} \\ \sin(x) &\qquad - \tfrac{d}{dx} \rightarrow \qquad \cos(x) \\ \cos(x) &\qquad - \tfrac{d}{dx} \rightarrow \qquad -\sin(x) \end{align*}

Derivative rules

In addition to the table of derivative formulas, there are some important derivatives rules that allow you to find derivatives of composite functions.

Constant multiple rule

The derivative of kk times the function f(x)f(x) is equal to kk times the derivative of f(x)f(x):

[kf(x)]′=kf′ ⁣(x).\big[ k f(x) \big]^\prime = k f^{\prime\!}(x).

Sum rule

The derivative of the sum of two functions is the sum of their derivatives:

[f(x)+g(x)]′=f′ ⁣(x)+g′ ⁣(x).\big[ f(x) + g(x) \big]^\prime = f^{\prime\!}(x) + g^{\prime\!}(x).

Product rule

The derivative of a product of two functions is the sum of two contributions:

[f(x)g(x)]′=f′ ⁣(x)g(x)+f(x)g′ ⁣(x).\big[ f(x)g(x) \big]^\prime = f^{\prime\!}(x)g(x) + f(x)g^{\prime\!}(x).

In each term, the derivative of one of the functions is multiplied by the value of the other function.

Quotient rule

This formula tells us how to obtain the derivative of a fraction of two functions:

[f(x)g(x)]′=f′ ⁣(x)g(x)−f(x)g′ ⁣(x)g(x)2.\left[ \frac{f(x)}{g(x)} \right]^\prime = \frac{f^{\prime\!}(x)g(x)-f(x)g^{\prime\!}(x)}{g(x)^2}.

Chain rule

If you encounter a situation that includes an inner function and an outer function, like f(g(x))f(g(x)), you can obtain the derivative by a two-step process:

[f(g(x))]′=f′ ⁣(g(x))g′ ⁣(x).\big[ f(g(x)) \big]^\prime = f^{\prime\!}(g(x))g^{\prime\!}(x).

In the first step, we leave the inner function g(x)g(x) alone and focus on taking the derivative of the outer function f(x)f(x). This step gives us f′ ⁣(g(x))f^{\prime\!}(g(x)), which is the value of f′f^{\prime} evaluated at g(x)g(x). In the second step, we multiply this expression by the derivative of the inner function g′ ⁣(x)g^{\prime\!}(x).

Higher derivatives

The second derivative of f(x)f(x) is denoted f′′ ⁣(x)f^{\prime\prime\!}(x) or d2fdx2\frac{d^2f}{dx^2}. It is obtained by applying the derivative operation to f(x)f(x) twice: ddx[ddx[<f>]]\frac{d}{dx}\big[ \frac{d}{dx}[\texttt{<f>}]\big].

Geometrically, the second derivative f′′ ⁣(x)f^{\prime\prime\!}(x) tells us the curvature of f(x)f(x). Positive curvature means the function opens upward and looks like the bottom of a valley. The function f(x)=x2f(x)=x^2 has derivative f′ ⁣(x)=2xf^{\prime\!}(x) = 2x and second derivative f′′ ⁣(x)=2f^{\prime\prime\!}(x) = 2, which means it has positive curvature everywhere. Negative curvature means the function opens downward and looks like a mountain peak. For example, the function g(x)=−x2g(x) = -x^2 has negative curvature, since its second derivative is g′′ ⁣(x)=−2<0g^{\prime\prime\!}(x) = -2 < 0.

Examples

We’ll use the SymPy function sp.diff to computes the derivative in the following examples.

Example 6

Calculate the derivative of f(x)=ex2f(x) = e^{x^2}.

Loading...

Example 7

To find the derivative of f(x)=sin⁡(x)ex2f(x) = \sin(x)e^{x^2}.

Loading...

Example 8

Calculate the derivative of sin⁡(x2)\sin(x^2).

Loading...

Checking the derivative formulas using SymPy

The table of derivative formulas tells us that [mx+b]′=m\big[mx+b\big]^\prime = m. Let’s verify that SymPy gives the same answer.

Loading...

We can also verify the derivative formula for the polynomial term [xn]′=nxn−1\big[x^n\big]^\prime = nx^{n-1}.

Loading...

The exponential function f(x)=exf(x)=e^x is an interesting special case, because it is equal to its derivative:

Loading...

Here is a mix of three other functions from the table of derivative formulas.

Loading...

Applications of derivatives

Tangent lines

The tangent line to the function f(x)f(x) at x=x0x=x_0 is the line that passes through the point (x0,f(x0))(x_0, f(x_0)) and has the same slope as the function at that point. The tangent line to the function f(x)f(x) at the point x=x0x=x_0 is described by the following formula:

T1(x)=f(x0) + f′(x0)(x−x0).T_1(x) = f(x_0) \ + \ f'(x_0)(x-x_0).

Let’s find the equation of the tangent line to f(x)=x2f(x)=x^2 at x0=1x_0=1?

Loading...
Loading...
Loading...
<Figure size 500x170 with 1 Axes>

We can see that the tangent line T1(x)=2x−1T_1(x) = 2x -1 pass through the point (1,1)(1,1) and has same slope as f(x)f(x) at x=1x=1.

Solving optimization problems using derivatives

Optimization is all finding the input for a function f(x)f(x) that results in the best value for f(x)f(x). The best value could be a maximum value (when the function represents something desirable like profits) or a minimum value (when the function represents something undesirable like costs).

The derivative f′(x)f'(x) encodes the information about the slope of f(x)f(x). Positive slope f′(x)>0f'(x)>0 means f(x)f(x) is increasing, negative slope f′(x)<0f'(x)<0 means f(x)f(x) is decreasing, and zero slope f′(x)=0f'(x)=0 means the graph of the function is horizontal. The critical points of a function f(x)f(x) are the solutions to the equation f′(x)=0f'(x)=0. Each critical point is a candidate to be either a maximum or a minimum of the function.

Analytical optimization using SymPy

Example 9

We define the SymPy expression qx that corresponds function q(x)=(x−5)2q(x) = (x-5)^2 and look at its graph.

<Figure size 500x170 with 1 Axes>

Next, we find the critical point(s) by solving the equation q′(x)=0q'(x) = 0.

Loading...

We see the function has only a single critical point x1∗=5x^*_1 = 5. Let’s check the sign of the second derivative at x1∗=5x^*_1 = 5.

Loading...

The second derivative is positive (like the bottom of a valley), so this means x1∗=5x^*_1 = 5 is a minimum.

Example 10

Let’s find the critical points of the function r(x)=x3−2x2+xr(x)=x^3-2x^2+x and use the information from its second derivative to find where the function reaches its maximum value between x=0x=0 and x=1x=1.

<Figure size 500x170 with 1 Axes>

Next, we compute the derivative r′(x)r'(x) and call the SymPy method .factor() to simulate what we would have to do on paper to find the critical values.

Loading...

We see the critical points are x1∗=13x^*_1 = \frac{1}{3} and x2∗=1x^*_2 = 1. We can confirm this more directly by asking SymPy to solve the equation r′(x)=0r'(x)=0.

Loading...

We want to now check if the critical values x1∗=13x^*_1 = \frac{1}{3} and x2∗=1x^*_2 = 1 are minimum or maximum by computing the second derivative of r(x)r(x).

Loading...

We now evaluate r′′(x)r''(x) at x1∗=13x^*_1 = \frac{1}{3}.

Loading...

The critical point x1∗=13x^*_1=\frac{1}{3} is a local maximum because it is a critical point of r(x)r(x) where the curvature is negative, meaning r(x)r(x) looks like the peak of a mountain at x1∗=13x^*_1=\frac{1}{3}.

We also evaluate r′′(x)r''(x) at x2∗=1x^*_2 = 1.

Loading...

The critical point x2∗=1x^*_2=1 is a local minimum because the function has positive curvature at x2∗=1x^*_2=1. This means r(x)r(x) looks like the bottom of a valley at x2∗=1x^*_2=1. We can confirm this by looking at the graph of the function.

Numerical optimization

If we repeatedly take steps in the “downhill” direction, we’ll end up at the bottom of a valley. This is the idea behind the gradient descent algorithm.

Example 9 revisited

Loading...

Example 10 revisited

Loading...

Numerical optimization using SciPy

Let’s now solve the optimization problems from examples 9 and 10 using the function minimize from scipy.optimize.

Loading...
Loading...

Integrals

Act 1: Integrals as area calculations

The integral of f(x)f(x) corresponds to the computation of the area under the graph of f(x)f(x). The area under f(x)f(x) between the points x=ax=a and x=bx=b is computed using the following integral:

Af(a,b)=∫abf(x) dx.A_f(a,b) = \int_a^b f(x) \: dx.

The numbers aa and bb are called the limits of integration.

The integral of the function f(x)f(x) between x=ax=a and x=bx=b corresponds to the area of the shaded region in the above figure.

Properties of integrals

The following properties of integrals follow from their interpretation as area calculations.

  • The sum of the integral from aa to bb and the integral from bb to cc is equal to the integral starting from aa going all the way to cc: ∫abf(x) dx+∫bcf(x) dx=∫acf(x) dx\int_a^b f(x) \, dx + \int_b^c f(x) \, dx = \int_a^c f(x) \, dx.

  • The integral of kk times the function f(x)f(x) is equal to kk times the integral of f(x)f(x): ∫abkf(x) dx=k∫abf(x) dx\int_a^b kf(x)\, dx = k\int_a^b f(x)\, dx.

  • The integral of the sum of two functions is the sum of their integrals: ∫ab[f(x)+g(x)] dx=∫abf(x) dx+∫abg(x) dx\int_a^b [f(x) + g(x)]\, dx = \int_a^b f(x)\, dx + \int_a^b g(x)\, dx.

  • An integral over a region with zero width has zero value: ∫aaf(x) dx=0\int_a^a f(x)\, dx = 0. Geometrically, this integral defines a region with height f(x)f(x) and width 0, so it has zero area.

Example 11: integral of a constant function

The figure below illustrates the integral under f(x)=3f(x) = 3 between a=0a=0 and b=5b=5, which we denote ∫05f(x) dx\int_0^5 f(x)\,dx.

<Figure size 500x170 with 1 Axes>

The area under f(x)f(x) between x=0x=0 and x=5x=5 is described by the following integral calculation:

Af(0,5)=∫05f(x) dx=3⋅5=15.A_f(0,5) = \int_0^5 f(x)\,dx = 3\cdot 5 = 15.

The area under the graph of f(x)f(x) is a rectangle with height 3 and width 5, so its area is 3⋅5=153 \cdot 5 = 15.

Example 12: integral of a linear function

Let’s now visualize the integral under g(x)=xg(x) = x between a=0a=0 and b=5b=5, which we denote ∫05g(x) dx\int_0^5 g(x)\,dx.

<Figure size 500x170 with 1 Axes>

This area is described by the following integral calculation:

Ag(0,5)=∫05g(x) dx=125⋅5=1252=252=12.5.A_g(0,5) = \int_0^5 g(x) \, dx = \tfrac{1}{2} 5 \cdot 5 = \tfrac{1}{2}5^2 = \tfrac{25}{2} = 12.5.

The region under the graph of g(x)g(x) has a triangular shape, so we can compute its area using the formula for the area of a triangle: base times height divided by 2.

Example 13: integral of a polynomial

Consider now the function h(x)=4−x2h(x) = 4 - x^2. We want to know the area under the graph of h(x)h(x) between x=0x=0 and x=2x=2, which is described by the following integral:

Ah(0,2)=∫02h(x) dx =  ???.A_h(0,2) = \int_{0}^2 h(x)\,dx \, = \; ???.
<Figure size 500x170 with 1 Axes>

The region under the graph of h(x)h(x) is curved and not a simple recognizable geometric shape with a known area formula. How could we compute the integral ∫02h(x) dx\int_{0}^2 h(x)\,dx in this case?

Computing integrals numerically

Computing the integral of f(x)f(x) numerically means we’re splitting the region of integration into many (think thousands or millions) of rectangular strips, computing the areas of these strips, then adding up the areas to obtain the total area under the graph of f(x)f(x). The approximation to the area under f(x)f(x) between x=ax=a and x=bx=b using nn rectangular strips corresponds to the following formula:

Af(a,b)≈∑k=1nf(a+kΔx) Δx,A_f(a,b) \approx \sum_{k=1}^{n} f(a + k\Delta x)\,\Delta x,

where Δx=b−an\Delta x = \frac{b-a}{n} is the width of each rectangular strip. The right endpoint of the kkth is located at xk=a+kΔxx_k = a + k\Delta x, so the height of the rectangular strips f(xk)f(x_k) varies as kk goes from k=1k=1 (first strip) to k=nk=n (last strip).

This is known as the Riemann sum formula for approximating areas. We’ll can turn the Riemann sum formula into a Python procedure that performs the nn-rectangle area calculation:

Example 13 continued

Let’s use the integrate procedure to compute the integral of the function h(x)=4−x2h(x) = 4- x^2.

Let’s start with n=10n=10, which is a very rough approximation.

Loading...
Riemann sum with n=10 rectangles: approx. area ≈ 4.92000
<Figure size 500x170 with 1 Axes>

We can compute the approximation with n=20n=20 rectangles just as easily:

Loading...
Riemann sum with n=20 rectangles: approx. area ≈ 5.13000
<Figure size 500x170 with 1 Axes>

The approximations get better and better as we increase the number of rectangles nn.

Loading...
Loading...
Loading...
Loading...
Loading...

Example 11N

We can also use integrate to compute the integral of the constant function f(x)=3f(x)=3 that we computed geometrically earlier.

Loading...

The numerical approximations we obtain is equal to to the exact answers ∫05f(x) dx=15\int_0^5 f(x)\,dx = 15 we obtained earlier using geometry.

Example 12N

Let’s now use integrate to compute the integral of the linear function g(x)=xg(x)=x.

Loading...

The numerical approximation is close to the exact answers ∫05g(x) dx=12.5\int_0^5 g(x) \, dx = 12.5.

Formal definition of the integral

In the limit as the number of rectangles nn approaches ∞\infty, the approximation to the area under the graph of f(x)f(x) becomes arbitrarily close to the true area.

The integral between x=ax=a and x=bx=b is defined as the limit as nn goes to infinity of the Riemann sum:

∫ab ⁣f(x) dx=lim⁡n→∞∑k=1nf(a+kΔx)Δx,where Δx=b−an.\int_{a}^{b}\!f(x)\,dx = \lim_{n\to\infty} \sum_{k=1}^{n} f(a + k\Delta x)\Delta x, \quad \text{where } \Delta x=\tfrac{b-a}{n}.

In words, the integral is a Riemann sum that consists of infinitely thin rectangular strips.

Act 2: Integrals as functions

The integral function F0F_0 corresponds to the area calculation as a function of the upper limit of integration:

F0(b)=∫0b ⁣f(x) dx .F_0(b) = \int_0^b \! f(x)\,dx\,.

The area under f(x)f(x) between x=ax=a and x=bx=b can be obtained by calculating the change in the integral function:

Af(a,b)=∫ab ⁣f(x) dx=F0(b)−F0(a).A_f(a,b) = \int_a^b \! f(x)\,dx = F_0(b)-F_0(a).

In SymPy we use sp.integrate(f,x) to obtain the integral function F0(x)F_0(x) of any function f(x)f(x): F0(x)=∫0xf(u) duF_0(x) = \int_0^x f(u)\,du.

<Figure size 800x200 with 5 Axes>

Example 11 revisited

Here is an illustration of the integral function F0F_0.

<Figure size 500x170 with 1 Axes>

The integral function F0F_0 corresponds to the area of a rectangle of height 3 and width bb:

F0(b)=Af(0,b)=∫0b ⁣f(x) dx=3b.F_0(b) = A_f(0,b) = \int_0^b \! f(x)\,dx = 3 b.

Example 12 revisited

Here is an illustration of the integral function G0G_0.

<Figure size 500x170 with 1 Axes>

Since the region is triangular, we can compute its area using the formula for the area of a triangle:

G0(b)=Ag(0,b)=∫0bg(x) dx=12b2.G_0(b) = A_g(0,b) = \int_0^b g(x) \, dx = \tfrac{1}{2}b^2.

Example 13 revisited

Here is an illustration of the integral function H0H_0.

<Figure size 500x170 with 1 Axes>

The area under h(x)=4−x2h(x) = 4 - x^2 from x=0x=0 until x=bx=b is described by the following integral calculation:

H0(b)=Ah(0,b)=∫0bh(x) dx=  ???.H_{0}(b) = A_h(0,b) = \int_{0}^b h(x) \, dx = \; ???.

We were able to compute the integral functions F0(b)F_0(b) and G0(b)G_0(b) thanks to the simple geometries of the areas under the graphs, but h(x)h(x) is a curve so it’s not clear how to proceed. We require some new integration methods.

Act 3: Fundamental theorem of calculus

The integral is the inverse operation of the derivative. If you perform the integral operation followed by the derivative operation on some function, you’ll get back the original function:

(ddx∘∫dx)f(x)=ddx∫cxf(u) du=f(x).\left(\frac{d}{dx} \circ \int dx \right) f(x) = \frac{d}{dx} \int_c^x f(u)\,du = f(x).

Let’s use SymPy to verify that the fundamental theorem of calculus is true by applying the sequence of operations on the function fx=f(x)=x2\texttt{fx} = f(x) = x^2.

Loading...
Loading...

The sequence of operations sp.diff(sp.integrate(f(x)))=ddx ⁣∫0x ⁣f(u)du\texttt{sp.diff(sp.integrate(}f(x)\texttt{))} = \frac{d}{dx}\!\int_0^x \!f(u)du brings us back to the original fx that we started with.

Alternately, if you compute the derivative of a function G(x)G(x) followed by the integral, you will obtain the original function (up to a constant):

(∫dx∘ddx)G(x)=∫cxG′(u) du=G(x)+C.\left( \int dx \circ \frac{d}{dx}\right) G(x) = \int_c^x G'(u)\,du = G(x) + C.

Let’s verify this formula by applying it to the function Gx=G(x)=x3\texttt{Gx} = G(x) = x^3.

Loading...
Loading...

Applying the operations integrate(diff(G(x))) ⁣=∫0x ⁣G′ ⁣(u) du\texttt{integrate(diff(}G(x)\texttt{))}\!=\int_0^x\!G^{\prime\!}(u)\,du brings us back to the original Gx that we started with.

Using antiderivatives to compute integrals

The fundamental theorem of calculus gives us a symbolic trick for computing integral function F0(b)=∫0bf(x) dxF_0(b) = \int_0^b f(x)\,dx.

Given some function f(x)f(x), any function F(x)F(x) that satisfies the equation F′ ⁣(x)=f(x)F^{\prime\!}(x) = f(x) is called an antiderivative of f(x)f(x). If you can find antiderivative function F(x)F(x), then the integral of f(x)f(x) is F(x)F(x) plus some constant:

F0(x)=∫0xf(u) du=F(x)+C.F_0(x) = \int_0^x f(u)\,du = F(x) + C.

To find an antiderivative of f(x)f(x), we look for a function F(x)F(x) whose derivative is f(x)f(x). We can use the table of derivative formulas in the reverse direction. For example, to find the antiderivative of the function f(x)=mf(x) = m, we look for a row where this function appears on the right side of the table, then look at the corresponding function on the left side of the table, which is the function F(x)=mx+bF(x) = mx + b in this case. We can verify that F′ ⁣(x)=f(x)F^{\prime\!}(x) = f(x), so indeed F(x)=mx+bF(x) = mx + b is an antiderivative of f(x)=mf(x) = m. Furthermore, the equivalence between antiderivatives and integral functions tells us that the integral function of f(x)f(x) is Fa(x)=∫axm du=mx+bF_a(x) = \int_a^x m\,du = mx + b, for some constant bb.

Using derivative formulas in reverse

Suppose we want to find the integral of the function f(x)=m+ex+1x+cos⁡(x)−sin⁡(x)f(x) = m + e^x + \frac{1}{x} + \cos(x) - \sin(x), which contains a mix of functions from the right side of  table of derivative formulas.

Loading...

SymPy tells us the integral function F0F_0 is F0(x)=mx+ex+log⁡(x)+sin⁡(x)+cos⁡(x)F_0(x) = mx + e^x + \log(x) + \sin(x) + \cos(x), which are all the corresponding terms from the left side of the table of derivative formulas.

Act 4: Techniques of integration

Substitution trick

See the tutorial PDF.

Example 14

Integration by parts

See the tutorial PDF.

Example 15

Computing integrals numerically using SciPy

We’ll now show some examples using the function quad form the module scipy.integrate. The function name quad is short for quadrature, which is an old name for computing areas.

Example 11N using SciPy

Let’s define the function f(x)=3f(x) = 3 and compute the integral ∫05f(x) dx\int_0^5 f(x)\,dx.

Loading...

The function quad returns two numbers as output: the value of the integral and a precision parameter. The output of the above code tells us the value of the integral is ∫05f(x) dx\int_{0}^5 f(x)\,dx is 15.0 and the accuracy of this value up to an error of 1.7×10−131.7\times 10^{-13}. Since we’re usually only interested in the value of the integral, we often select the first output of quad so you’ll see the code like quad(...)[0] in all the code examples below.

Loading...

Example 12N using SciPy

Define the function g(x)=xg(x) = x and compute the integral ∫05g(x) dx\int_0^5 g(x)\,dx.

Loading...

Example 13N using SciPy

Lets now compute ∫02h(x) dx\int_{0}^2 h(x)\,dx for the function h(x)=4−x2h(x) = 4 - x^2.

Loading...

Computing integral functions symbolically using SymPy

We can use Python to do symbolic integration using variables (symbols) instead of numbers. The SymPy function sp.integrate allows us to obtain the formulas for integrals and integral functions. We’ll now revisit the integral calculations from the three examples using symbolic math. We start by defining three symbols x, a, and b.

We’ll use these symbols to express the functions and the limits of integration.

Example 11S: Constant function

Consider the constant function f(x)=3f(x) = 3, which we can define as follows:

Loading...

To compute the integral ∫abf(x)dx\int_a^b f(x) dx, we call the SymPy function sp.integrate, passing in the function fx as the first argument, and the triple (x,a,b)\texttt{(x,a,b)} as the second argument, which specifies the variable of integration and the limits of integration aa and bb.

Loading...

The answer 3⋅(b−a)3\cdot (b-a) is the general expression for calculating the area under f(x)=3f(x)=3, for between any starting point x=ax=a and end point x=bx=b. Geometrically, this is just a height-times-width formula for the area of a rectangle.

To compute the specific integral between a=0a=0 and b=5b=5, we use the .subs (substitute) method, passing in a Python dictionary of the values we want to “plug” into the general expression.

Loading...

The integral function F0(b)=∫0bf(x)dxF_0(b) = \int_0^b f(x) dx is obtained by specifying the variable b as the upper limit of integraiotn.

Loading...

We can obtain the area Af(0,5)A_f(0,5) by computing the change in integral function Af(0,5)=F0(5)−F0(0)A_f(0,5) = F_0(5) - F_0(0).

Loading...

Example 12S: Linear function

Let’s now compute the integral function of the linear function g(x)=xg(x) = x, which corresponds to the following SymPy expression:

Loading...

The integral function G0(b)=∫0bg(x)dxG_0(b) = \int_0^b g(x) dx is obtained using a variable as the upper limit of integration.

Loading...

Note this is the same formulas as the formula we obtained using geometry. We can now use G0b to calculate the area Ag(0,5)A_g(0,5) thanks to the formula Ag(0,5)=G0(5)−G0(0)A_g(0,5) = G_0(5) - G_0(0).

Loading...

Example 13S: Polynomial function

We’ll now use SymPy to integrate the function h(x)=4−x2h(x) = 4 - x^2.

Loading...
Loading...

We can obtain the area Ah(0,2)=∫02h(x) dxA_h(0,2)=\int_0^2 h(x)\,dx by using Ah(0,2)=H0(2)−H0(0)A_h(0,2) = H_0(2) - H_0(0).

Loading...

Note SymPy calculated the exact value 163=5.3‾\frac{16}{3} = 5.\overline{3} and not the floating point approximation 5.3333…5.3333\ldots.

Applications of integration

Integrals are essential tools used in many areas of science, engineering, and business.

Kinematics

Calculus was originally invented to describe the equations of motion x(t)x(t), v(t)v(t), and a(t)a(t), which correspond to an object’s position, velocity, and acceleration at time tt. We call these the kinematics equations, from the Greek word kinema for motion. The velocity function v(t)v(t) is the derivative of the position function, and the acceleration a(t)a(t) is the derivative of the velocity. We can summarize these relations as follows:

a(t)⟵ddtv(t)⟵ddtx(t).a(t) \overset{\frac{d}{dt} }{\longleftarrow} v(t) \overset{\frac{d}{dt} }{\longleftarrow} x(t).

Let’s analyze the case where the net force on the object is constant. A constant force causes a constant acceleration a=Fm=constanta = \frac{F}{m} = \textrm{constant}. We find v(t)v(t) by integrating a(t)=aa(t)=a.

Loading...

We can now find x(t)x(t) by integrating v(t)v(t).

Loading...

These two calculus steps allow us to obtain the famous kinematics equation for uniform accelerated motion (UAM):

a(t)=a,v(t)=vi+at,x(t)=xi+vit+12at2.\begin{align*} a(t) &= a, \\ v(t) &= v_i + at, \\ x(t) &= x_i + v_it + \tfrac{1}{2}at^2. \end{align*}

You probably had to memorize these equations in your physics class, bu now Now you know how to derive them from first principles.

Solving differential equations

A differential equation is an equation that involves the derivatives of some unknown function f(x)f(x). Integration is very useful for solving differential equations. For example, to solve for f(x)f(x) in the differential equation ddxf(x)=g(x)\frac{d}{dx}f(x) = g(x), we can take the integral on both sides of the equation to obtain the answer f(x)=∫g(x) dx+Cf(x) = \int g(x)\,dx + C.

The SymPy function sp.dsolve(diff_expr, <f>) allows us to solve the differential equation diff_expr = 0 for the unknown function <f>. We’ll now solve some differential equations using sp.dsolve.

Kinematics revisited

The kinematics equations when the acceleration is constant come from the differential equation x′′ ⁣(t)=ax^{\prime\prime\!}(t) = a, which we can also write as x′′ ⁣(t)−a=0x^{\prime\prime\!}(t) - a = 0.

Loading...

The solution is the same as the equation x(t)=xi+vit+12at2x(t) = x_i + v_it + \tfrac{1}{2}at^2 that we obtained earlier if we identify the integration constants C1C_1 as the initial position xix_i, and C2C_2 as the initial velocity viv_i.

Bacterial growth

In biology, unconstrained bacterial growth is described by the equation b′ ⁣(t)=kb(t)b^{\prime\!}(t) = kb(t), where b(t)b(t) is the number of bacteria at time tt.

Loading...
Radioactive decay

Radioactive decay is described by the differential equation r′ ⁣(t)=−λr(t)r^{\prime\!}(t) = -\lambda r(t), where r(t)r(t) describes the number of atoms of some radioactive element.

Loading...
Simple harmonic motion (SHM)

Simple harmonic motion is described by the differential equation x′′ ⁣(t)+ω2x(t)=0x^{\prime\prime\!}(t) + \omega^2 x(t) = 0.

Loading...

Probability calculations

Integration is required for computing probabilities of continuous random variables. A continuous random variable XX is described by its probability density function fXf_X. For example, the standard normal random variable ZZ is described by the probability density function fZ(z)=12πe−12z2f_Z(z) = \tfrac{1}{\sqrt{2\pi}} e^{ -\frac{1}{2}z^2 }.

<Figure size 500x170 with 1 Axes>

To compute the probability of the event {a≤X≤b}\{ a \leq X \leq b\}, we must find calculate the total probability density fXf_X that falls between x=ax=a and x=bx=b, which is given by the integral: Pr({a≤X≤b})=∫abfX(x) dx\textrm{Pr}( \{ a \leq X \leq b\} ) = \int_a^b f_X(x) \, dx. For example, the probability of the event {−1≤Z≤1}\{ -1 \leq Z \leq 1\} is given by ∫−11fZ(z) dz\int_{-1}^1 f_Z(z) \, dz.

Loading...

To probability of the event {−2≤Z≤2}\{ -2 \leq Z \leq 2\} is calculated as the integral ∫−22fZ(z) dz\int_{-2}^2 f_Z(z) \, dz.

Loading...

Sequences and series

Sequences are functions with discrete inputs

Instead of continuous inputs x∈Rx\in \mathbb{R}, sequences take natural numbers k∈Nk\in\mathbb{N} as inputs. We denote sequences as aka_k instead of the usual function notation a(k)a(k).

We can define a sequence by specifying an expression for its kthk^\mathrm{th} term.

The natural numbers

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
<Figure size 500x170 with 1 Axes>

We use a stem plot to represent the sequence nkn_k graphically, which shows it is only defined when kk is a natural number.

Squares of natural numbers

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
<Figure size 500x170 with 1 Axes>

Harmonic sequence

[1.0, 0.5, 0.3333, 0.25, 0.2, 0.1667, 0.1429, 0.125, 0.1111]
<Figure size 500x170 with 1 Axes>

The alternating harmonic sequence

[1.0, -0.5, 0.3333, -0.25, 0.2, -0.1667, 0.1429, -0.125, 0.1111]
<Figure size 500x170 with 1 Axes>

Inverse factorial sequence

[1.0, 1.0, 0.5, 0.166667, 0.041667, 0.008333, 0.001389, 0.000198, 2.5e-05, 3e-06]
<Figure size 500x170 with 1 Axes>

Geometric sequence

[1.0, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.01562, 0.00781, 0.00391, 0.00195, 0.00098]
<Figure size 500x170 with 1 Axes>

Powers of two

[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
<Figure size 500x170 with 1 Axes>

Convergence of sequences

What happens to a sequence as kk goes to infinity? We use the limit notation lim⁡k→∞\lim_{k \to \infty} to describe this process. There are two behaviours we’re interested in: sequences that blow up to infinity, and sequences that converge to some finite value.

Divergent sequences

The sequences nk=kn_k = k, qk=k2q_k = k^2, and bk=2kb_k = 2^k keep getting larger and larger as kk goes to infinity.

Loading...
Loading...
Loading...

Convergent sequences

The sequences hk=1kh_k = \tfrac{1}{k}, ak=(−1)k+1ka_k = \tfrac{(-1)^{k+1}}{k}, and fk=1k!f_k = \tfrac{1}{k!} converge to 0 in the limit as kk goes to infinity.

Loading...
Loading...
Loading...

Summation notation

The capital Greek letter sigma stands in for the word sum, and the range of index values included in this sum is denoted below and above the summation sign. For example, the sum of the values in the sequence ckc_k from k=0k=0 until k=nk=n is denoted

∑k=0nck=c0+c1+c2+⋯+cn−1+cn.\sum_{k=0}^n c_k = c_0 + c_1 + c_2 + \cdots + c_{n-1} + c_n.

Exact formulas for finite sums

We’ll now show some useful formulas for calculating the sums of certain sequences.

Geometric sums

The formula for the sum of the first nn terms in the geometric sequence is

Gn=∑k=0nrk=1+r+r2+⋯+rn=1−rn+11−r.G_n = \sum_{k=0}^n r^k = 1 + r + r^2 + \cdots + r^n = \frac{1-r^{n+1}}{1-r}.

Let’s verify this using SymPy. To work with series, use the sp.summation function whose syntax is analogous to the sp.integrate function.

Loading...

We can use this formula to find the sum of the powers of 2:

∑k=0n2k=1+2+4+8+⋯+2n=1−2n+11−2=2n+1−1.\sum_{k=0}^n 2^k = 1 + 2 + 4 + 8 + \cdots + 2^n = \frac{1-2^{n+1}}{1-2} = 2^{n+1} -1.
Loading...

Arithmetic sums

The sum of the first nn positive integers and the sum of their squares are described by the following formulas:

∑k=1nk=n(n+1)2and∑k=1nk2=n(n+1)(2n+1)6.\sum_{k=1}^n k = \frac{n(n+1)}{2} \qquad \text{and} \qquad \sum_{k=1}^n k^2=\frac{n(n+1)(2n+1)}{6}.
Loading...
Loading...

Sums of binomial coefficients

The binomial coefficient is denoted (nk){n \choose k}, which is read as “nn choose kk.” The binomial coefficient counts the number of combinations of kk items we can choose from a set of nn items, and it is computed using the formula:

(nk)=n!(n−k)! k!.{n \choose k} = \frac{n!}{(n-k)! \, k!}.

For example, the number of combinations of size 2 selected from a list of 5 items is

(52)=5!(5−2)! 2!=5!3! 2!=5⋅4⋅3⋅2⋅13⋅2⋅1    ⋅    2⋅1=1206⋅2=10.{5 \choose 2} = \frac{5!}{(5-2)!\,2!} = \frac{5!}{3!\,2!} = \frac{5\cdot 4 \cdot 3 \cdot 2 \cdot 1}{3 \cdot 2 \cdot 1 \;\; \cdot \;\; 2 \cdot 1 } = \frac{120}{6 \cdot 2} = 10.

The binomial coefficient appears in the expansion of the binomial expression (a+b)n(a + b)^n, which can written as the following sum:

(a+b)n=(n0)an+(n1)an−1b+⋯+(nn−1)abn−1+(nn)bn=∑k=0n(nk)an−kbk.\begin{aligned} (a+b)^n &= {n \choose 0}a^n + {n \choose 1}a^{n-1}b % + {n \choose 2}a^{n-2}b^2 + \cdots + {n \choose n-1 }ab^{n-1} + {n \choose n}b^n \\ &= \sum_{k=0}^n {n \choose k} a^{n-k} b^k. \end{aligned}

This sum appears in certain calculations in probability theory.

Series

Series are defined as the sums computed from the terms in the sequence ckc_k. The finite series ∑k=1nck\sum_{k=1}^n c_k computes the first nn terms of the sequence:

Cn=∑k=1nck=c1+c2+c3+c4+c5+⋯+cn−1+cn.C_n = \sum_{k=1}^n c_k = c_1 + c_2 + c_3 + c_4 + c_5 + \cdots + c_{n-1} + c_n.

The infinite series ∑ck\sum c_k computes all the terms in the sequence:

C∞=lim⁡n→∞Cn=lim⁡n→∞∑k=1nck=c1+c2+c3+c4+c5+⋯ .C_\infty = \lim_{n\to \infty} C_n = \lim_{n\to \infty} \sum_{k=1}^n c_k = c_1 + c_2 + c_3 + c_4 + c_5 + \cdots.

The infinite series ∑ck\sum c_k of the sequence ck:N→Rc_k : \mathbb{N} \to \mathbb{R} is analogous to the integral ∫0∞f(x) dx\int_0^\infty f(x) \,dx of a function f:R→Rf : \mathbb{R} \to \mathbb{R}.

Some infinite series converge to a finite value. For example, when ∣r∣<1|r|<1, the limit as n→∞n \to \infty of the geometric series converges to the following value:

G∞=lim⁡n→∞Gn=∑k=0∞rk=1+r+r2+r3+⋯=11−r.G_\infty = \lim_{n \to \infty} G_n = \sum_{k=0}^\infty r^k = 1 + r + r^2 + r^3 + \cdots = \frac{1}{1-r}.

This expression describes an infinite sum, which is not possible to compute in practice, but we can see the truth of this equation using our mind’s eye. The formula for first nn terms is the geometric series is Gn=1−rn+11−rG_n = \frac{1-r^{n+1}}{1-r}. The term rn+1r^{n+1} goes to zero as n→∞n \to \infty, so the only part of the formula that remains is 11−r\frac{1}{1-r}.

Loading...

Example 16: sum of a geometric series

We can represent the series as the area of the rectangles of width one constructed from the values of the sequences.

The sum of the first 11 terms of the sequence is 1.999023
<Figure size 500x170 with 1 Axes>

Let’s evaluate the partial sums of the sequence to see how quickly the series converges.

Loading...
Loading...
Loading...

We can compute the infinite sum using SymPy.

Loading...

Convergent and divergent series

We say the geometric series G∞=∑k=0∞rkG_\infty = \sum_{k=0}^\infty r^k converges to the value 11−r\frac{1}{1-r}. We can also say that the infinite geometric series ∑k=1∞gk\sum_{k=1}^\infty g_k is convergent, meaning it has a finite value and doesn’t blow up. Another example of a converging infinite series is F∞=∑k=0∞fkF_\infty = \sum_{k=0}^\infty f_k, which converges to the number ee, as we’ll see in Example 17 below.

In contrast, the harmonic series H∞=∑k=1∞hkH_\infty = \sum_{k=1}^\infty h_k diverges. When we sum together more and more terms of the sequences hkh_k, the total computed keeps growing and the infinite series blows up to infinity H∞=∑k=1∞hk=∞H_\infty = \sum_{k=1}^\infty h_k = \infty. We say that the harmonic series is divergent.

Harmonic series up to n= 20 is 3.5477396571436817
Harmonic series up to n= 100 is 5.17737751763962
Harmonic series up to n= 1000 is 7.484470860550345
Harmonic series up to n= 10000 is 9.787506036044382
Harmonic series up to n= 1000000 is 14.392725722865723
Loading...

Using convergent series for practical calculations

Example 17: Euler’s number

The infinite sum of the sequence fk=1k!f_k = \tfrac{1}{k!} converges to Euler’s number e=2.71828182845905…e = 2.71828182845905\ldots:

F∞=lim⁡n→∞Fn=∑k=0∞1k!=1+1+12+16+124+1720+⋯=e.F_\infty = \lim_{n \to \infty} F_n = \sum_{k=0}^\infty \tfrac{1}{k!} = 1 + 1 + \tfrac{1}{2} + \tfrac{1}{6} + \tfrac{1}{24} + \tfrac{1}{720} + \cdots = e. % = 2.71828\ldots.

The calculation above is not just cool math fact, but a useful computational procedure that we can use to approximate the value of e=2.71828…e = 2.71828\ldots using only basic arithmetic operations like repeated multiplication (factorial), division, and addition.

Let’s start with a visualization of the series.

The sum of the first 11 terms of the sequence is 2.718282
<Figure size 500x170 with 1 Axes>

Using a Python for-loop, we can obtain an approximation to ee that is accurate to six decimals by summing 10 terms in the series:

Loading...

The approximation becomes more accurate as we sum together additional terms.

Loading...
Loading...

The approximation we obtain using 20 terms in a series is a very close approximation to the exact value.

Loading...

We can use the SymPy function sp.summation to compute the infinite sum symbolically.

Loading...

Example 18: Natural logarithm of 2

We can calculate the value ln⁡(2)\ln(2) by computing the infinite sum of the alternating harmonic sequence ak=(−1)k+1ka_k = \tfrac{(-1)^{k+1}}{k}:

A∞=lim⁡n→∞∑k=1n(−1)k+1k=1−12+13−14+15−16+⋯=ln⁡(2).A_\infty = \lim_{n \to \infty} \sum_{k=1}^n \tfrac{(-1)^{k+1}}{k} = 1 - \tfrac{1}{2} + \tfrac{1}{3} - \tfrac{1}{4} + \tfrac{1}{5} - \tfrac{1}{6} + \cdots = \ln(2).
Alternating geometric series up to n= 10 is 0.6456349206349206
Alternating geometric series up to n= 100 is 0.6881721793101953
Alternating geometric series up to n= 1000 is 0.6926474305598203
Alternating geometric series up to n= 1000000 is 0.6931466805601953
Loading...
Loading...

Power series

The term power series describes a series whose terms contain different powers of the variable xx. The kkthe term in a power series consists of some coefficient ckc_k and the kkth power of xx:

Pn(x)=∑k=0nckxk=c0+c1x+c2x2+c3x3+⋯+cnxn.P_n(x) = \sum_{k=0}^n c_k x^k = c_0 + c_1x + c_2x^2 + c_3x^3 % + c_4x^4 + c_5x^5 + \cdots + c_nx^n.

The math expression we obtain in this way is a polynomial of degree nn in xx, which we denote Pn(x)P_n(x). Depending on the choice of the coefficients (c0,c1,c2,c3,…,cn)(c_0, c_1, c_2, c_3, \ldots, c_n) we can make the polynomial function Pn(x)P_n(x) approximate some other function f:R→Rf : \mathbb{R} \to \mathbb{R}. To find such approximations, we need some way to choose the coefficients ckc_k of the power series, so that the resulting polynomial approximates the function: Pn(x)≈f(x)P_n(x) \approx f(x).

Taylor series

The Taylor series approximation to the function f(x)f(x) is a power series whose coefficients ckc_k are computed by evaluating the kkth derivative of the function f(x)f(x) at x=0x=0, which we denote f(k)(0)f^{(k)}(0). Specifically, the kkth coefficient in the Taylor series approximation for the function f(x)f(x) is ck=f(k)(0)k!c_k = \frac{f^{(k)}(0)}{k!}, where k!k! is the factorial function.

The finite Taylor series with nn terms produces the following approximation:

f(x)≈f(0)+f′ ⁣(0)x+f′′(0)2!x2+⋯+f(n)(0)n!xn=∑k=0nf(k)(0)k!xk.\begin{align*} f(x) &\approx f(0) +f^{\prime\!}(0)x +\tfrac{f^{\prime\prime}(0)}{2!}x^2 % +\tfrac{f^{(3)}(0)}{3!}x^3 + \cdots +\tfrac{f^{(n)}(0)}{n!}x^n \\ % +\tfrac{f^{\prime\prime\prime}(0)}{3!}x^3 % +\tfrac{f^{(4)}(0)}{4!}x^4 + \cdots \\ &= \sum_{k=0}^n \tfrac{f^{(k)}(0)}{k!}x^k. \end{align*}

In the limit as nn goes to infinity, the Taylor series approximation becomes exactly equal to the function f(x)f(x):

f(x)=f(0)+f′ ⁣(0)x+f′′(0)2!x2+f(3)(0)3!x3+⋯=∑k=0∞f(k)(0)k!xk.\begin{align*} f(x) &= f(0) + f^{\prime\!}(0)x + \tfrac{f^{\prime\prime}(0)}{2!}x^2 + \tfrac{f^{(3)}(0)}{3!}x^3 + \cdots \\ &= \sum_{k=0}^\infty \tfrac{f^{(k)}(0)}{k!}x^k. \end{align*}

Using this formula and your knowledge of derivative formulas, you can compute the Taylor series of any function f(x)f(x). For example, let’s find the Taylor series of the function f(x)=exf(x) = e^x at x=0x=0. The first derivative of f(x)=exf(x) = e^x is f′ ⁣(x)=exf^{\prime\!}(x) = e^x. The second derivative of f(x)=exf(x) = e^x is f′′ ⁣(x)=exf^{\prime\prime\!}(x) = e^x. In fact, all the derivatives of f(x)f(x) will be exe^x because the derivative of exe^x is equal to exe^x. The kkth coefficient in the power series of f(x)=exf(x)=e^x at the point x=0x=0 is equal to the value of the kkth derivative of f(x)f(x) evaluated at x=0x=0 divided by k!k!. In the case of f(x)=exf(x) = e^x, we have f(k)(0)=e0=1f^{(k)}(0) = e^0 = 1. The coefficient of the kkth term is ck=f(k)(0)k!=1k!c_k = \tfrac{f^{(k)}(0)}{k!} = \tfrac{1}{k!}, so the Taylor series of f(x)=exf(x) = e^x is

ex=∑k=0∞1k!xk=1+x+x22+x33!+x44!+x55!+x66!+x77!+⋯e^x = \sum_{k=0}^\infty \tfrac{1}{k!}x^k = 1 + x + \tfrac{x^2}{2} + \tfrac{x^3}{3!} + \tfrac{x^4}{4!} + \tfrac{x^5}{5!} + \tfrac{x^6}{6!} + \tfrac{x^7}{7!} + \cdots

Taylor series are a powerful computational tool for approximating functions. As we compute more terms from the above series, the polynomial approximation to the function f(x)=exf(x)=e^x becomes more accurate.

Obtaining Taylor series using SymPy

The SymPy function series is a convenient way to obtain the series of any function fun. Calling sp.series(fun,var,x0,n) will show you the series expansion of expr near var=x0 including all powers of var less than n.

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Applications of series

Series allow us to compute numbers like ee, π\pi, ln⁡(2)\ln(2), etc. Taylor series allow us to approximate functions exe^x, sin⁡(x)\sin(x), cos⁡(x)\cos(x), etc.

The Taylor series representation for the function f(x)f(x) provides an easy way to compute its integral function F0(x)=∫0xf(u) duF_0(x) = \int_0^x f(u)\,du. The Taylor series f(x)=∑k=0∞ckxkf(x) = \sum_{k=0}^\infty c_k x^k consists only of polynomial terms of the form ckxkc_k x^k. To compute the integral function F0F_0, we can compute the integral of each term, which is ∫ckxk dx=ckk+1xk+1\int c_k x^k \,dx = \frac{c_k}{k+1}x^{k+1}. This means the Taylor series of the integral function is F0(x)=∑k=0∞ckk+1xk+1+CF_0(x) = \sum_{k=0}^\infty \frac{c_k}{k+1}x^{k+1} + C.

Advanced calculus

Solutions to practice problems

Learning calculus requires calculating lots of limits, derivatives, and integrals. Here are some practice problems for you.

P1 Calculate the following limit expressions:

(a) lim⁡x→∞7x+4(b) lim⁡x→∞4x2−7x+1x2(c) lim⁡x→0−1x\text{(a)}~\lim_{x\to \infty} \tfrac{7}{x+4} \qquad \text{(b)}~\lim_{x\to\infty}\tfrac{4x^2-7x+1}{x^2} \qquad \text{(c)}~\lim_{x\to 0^-} \tfrac{1}{x}
Loading...
Loading...
Loading...

P2 Assuming lim⁡x→∞f(x)=2\displaystyle\lim_{x\to\infty} f(x)= 2 and lim⁡x→∞g(x)=3\displaystyle\lim_{x\to\infty} g(x)=3, compute:

(a)  ⁣ ⁣lim⁡x→∞(2f(x)−g(x))(b)  ⁣ ⁣lim⁡x→∞f(x)g(x)(c)  ⁣ ⁣lim⁡x→∞4f(x)g(x)+1\text{(a)}~\!\!\lim_{x\to\infty}\bigl(2f(x)-g(x)\bigr) \quad \text{(b)}~\!\!\lim_{x\to\infty} f(x)g(x) \quad \text{(c)}~\!\!\lim_{x\to\infty}\tfrac{4f(x)}{g(x)+1}

P3 Find the derivative with respect to xx of the functions:

(a) f(x)=x13(b) g(x)=x3(c) h(x)=ax2+bx+c\text{(a)}~f(x) = x^{13} \qquad \text{(b)}~g(x) = \sqrt[3]{x} \qquad \text{(c)}~h(x) = ax^2 + bx + c
Loading...
Loading...
Loading...

P4 Calculate the derivatives of the following functions:

(a) p(x)=2x+33x+2(b) q(x)=x2+1(c) r(θ)=sin⁡3θ\text{(a)}~p(x) = \tfrac{2x + 3}{3x + 2} \qquad \text{(b)}~q(x) = \sqrt{x^2 + 1} \qquad \text{(c)}~r(\theta) = \sin^3 \theta
Loading...
Loading...
Loading...

P5 Find the maximum and the minimum of f(x)=x5−5xf(x) = x^5-5x.

Loading...
When x = -1 , f''(-1) = -20 so maximum
When x = 1 , f''(1) = 20 so minimum

P6 Calculate the integral function F0(b)=∫0bf(x) dxF_0(b) = \int_0^b f(x)\,dx for the polynomial f(x)=4x3+3x2+2x+1f(x) = 4x^3 + 3x^2 + 2x + 1.

Loading...

P7 Find the area under f(x)=8−x3f(x)=8-x^3 from x=0x=0 to x=2x=2.

Loading...

P8 Find the area under the graph of the function g(x)=sin⁡(x)g(x)=\sin(x) between x=0x=0 to x=πx=\pi.

Loading...

P9 Compute ∫014x(1+x2)3 dx\int_{0}^{1} \frac{4x}{(1+x^2)^3}\,dx using the substitution u=1+x2u = 1 + x^2. Check your answer numerically using the SciPy function quad.

Loading...

P10 Calculate the value of the infinite series ∑k=0∞(23) ⁣k\sum_{k=0}^\infty \left(\frac{2}{3}\right)^{\!k}.

Loading...

P11 Find the Taylor series for the function f(x)=e−xf(x) = e^{-x}.

Loading...

Here are some recommended resources to learn more about calculus.

[ Calculus for beginners and srtists ]
https://math.mit.edu/~djk/calculus_beginners/
via https://news.ycombinator.com/item?id=41086531

[ Jim Fowler video lectures ]
https://www.youtube.com/@kisonecat/playlists

[ Highlights of Calculus lectures by Gilbert Strang ]
https://www.youtube.com/playlist?list=PLBE9407EA64E2C318

[ Nice visualization of integration ]
https://x.com/abakcus/status/1898933342457758033

Book plug

This material in this tutorial comes from ...