Skip to content

Commit

Permalink
Added 'limit' calculus
Browse files Browse the repository at this point in the history
  • Loading branch information
cfgnunes committed Apr 30, 2023
1 parent 3fa9354 commit d2091ce
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ python3 main.py

## Implementations

### Limits

- Epsilon-delta method

### Solutions of equations

- Bisection method
Expand Down
40 changes: 40 additions & 0 deletions limits.py
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
28 changes: 28 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import differentiation
import integration
import interpolation
import limits
import linear_systems
import linear_systems_iterative
import ode
Expand All @@ -30,6 +31,30 @@ def wrapper(*args, **kwargs):
return wrapper


@print_docstring
def example_limit_epsilon_delta():
"""Run an example 'Limits: epsilon-delta definition'."""
def f(x):
return math.sin(x) / x

x = 0
toler = 10 ** -5
iter_max = 100

print("Inputs:")
print(f"x = {x}")
print(f"toler = {toler}")
print(f"iter_max = {iter_max}")

print("Execution:")
limit, i, converged = limits.limit_epsilon_delta(f, x, toler, iter_max)

print("Output:")
print(f"limit = {limit:.5f}")
print(f"i = {i}")
print(f"converged = {converged}")


@print_docstring
def example_solution_bisection():
"""Run an example 'Solutions: Bisection'."""
Expand Down Expand Up @@ -732,6 +757,9 @@ def main():
"""Run the main function."""
# Execute all examples

# Limits
example_limit_epsilon_delta()

# Solutions of equations
example_solution_bisection()
example_solution_secant()
Expand Down

0 comments on commit d2091ce

Please sign in to comment.