Implementing Limits in Python
Limits are crucial in modeling. Here you'll not only learn how to effectively use Python (along with helpful libraries sympy
and matplotlib
), but also how to evaluate, graph and analyze these graphs.
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/x
has 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→0xsin(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/x
has a horizontal asymptote at y=0; - The function
sin(x)/x
approaches y=1, so we add a dashed red line for visual clarity.
1. In the function f_same = 1 / x
, what happens if you replace NaN
handling with float('inf')
for x = 0
in y_vals_same
?
2. What would happen if we defined f_special = sp.sin(x) / x
but changed the x range to exclude negative values?
3. When plotting f_same = 1 / x
, why do we check if val != 0
before computing function values?
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 2
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 1.89
Implementing Limits in Python
Veeg om het menu te tonen
Limits are crucial in modeling. Here you'll not only learn how to effectively use Python (along with helpful libraries sympy
and matplotlib
), but also how to evaluate, graph and analyze these graphs.
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/x
has 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→0xsin(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/x
has a horizontal asymptote at y=0; - The function
sin(x)/x
approaches y=1, so we add a dashed red line for visual clarity.
1. In the function f_same = 1 / x
, what happens if you replace NaN
handling with float('inf')
for x = 0
in y_vals_same
?
2. What would happen if we defined f_special = sp.sin(x) / x
but changed the x range to exclude negative values?
3. When plotting f_same = 1 / x
, why do we check if val != 0
before computing function values?
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 2