Lesson 2

Welcome to our lesson on understanding *limits*, a fundamental concept in calculus with important applications in machine learning. Limits help us understand how functions behave as we approach certain points, which is crucial for defining derivatives and integrals. By the end, you'll grasp what limits are, learn how to compute them numerically, and see how to implement this in Python.

A limit in mathematics describes the value a function approaches as the input nears some value. Imagine driving toward a red light. As you get closer, you're approaching a specific point where you'll stop. That's similar to limits: as the input nears a particular value, the output of the function approaches a specific value.

$\lim_{{x \to a}} f(x) = L$ This reads as "the limit of $f(x)$ as $x$ approaches $a$ is $L$."

Consider the function $f(x) = x^2$. To find the limit as $x$ approaches 2, think about what happens to $f(x)$ when $x$ gets very close to 2. The values of $x^2$ will get closer to $2^2 = 4$.

Think of $x$ as a car. If we're getting closer to 2, what number is $x^2$ approaching? Like the car nearing the red light, $x^2$ gets closer to 4 as $x$ approaches 2.

We can calculate this practically using a small value $h$: $f(x + h)$ Here, $h$ is a very small number.

Let's see how to calculate limits numerically using Python.

Python`1# Numeric limit calculation 2def limit(f, x, h=1e-5): 3 return f(x + h) 4 5# Sample function: f(x) = x^2 6f = lambda x: x**2 7 8# Compute limit of f at x=2 9print("Limit of f(x) as x approaches 2:", limit(f, 2)) # Limit of f(x) as x approaches 2: 4.00004`

**Define Limit Function**: The`limit`

function calculates the function value for a small increment $h$. Here,`f`

is the function,`x`

is the point, and`h`

is a small number.**Define Sample Function**: We use a lambda function $f(x) = x^2$ to keep it simple.**Compute and Print**: Compute the limit as $x$ approaches 2 and print the result.

The choice of $h$ is crucial. It needs to be small but not too small to avoid numerical errors due to computer precision. Typically, $h$ is set around $10^{-5}$ or smaller for good accuracy.

Consider this: if you’re too close to the wall (like $h$ being too small), you can't see much of the wall's shape. Step a tiny bit back (a reasonable $h$), and the shape becomes clearer without much distortion.

Let's see what happens with different $h$ values:

Python`1# Numeric limit calculation 2def limit(f, x, h=1e-5): 3 return f(x + h) 4 5# Sample function: f(x) = x^2 6f = lambda x: x**2 7 8print("Limit with h=1e-5:", limit(f, 2, 1e-5)) # Limit with h=1e-5: 4.00004 9print("Limit with h=1e-3:", limit(f, 2, 1e-3)) # Limit with h=1e-3: 4.004 10print("Limit with h=1e-1:", limit(f, 2, 1e-1)) # Limit with h=1e-1: 4.41`

You'll notice that as $h$ gets larger (like $h = 0.1$), the limit's accuracy decreases.

Let's examine the function $\frac{\sin(x)}{x}$, which is undefined at $x = 0$. This is because division by zero is not allowed in mathematics.

Here is the graph of $\frac{\sin(x)}{x}$:

Note that the function is not defined at $x = 0$.

For some tasks, we might want this function to be defined at every point, which means we will have to somehow define the $f(0)$. To find the most appropriate value of $\frac{\sin(x)}{x}$ at $x = 0$ one can use limits. Let's observe what happens as $x$ approaches 0.

The limit is: $\lim_{{x \to 0}} \frac{\sin(x)}{x} = 1$

This means that as $x$ gets very close to 0, the value of $\frac{\sin(x)}{x}$ gets very close to 1. This can be clearly seen on the plot. Also, we can verify it with calculations.

Python`1import numpy as np 2 3def limit_sin_x_over_x(h=0.00001): 4 # Using a small value of h to approximate the limit 5 return np.sin(h) / h 6 7# Compute limit of sin(x)/x as x approaches 0 8print("Limit of sin(x)/x as x approaches 0:", limit_sin_x_over_x()) # 0.999999...`

In this code, we calculate the limit using a slightly adjusted expression of $\frac{\sin(h)}{h}$.

Knowing this, we can redefine our function:

$f(x) = \begin{cases} \frac{\sin(x)}{x} & \text{if } x \neq 0 \\ 1 & \text{if } x = 0 \end{cases}$Now, the plot of the function will look like this:

Note: that doesn't mean that the original $f(x) = \frac{\sin{x}}{x}$ is equal to $1$ at $x=0$. It is still undefined. We used limits to determine the value it approaches and manually inserted it, constructing a new function, which looks similar, but **is defined** at $x=0$.

We've covered what limits are, why they are important, and how to calculate them both conceptually and numerically. We broke down a Python code snippet demonstrating this process for the function $f(x) = x^2$. Remember, limits help understand how functions behave as inputs get close to a particular point, which is key for more advanced topics like derivatives.

Now it's time to put your new knowledge into practice! You'll be working on hands-on exercises where you'll code your own limit functions and evaluate limits for different functions and points in the CodeSignal IDE. Happy coding!