Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Implementing Limits in Python | Mathematical Analysis
Mathematics for Data Science

bookImplementing Limits in Python

Before exploring how limits behave visually, you need to know how to compute them directly using the sympy library. Here are three common types of limits you'll encounter.

1. Finite Limit

This example shows a function that approaches a specific finite value as x2x \to 2.

12345678
import sympy as sp x = sp.symbols('x') f = (x**2 - 4) / (x - 2) # Compute the limit as x approaches 2 limit_value = sp.limit(f, x, 2) print("Limit of f(x) as x approaches 2:", limit_value)
copy

2. Limit That Does Not Exist

Here, the function behaves differently from the left and right sides, so the limit does not exist.

1234567891011
import sympy as sp x = sp.symbols('x') f = (2 - x) # Compute the limit as x approaches infinity and negative infinity left_limit = sp.limit(f, x, -sp.oo) right_limit = sp.limit(f, x, sp.oo) print("Left-hand limit:", left_limit) print("Right-hand limit:", right_limit)
copy

3. Infinite Limit

This example shows a function that approaches zero as (x) grows infinitely large.

12345678
import sympy as sp x = sp.symbols('x') f = 1 / x # Compute the limit as x approaches infinity limit_value = sp.limit(f, x, sp.oo) print("Limit of 1/x as x approaches infinity:", limit_value)
copy

These short snippets demonstrate how to use sympy.limit() to compute different types of limits - finite, undefined, and infinite - before analyzing them graphically

Defining the Functions

f_diff = (2 - x)  # Approaches 2 as x → -∞ and -∞ as x → +∞
f_same = 1 / x  # Approaches 0 as x → ±∞
f_special = sp.sin(x) / x  # Special limit sin(x)/x
  • f_diff: a simple linear function where the left-hand and right-hand limits diverge;
  • f_same: the classic reciprocal function, approaching the same limit from both sides;
  • f_special: a well-known limit in calculus, which equals 1 as x0x \to 0.

Handling Division by Zero

y_vals_same = [f_same.subs(x, val).evalf() if val != 0 else np.nan for val in x_vals]
y_vals_special = [f_special.subs(x, val).evalf() if val != 0 else 1 for val in x_vals]
  • The function f_same = 1/x has an issue at x=0x = 0 (division by zero), so we replace that with NaN (Not a Number) to avoid errors;
  • For f_special, we know that limx0sin(x)x=1\lim_{\raisebox{-1pt}{$x \to 0$}}\frac{\raisebox{1pt}{$sin(x)$}}{\raisebox{-1pt}{$x$}} = 1, so we manually assign 11 when x=0x = 0.

Plotting Horizontal Asymptotes

axs[1].axhline(0, color='blue', linestyle='dashed', linewidth=2, label='y = 0 (horizontal asymptote)')
axs[2].axhline(1, color='red', linestyle='dashed', linewidth=2, label=r"$y = 1$ (horizontal asymptote)")
  • The function 1/x has a horizontal asymptote at y=0y = 0;
  • The function sin(x)/x approaches y=1y = 1, so we add a dashed red line for visual clarity.
question mark

Which sympy function is used to compute the limit of a function in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about how to interpret the results of these limits?

What is the significance of horizontal asymptotes in these examples?

Could you provide more details about the special limit involving sin(x)/x?

Awesome!

Completion rate improved to 1.96

bookImplementing Limits in Python

Swipe to show menu

Before exploring how limits behave visually, you need to know how to compute them directly using the sympy library. Here are three common types of limits you'll encounter.

1. Finite Limit

This example shows a function that approaches a specific finite value as x2x \to 2.

12345678
import sympy as sp x = sp.symbols('x') f = (x**2 - 4) / (x - 2) # Compute the limit as x approaches 2 limit_value = sp.limit(f, x, 2) print("Limit of f(x) as x approaches 2:", limit_value)
copy

2. Limit That Does Not Exist

Here, the function behaves differently from the left and right sides, so the limit does not exist.

1234567891011
import sympy as sp x = sp.symbols('x') f = (2 - x) # Compute the limit as x approaches infinity and negative infinity left_limit = sp.limit(f, x, -sp.oo) right_limit = sp.limit(f, x, sp.oo) print("Left-hand limit:", left_limit) print("Right-hand limit:", right_limit)
copy

3. Infinite Limit

This example shows a function that approaches zero as (x) grows infinitely large.

12345678
import sympy as sp x = sp.symbols('x') f = 1 / x # Compute the limit as x approaches infinity limit_value = sp.limit(f, x, sp.oo) print("Limit of 1/x as x approaches infinity:", limit_value)
copy

These short snippets demonstrate how to use sympy.limit() to compute different types of limits - finite, undefined, and infinite - before analyzing them graphically

Defining the Functions

f_diff = (2 - x)  # Approaches 2 as x → -∞ and -∞ as x → +∞
f_same = 1 / x  # Approaches 0 as x → ±∞
f_special = sp.sin(x) / x  # Special limit sin(x)/x
  • f_diff: a simple linear function where the left-hand and right-hand limits diverge;
  • f_same: the classic reciprocal function, approaching the same limit from both sides;
  • f_special: a well-known limit in calculus, which equals 1 as x0x \to 0.

Handling Division by Zero

y_vals_same = [f_same.subs(x, val).evalf() if val != 0 else np.nan for val in x_vals]
y_vals_special = [f_special.subs(x, val).evalf() if val != 0 else 1 for val in x_vals]
  • The function f_same = 1/x has an issue at x=0x = 0 (division by zero), so we replace that with NaN (Not a Number) to avoid errors;
  • For f_special, we know that limx0sin(x)x=1\lim_{\raisebox{-1pt}{$x \to 0$}}\frac{\raisebox{1pt}{$sin(x)$}}{\raisebox{-1pt}{$x$}} = 1, so we manually assign 11 when x=0x = 0.

Plotting Horizontal Asymptotes

axs[1].axhline(0, color='blue', linestyle='dashed', linewidth=2, label='y = 0 (horizontal asymptote)')
axs[2].axhline(1, color='red', linestyle='dashed', linewidth=2, label=r"$y = 1$ (horizontal asymptote)")
  • The function 1/x has a horizontal asymptote at y=0y = 0;
  • The function sin(x)/x approaches y=1y = 1, so we add a dashed red line for visual clarity.
question mark

Which sympy function is used to compute the limit of a function in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
some-alt