Commit 5b518fb4 authored by Adam Blank's avatar Adam Blank
Browse files

Update taylor.py

parent 27ec1842
Showing with 9 additions and 10 deletions
+9 -10
...@@ -7,15 +7,13 @@ from src.differentiation import evaluate_diff ...@@ -7,15 +7,13 @@ from src.differentiation import evaluate_diff
from math import factorial, ceil from math import factorial, ceil
N = 10
@cache @cache
def nth_derivative(func, n): def nth_derivative(func, n):
"""self explanatory for now...""" """self explanatory for now..."""
return None return None
@cache @cache
def taylor(func, x): def taylor(func, x, N):
""" """
Returns the taylor expansion of func as a function of x, centered at 'b'. 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), 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): ...@@ -25,13 +23,13 @@ def taylor(func, x):
""" """
return None 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'. Returns taylor(-), evaluated by plugging in f_at_b for 'q' and b for 'b'.
""" """
return None 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 Calculates the maximum error of the estimate below using Taylor's Theorem
in the following way. in the following way.
...@@ -55,7 +53,7 @@ def max_error(func, a, x, n=N): ...@@ -55,7 +53,7 @@ def max_error(func, a, x, n=N):
return None return None
@cache @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). 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): ...@@ -64,19 +62,20 @@ def estimate(func, x, a, func_at_a):
PI = 3.14159265358979323846264338327950288419716939937510582097494 PI = 3.14159265358979323846264338327950288419716939937510582097494
def arctan(x): def arctan(x):
N = 10
if x == 0: if x == 0:
return 0, 0 return 0, 0
elif x == 1: elif x == 1:
return PI/4, Decimal(10**-16) return pi/4, Decimal(10**-16)
a = 0 a = 0
total_error = Decimal(0) total_error = Decimal(0)
prev_value = 0 prev_value = 0
while a + 1 <= x: while a + 1 <= x:
prev_value = estimate('arctan', a + 1, a, prev_value) prev_value = estimate('arctan', a + 1, a, prev_value, N)
total_error += max_error('arctan', a, a + 1) total_error += max_error('arctan', a, a + 1, N)
a += 1 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__": if __name__ == "__main__":
for i in range(0, 4): for i in range(0, 4):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment