Lesson 2
Understanding Limits
Lesson Introduction

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.

Concept of Limits

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.

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

Limit Calculation Step-by-Step

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

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

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

Python Implementation

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
Code Breakdown
  1. Define Limit Function: The limit function calculates the function value for a small increment hh. Here, f is the function, x is the point, and h is a small number.
  2. Define Sample Function: We use a lambda function f(x)=x2f(x) = x^2 to keep it simple.
  3. Compute and Print: Compute the limit as xx approaches 2 and print the result.
Importance of h

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

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

Let's see what happens with different hh 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 hh gets larger (like h=0.1h = 0.1), the limit's accuracy decreases.

Example Function

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

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

Note that the function is not defined at x=0x = 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)f(0). To find the most appropriate value of sin(x)x\frac{\sin(x)}{x} at x=0x = 0 one can use limits. Let's observe what happens as xx approaches 0.

Calculating the Limit

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

This means that as xx gets very close to 0, the value of sin(x)x\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 sin(h)h\frac{\sin(h)}{h}.

Redefining the Function

Knowing this, we can redefine our function:

f(x)={sin(x)xif x01if x=0f(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)=sinxxf(x) = \frac{\sin{x}}{x} is equal to 11 at x=0x=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=0x=0.

Lesson Summary

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)=x2f(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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.