Implementing 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 xβ2.
12345678import 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)
2. Limit That Does Not Exist
Here, the function behaves differently from the left and right sides, so the limit does not exist.
1234567891011import 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)
3. Infinite Limit
This example shows a function that approaches zero as (x) grows infinitely large.
12345678import 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)
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 xβ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/xhas an issue at x=0 (division by zero), so we replace that withNaN(Not a Number) to avoid errors; - For
f_special, we know that limxβ0ββxβsin(x)ββ=1, so we manually assign 1 when x=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/xhas a horizontal asymptote at y=0; - The function
sin(x)/xapproaches y=1, so we add a dashed red line for visual clarity.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 1.96
Implementing 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 xβ2.
12345678import 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)
2. Limit That Does Not Exist
Here, the function behaves differently from the left and right sides, so the limit does not exist.
1234567891011import 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)
3. Infinite Limit
This example shows a function that approaches zero as (x) grows infinitely large.
12345678import 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)
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 xβ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/xhas an issue at x=0 (division by zero), so we replace that withNaN(Not a Number) to avoid errors; - For
f_special, we know that limxβ0ββxβsin(x)ββ=1, so we manually assign 1 when x=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/xhas a horizontal asymptote at y=0; - The function
sin(x)/xapproaches y=1, so we add a dashed red line for visual clarity.
Thanks for your feedback!