Lesson 5

Welcome! Today, we'll explore **integrals**, a fundamental concept in calculus that's key to many machine learning applications. Integrals help us understand areas under curves, representing things like accumulated quantities over time. By the end of this lesson, you'll know what integrals are, why they're important, and how to compute them using Python.

An integral in calculus sums infinite tiny pieces to calculate the total area under a curve. This is useful in many real-life situations. Think about measuring the distance a car travels by knowing its speed over time. The integral helps us accumulate those tiny speed changes to find the total distance.

There are two main types of integrals: indefinite and definite. An indefinite integral represents a family of functions and includes a constant of integration. A definite integral calculates the exact area under the curve between two points, (a) and (b).

The most straightforward way to calculate a complex area is to approximate it with rectangles. Here is how we can do it with a simple $y = x^2$ curve:

In this plot, the sum of areas of the white rectangles is close to the area of the function.

We can make this calculation more accurate by using more rectangles:

It is easy to see how `20`

rectangles approximate the area under the curve way more accurate than `10`

. The formula for the area would be:

where:

$f(x_i^*)$ is the height of the rectangle at the sample point $x_i^*$. $\Delta x = \frac{b - a}{n}$ is the width of each rectangle.

The key idea of the integral is to take this approximation to the limit: by using infinitely many rectangles with infinitely small size each, we can get the exact area under the curve:

And here is the final formula. We apply the limit $n \to \infty$ to the sum of rectangles. By doing so, we ask: what does this sum approach when the amount of rectangles approaches infinity?

$\int_a^b f(x)dx = \lim_{n \to \infty} \sum_{i=1}^n f(x_i^*) \Delta x$where:

- $\int_a^b f(x)dx$ is the integral symbol representing the area under the curve from $a$ to $b$.
- $\Delta x = \frac{b - a}{n}$ is the width of each rectangle.
- $x_i^*$ is a sample point in the $i$-th subinterval $[x_{i-1}, x_i]$.
- $x_0 = a$, $x_i = a + i \Delta x$, and $x_n = b$.

It might seem difficult to calculate, but in reality it is not! We need this formula only to understand the concept. To actually calculate an integral, we are going to apply another formula, called **the fundamental theorem of calculus**.

The **Fundamental Theorem of Calculus** connects differentiation and integration, showing that they are essentially inverse processes. It states that if a function $F(x)$ is the antiderivative of $f(x)$, then the definite integral of $f$ from $a$ to $b$ can be computed as follows:

$\int_a^b f(x)dx = F(b) - F(a)$

An antiderivative of a function $f(x)$ is another function $F(x)$ such that the derivative of $F(x)$ equals $f(x)$. In other words, $F'(x) = f(x)$.

Example: Integrating $f(x) = x^2$

Let's illustrate this with an example by finding the integral of $f(x) = x^2$ from 0 to 1.

**Find the Antiderivative:**To find the antiderivative of $f(x) = x^2$, we look for a function $F(x)$ whose derivative is $x^2$. The antiderivative of $f(x) = x^2$ is $F(x) = \frac{x^3}{3} + C$, where C is a constant.**Evaluate at the Bounds:**- $F(1) = \frac{1^3}{3} = \frac{1}{3}$
- $F(0) = \frac{0^3}{3} = 0$

**Subtract the Values:**$\int_0^1 x^2dx = F(1) - F(0) = \frac{1}{3} - 0 = \frac{1}{3}$

Thus, the integral of $x^2$ from 0 to 1 is $\frac{1}{3}$.

This method provides an exact solution by leveraging the antiderivative, simplifying the process into straightforward evaluations at the bounds.

Sometimes, it's challenging to find the exact integral of a function analytically. Numerical methods like the **Trapezoidal Rule** approximate the area under a curve by dividing it into small trapezoids, which is exactly what we started with. With python, we can approximate a curve with thousands of rectangles, which will be still not **exact**, but very close to it. In fact, as close as we wish it to be!

Let's dive into numerical integration using the Trapezoidal Rule with Python.

Python`1# Numerical integration using the trapezoidal rule 2import numpy as np 3 4def integrate(f, a, b, n=1000): 5 x = np.linspace(a, b, n) # Generate n points between a and b 6 y = f(x) # Apply function to each point 7 area = np.trapz(y, x) # Use trapezoidal rule to estimate the integral 8 return area 9 10# Sample function: f(x) = x^2 11f = lambda x: x**2 12 13# Compute integral of f from 0 to 1 14result = integrate(f, 0, 1) 15print("Integral of f(x) from 0 to 1:", result) # Integral of f(x) from 0 to 1: 0.3333335`

Step-by-Step Explanation:

**Importing Libraries:**We import`numpy`

for numerical operations.**Defining the Integration Function:**The`integrate`

function takes a function`f`

, limits`a`

and`b`

, and the number of points`n`

.**Generating Points:**`np.linspace(a, b, n)`

creates`n`

evenly spaced points from`a`

to`b`

.**Applying the Function:**`f(x)`

calculates the function's value at each point.**Calculating the Area:**`np.trapz(y, x)`

computes the integral using the trapezoidal rule.

As you can see, the result is **very** close to $\frac{1}{3}$, which we know to be the correct answer.

Fantastic! You've learned the basics of integrals, their significance, and how to approximate them using the Trapezoidal Rule. We also walked through a Python implementation of this method.

Now it's time to put this knowledge into practice. In the next session, you'll use the `integrate`

function to compute integrals of different functions and explore further applications of numerical integration. Let's dive into those exercises and solidify your understanding!