Polynomials
Polynomials and their roots (lesson 1.3.1)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
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.
Bisection
Bisection (lesson 1.3.2)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
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.
Newton's method
Newton's method (lesson 1.3.3)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
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
Bisection, with a safety check
Write bisect(f, a, b, tol=1e-10) that returns a root of f between a and b, accurate to within tol.
- If
f(a)andf(b)have the same sign, the bracket proves nothing: there may be no root, two roots or a double root inside. RaiseValueError. - If
f(a)orf(b)is already exactly 0, return that end. - Otherwise halve the interval until it is narrower than
tol, keeping the half with the sign change, and return its midpoint.
The tests use rising and falling functions, a bracket with three roots inside, and brackets with no sign change.
See the tests
import math
def test_square_root_of_two():
"""x² − 2 on [1, 2] gives √2"""
expect_close(bisect(lambda x: x**2 - 2, 1, 2), math.sqrt(2), what="bisect(x² − 2, 1, 2)", rel=1e-9)
def test_cubic():
"""x³ − x − 1 on [1, 2]: the answer makes the cubic zero"""
r = bisect(lambda x: x**3 - x - 1, 1, 2)
expect_close(r**3 - r - 1, 0.0, what="f at your root", abs_tol=1e-8)
def test_decreasing_function():
"""A falling function works too: 5 − x on [0, 10] gives 5"""
expect_close(bisect(lambda x: 5 - x, 0, 10), 5.0, what="bisect(5 − x, 0, 10)", rel=1e-9)
def test_tolerance():
"""A looser tolerance still lands within it: x² − 2 with tol = 0.01"""
r = bisect(lambda x: x**2 - 2, 1, 2, tol=0.01)
assert abs(r - math.sqrt(2)) < 0.01, f"expected within 0.01 of √2, got {r}"
def test_root_at_an_end():
"""If f(a) is already 0, return a"""
expect_close(bisect(lambda x: x - 1, 1, 3), 1.0, what="bisect(x − 1, 1, 3)")
def test_no_bracket():
"""No sign change between a and b raises ValueError"""
try:
bisect(lambda x: x**2 + 1, -1, 1)
except ValueError:
return
raise AssertionError("expected ValueError: x² + 1 is positive at both ends")
def test_root_at_b():
"""If f(b) is already 0, return b"""
expect_close(bisect(lambda x: x - 3, 1, 3), 3.0, what="bisect(x − 3, 1, 3)")
def test_several_roots():
"""(x − 1)(x − 2)(x − 3) on [0, 4.5] holds three roots: any one of them is right"""
r = bisect(lambda x: (x - 1) * (x - 2) * (x - 3), 0, 4.5)
nearest = min(abs(r - root) for root in (1, 2, 3))
assert nearest < 1e-9, f"expected one of 1, 2 or 3, got {r}"
def test_double_root():
"""(x − 1)² on [0, 3] has a double root but no sign change: raise ValueError"""
try:
bisect(lambda x: (x - 1) ** 2, 0, 3)
except ValueError:
return
raise AssertionError("expected ValueError: (x − 1)² is positive at both ends")