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 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:
is the height of the rectangle at the sample point . 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 to the sum of rectangles. By doing so, we ask: what does this sum approach when the amount of rectangles approaches infinity?
where:
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 is the antiderivative of , then the definite integral of from to can be computed as follows:
An antiderivative of a function is another function such that the derivative of equals . In other words, .
Example: Integrating
Let's illustrate this with an example by finding the integral of from 0 to 1.
Thus, the integral of from 0 to 1 is .
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.
Python1# 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:
numpy
for numerical operations.integrate
function takes a function f
, limits a
and b
, and the number of points n
.np.linspace(a, b, n)
creates n
evenly spaced points from a
to b
.f(x)
calculates the function's value at each point.np.trapz(y, x)
computes the integral using the trapezoidal rule.As you can see, the result is very close to , 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!