diff --git a/src/taylor.py b/src/taylor.py index 8f443f686039cbc8fbec414ad0438b33851be402..b56735ef552dd5b157419ff2708f30d049f2e022 100644 --- a/src/taylor.py +++ b/src/taylor.py @@ -7,15 +7,13 @@ from src.differentiation import evaluate_diff from math import factorial, ceil -N = 10 - @cache def nth_derivative(func, n): """self explanatory for now...""" return None @cache -def taylor(func, x): +def taylor(func, x, N): """ Returns the taylor expansion of func as a function of x, centered at 'b'. Since the 0th term is just the function itself (which we don't have the answer for yet), @@ -25,13 +23,13 @@ def taylor(func, x): """ return None -def taylor_at(func, b, f_at_b): +def taylor_at(func, b, f_at_b, N): """ Returns taylor(-), evaluated by plugging in f_at_b for 'q' and b for 'b'. """ return None -def max_error(func, a, x, n=N): +def max_error(func, a, x, n): """ Calculates the maximum error of the estimate below using Taylor's Theorem in the following way. @@ -55,7 +53,7 @@ def max_error(func, a, x, n=N): return None @cache -def estimate(func, x, a, func_at_a): +def estimate(func, x, a, func_at_a, N): """ Uses the taylor expansion centered around a to compute an estimate for func(x). """ @@ -64,19 +62,20 @@ def estimate(func, x, a, func_at_a): PI = 3.14159265358979323846264338327950288419716939937510582097494 def arctan(x): + N = 10 if x == 0: return 0, 0 elif x == 1: - return PI/4, Decimal(10**-16) + return pi/4, Decimal(10**-16) a = 0 total_error = Decimal(0) prev_value = 0 while a + 1 <= x: - prev_value = estimate('arctan', a + 1, a, prev_value) - total_error += max_error('arctan', a, a + 1) + prev_value = estimate('arctan', a + 1, a, prev_value, N) + total_error += max_error('arctan', a, a + 1, N) a += 1 - return estimate('arctan', x, floor(x), prev_value), total_error + max_error('arctan', floor(x), x) + return estimate('arctan', x, floor(x), prev_value, N), total_error + max_error('arctan', floor(x), x, N) if __name__ == "__main__": for i in range(0, 4):