Skip to content

Commit 81b6bc1

Browse files
committed
Added 'limit' calculus
1 parent a12f5ed commit 81b6bc1

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ python3 main.py
3939

4040
## Implementations
4141

42+
### Limits
43+
44+
- Epsilon-delta method
45+
4246
### Solutions of equations
4347

4448
- Bisection method

limits.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Methods for compute limits."""
2+
3+
import math
4+
5+
6+
def limit_epsilon_delta(f, x, toler, iter_max):
7+
"""Calculate a limit using the epsilon-delta definition.
8+
9+
Args:
10+
f (function): equation f(x).
11+
x (float): the value the independent variable is approaching.
12+
toler (float): tolerance (stopping criterion).
13+
iter_max (int): maximum number of iterations (stopping criterion).
14+
15+
Returns:
16+
limit (float): the limit value.
17+
"""
18+
delta = 0.5
19+
limit_low_prev = -math.inf
20+
limit_up_prev = math.inf
21+
22+
converged = False
23+
for i in range(0, iter_max + 1):
24+
delta /= (i + 1)
25+
limit_low = f(x - delta)
26+
limit_up = f(x + delta)
27+
28+
if math.fabs(limit_low_prev - limit_low) <= toler \
29+
and math.fabs(limit_up_prev - limit_up) <= toler \
30+
and math.fabs(limit_up_prev - limit_low_prev) <= toler:
31+
converged = True
32+
break
33+
34+
limit_up_prev = limit_up
35+
limit_low_prev = limit_low
36+
37+
if limit_low * limit_up < 0 or math.fabs(limit_up - limit_low) > toler:
38+
raise ValueError("Two sided limit does not exist.")
39+
40+
return limit_low, i, converged

main.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import differentiation
1414
import integration
1515
import interpolation
16+
import limits
1617
import linear_systems
1718
import linear_systems_iterative
1819
import ode
@@ -30,6 +31,30 @@ def wrapper(*args, **kwargs):
3031
return wrapper
3132

3233

34+
@print_docstring
35+
def example_limit_epsilon_delta():
36+
"""Run an example 'Limits: epsilon-delta definition'."""
37+
def f(x):
38+
return math.sin(x) / x
39+
40+
x = 0
41+
toler = 10 ** -5
42+
iter_max = 100
43+
44+
print("Inputs:")
45+
print(f"x = {x}")
46+
print(f"toler = {toler}")
47+
print(f"iter_max = {iter_max}")
48+
49+
print("Execution:")
50+
limit, i, converged = limits.limit_epsilon_delta(f, x, toler, iter_max)
51+
52+
print("Output:")
53+
print(f"limit = {limit:.5f}")
54+
print(f"i = {i}")
55+
print(f"converged = {converged}")
56+
57+
3358
@print_docstring
3459
def example_solution_bisection():
3560
"""Run an example 'Solutions: Bisection'."""
@@ -732,6 +757,9 @@ def main():
732757
"""Run the main function."""
733758
# Execute all examples
734759

760+
# Limits
761+
example_limit_epsilon_delta()
762+
735763
# Solutions of equations
736764
example_solution_bisection()
737765
example_solution_secant()

0 commit comments

Comments
 (0)