-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Methods for compute limits.""" | ||
|
||
import math | ||
|
||
|
||
def limit_epsilon_delta(f, x, toler, iter_max): | ||
"""Calculate a limit using the epsilon-delta definition. | ||
Args: | ||
f (function): equation f(x). | ||
x (float): the value the independent variable is approaching. | ||
toler (float): tolerance (stopping criterion). | ||
iter_max (int): maximum number of iterations (stopping criterion). | ||
Returns: | ||
limit (float): the limit value. | ||
""" | ||
delta = 0.5 | ||
limit_low_prev = -math.inf | ||
limit_up_prev = math.inf | ||
|
||
converged = False | ||
for i in range(0, iter_max + 1): | ||
delta /= (i + 1) | ||
limit_low = f(x - delta) | ||
limit_up = f(x + delta) | ||
|
||
if math.fabs(limit_low_prev - limit_low) <= toler \ | ||
and math.fabs(limit_up_prev - limit_up) <= toler \ | ||
and math.fabs(limit_up_prev - limit_low_prev) <= toler: | ||
converged = True | ||
break | ||
|
||
limit_up_prev = limit_up | ||
limit_low_prev = limit_low | ||
|
||
if limit_low * limit_up < 0 or math.fabs(limit_up - limit_low) > toler: | ||
raise ValueError("Two sided limit does not exist.") | ||
|
||
return limit_low, i, converged |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters