Solving and Visualizing a Limit Problem in Calculus

Here is an example of solving a limit problem in calculus using $Python$, along with visualization.


Problem

Find the limit of the following function as $( x \to 0 )$ and visualize it using $Python$:

$$
f(x) = \frac{\sin(x)}{x}
$$

It is known that the limit of this function as $( x \to 0 )$ is $1$.


Python Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp

# Symbolic calculation to find the limit
x = sp.symbols('x')
f = sp.sin(x) / x
limit_value = sp.limit(f, x, 0)
print(f"The limit as x -> 0 is: {limit_value}")

# Visualization
x_vals = np.linspace(-2*np.pi, 2*np.pi, 400)
y_vals = np.sin(x_vals) / x_vals

# Replace NaN at x=0 with the limit value
y_vals[np.isnan(y_vals)] = limit_value

plt.figure(figsize=(8, 6))
plt.plot(x_vals, y_vals, label=r"$f(x) = \frac{\sin(x)}{x}$")
plt.scatter(0, limit_value, color='red', label=f"Limit at x=0: {limit_value}")
plt.axhline(limit_value, color='gray', linestyle='--', alpha=0.5)
plt.axvline(0, color='gray', linestyle='--', alpha=0.5)
plt.title(r"Limit of $\frac{\sin(x)}{x}$ as $x \to 0$")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.legend()
plt.grid(True)
plt.show()

Explanation

  1. Symbolic Calculation:

    • Using sympy, we compute the limit of $( f(x) = \frac{\sin(x)}{x} )$ as $( x \to 0 )$.
    • The result is 1.
  2. Visualization:

    • We generate $ x $ values using numpy and compute the corresponding $ f(x) $ values.
    • At $ x = 0 $, $ \frac{\sin(0)}{0} $ is undefined (NaN), so we replace it with the limit value 1.
    • The graph highlights the point $ x = 0 $ with a red dot and shows the limit value as a horizontal dashed line.
The limit as x -> 0 is: 1

  1. Graph Interpretation:
    • The graph shows that $ f(x) $ approaches 1 as $ x \to 0 $.
    • Although the function appears continuous at $ x = 0 $, it is actually undefined at that point.

Execution Result

  • Symbolic calculation result: The limit as x -> 0 is: 1
  • Graph: The graph visually confirms that $ f(x) $ converges to 1 as $ x \to 0 $.