Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs1-24fa
minimathematica
Commits
5b518fb4
Commit
5b518fb4
authored
7 months ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
Update taylor.py
parent
27ec1842
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/taylor.py
+9
-10
src/taylor.py
with
9 additions
and
10 deletions
+9
-10
src/taylor.py
View file @
5b518fb4
...
...
@@ -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
):
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help