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)
(f, x, toler, iter_max)
| 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.1 |
| 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 - limit_low_prev) <= toler \ |
| 29 | and math.fabs(limit_up - limit_up_prev) <= toler \ |
| 30 | and math.fabs(limit_up - limit_low) <= toler: |
| 31 | converged = True |
| 32 | break |
| 33 | |
| 34 | limit_up_prev = limit_up |
| 35 | limit_low_prev = limit_low |
| 36 | |
| 37 | if math.fabs(limit_up - limit_low) > 10 * toler: |
| 38 | raise ValueError("Two sided limit does not exist.") |
| 39 | |
| 40 | return limit_low, i, converged |