Taylor polynomials
Taylor polynomials (lesson 2.7.1)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Standard series and the error
Standard series and the size of the error (lesson 2.7.2)
0 of 8 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Coding exercise
Taylor polynomials and the series for ln(1 + x)
Write two functions.
taylor_poly(derivs, a, x)returns the Taylor polynomial aboutaatx(lesson 2.7.1). The listderivsholds , so its length minus one is the degree.math.factorial(k)gives .ln1p_series(x, n)returns the sum of the firstnterms of (lesson 2.7.2). If is not in , raiseValueError: the series does not converge there.
The tests use about 0, about 2 and about 4, on both sides of the centre. They also check , the slow series at , and the two values of where the series fails.
See the tests
import math
def test_exp_about_0():
"""eˣ about 0 to degree 3, at 0.5, gives 1.645833…"""
expect_close(taylor_poly([1, 1, 1, 1], 0, 0.5), 1 + 0.5 + 0.5**2 / 2 + 0.5**3 / 6, what="taylor_poly([1, 1, 1, 1], 0, 0.5)")
def test_factorials():
"""The k! is there: [1, 1, 1, 1] about 0 at x = 1 gives 8/3, not 4"""
expect_close(taylor_poly([1, 1, 1, 1], 0, 1), 8 / 3, what="taylor_poly([1, 1, 1, 1], 0, 1)")
def test_cubic_about_2():
"""x³ about 2, with derivatives [8, 12, 12, 6], is exact: 15.625 at 2.5"""
expect_close(taylor_poly([8, 12, 12, 6], 2, 2.5), 2.5**3, what="taylor_poly([8, 12, 12, 6], 2, 2.5)")
def test_left_of_centre():
"""√x about 4 to degree 2, at 3.8 (left of the centre), gives 1.949375"""
expect_close(taylor_poly([2, 1 / 4, -1 / 32], 4, 3.8), 1.949375, what="taylor_poly([2, 1/4, -1/32], 4, 3.8)")
def test_degree_0():
"""With one value, the copy is the constant f(a)"""
expect_close(taylor_poly([5], 3, 10), 5, what="taylor_poly([5], 3, 10)")
def test_ln_two_terms():
"""Two terms at x = 0.05 give 0.04875"""
expect_close(ln1p_series(0.05, 2), 0.04875, what="ln1p_series(0.05, 2)")
def test_ln_inside():
"""40 terms at x = −0.5 give ln 0.5 to about 13 digits"""
expect_close(ln1p_series(-0.5, 40), math.log(0.5), what="ln1p_series(-0.5, 40)", rel=1e-12)
def test_ln_slow_end():
"""x = 1 is allowed but slow: 1,000 terms are within 0.001 of ln 2"""
value = ln1p_series(1, 1000)
assert value is not None and abs(value - math.log(2)) < 0.001, f"expected within 0.001 of ln 2 = {math.log(2):.6f}, got {value}"
def test_ln_beyond_1():
"""x = 1.5 raises ValueError: the series diverges there"""
try:
ln1p_series(1.5, 10)
except ValueError:
return
raise AssertionError("expected ValueError for x = 1.5")
def test_ln_at_minus_1():
"""x = −1 raises ValueError: there the series is minus the harmonic series"""
try:
ln1p_series(-1, 10)
except ValueError:
return
raise AssertionError("expected ValueError for x = -1")