Skip to content

Commit 3b510b8

Browse files
committed
limits code added
1 parent 010b96a commit 3b510b8

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

limits.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#finding limits of a function using user defined function
2+
def find_limit(func, x, approach):
3+
if approach == 'left':
4+
return func(x - 0.0001) # Approaching from the left
5+
elif approach == 'right':
6+
return func(x + 0.0001) # Approaching from the right
7+
else:
8+
raise ValueError("Approach must be 'left' or 'right'.")
9+
10+
11+
# Example usage:
12+
if __name__ == "__main__":
13+
# Define a sample function
14+
def function(x):
15+
return (x**3 - 1) / (x - 1)
16+
17+
x_value = 1
18+
left_limit = find_limit(function, x_value, 'left')
19+
right_limit = find_limit(function, x_value, 'right')
20+
21+
22+
23+
24+
print(f"Left-hand limit as x approaches {x_value}: {left_limit}")
25+
print(f"Right-hand limit as x approaches {x_value}: {right_limit}")
26+
if abs(left_limit - right_limit) < 0.1:
27+
print(f"The limit as x approaches {x_value} is equal to: {int(right_limit)}")
28+
else:
29+
print(f"The limit does not exist as the left-hand limit ({left_limit}) and right-hand limit ({right_limit}) are not equal.Hence limit does not exist.")
30+
31+
32+
#plot function to visualize limits
33+
import numpy as np
34+
import matplotlib.pyplot as plt
35+
x_vals = np.linspace(0.5, 1.5, 100)
36+
y_vals = [function(x) for x in x_vals]
37+
plt.plot(x_vals, y_vals, label='f(x)')
38+
plt.axvline(x=x_value, color='red', linestyle='--', label=f'x = {x_value}')
39+
plt.xlabel('x')
40+
plt.ylabel('f(x)')
41+
plt.title('Function Visualization')
42+
plt.legend()
43+
plt.grid(True)
44+
plt.show()

0 commit comments

Comments
 (0)