Maxima and minima
Maxima and minima (lesson 2.4.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.
Concavity
Concavity and the second derivative (lesson 2.4.2)
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.
Newton's method
Newton's method with derivatives (lesson 2.4.3)
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.
Coding exercise
Newton's method with a bracket
Write safe_newton(f, fprime, a, b, tol=1e-12, max_steps=100) that returns a root of f in the bracket , as in lesson 2.4.3.
- If
f(a)orf(b)is 0, return that end. If they have the same sign, raiseValueError: there is no bracket. - Start at the midpoint. After each guess , shrink the bracket to the half that still has the sign change.
- Take the Newton step when
fprime(x)is not 0 and the step lands strictly inside the bracket. Otherwise take the midpoint of the bracket. - Return when a step moves by less than
tol, or whenf(x)is exactly 0. RaiseRuntimeErroraftermax_stepssteps.
The tests include the cycle of lesson 1.3.3, a flat tangent at the start and the runaway . One test allows only 12 steps, so bisection alone is too slow.
See the tests
import math
def test_square_root():
"""x² − 2 on [1, 2] gives √2"""
expect_close(safe_newton(lambda x: x**2 - 2, lambda x: 2 * x, 1, 2), math.sqrt(2), what="root of x² − 2", rel=1e-12)
def test_x_exp_x():
"""xeˣ − 1 on [0, 1] gives about 0.567143"""
r = safe_newton(lambda x: x * math.exp(x) - 1, lambda x: math.exp(x) * (1 + x), 0, 1)
expect_close(r, 0.567143290409784, what="root of xeˣ − 1", rel=1e-12)
def test_fast():
"""It takes Newton steps: xeˣ − 1 on [0, 1] converges within 12 steps"""
try:
r = safe_newton(lambda x: x * math.exp(x) - 1, lambda x: math.exp(x) * (1 + x), 0, 1, max_steps=12)
except RuntimeError:
raise AssertionError("no answer within 12 steps: bisection alone needs about 40, Newton about 6")
expect_close(r, 0.567143290409784, what="root of xeˣ − 1 within 12 steps", rel=1e-12)
def test_cycle():
"""x³ − 2x + 2 on [−3, 3], where plain Newton from 0 cycles, gives about −1.769292"""
r = safe_newton(lambda x: x**3 - 2 * x + 2, lambda x: 3 * x**2 - 2, -3, 3)
expect_close(r, -1.769292354238631, what="root of x³ − 2x + 2", rel=1e-12)
def test_flat_start():
"""x³ − 3x − 1 on [0, 2] starts on a flat tangent at 1 and gives about 1.879385"""
r = safe_newton(lambda x: x**3 - 3 * x - 1, lambda x: 3 * x**2 - 3, 0, 2)
expect_close(r, 1.8793852415718169, what="root of x³ − 3x − 1 in [0, 2]", rel=1e-12)
def test_runaway():
"""xe⁻ˣ on [−1, 4], where plain Newton from 1.5 runs away, gives 0"""
r = safe_newton(lambda x: x * math.exp(-x), lambda x: (1 - x) * math.exp(-x), -1, 4)
expect_close(r, 0.0, what="root of xe⁻ˣ", abs_tol=1e-12)
def test_root_at_end():
"""A root at an end of the bracket is returned: x² − 4 on [2, 5] gives 2"""
expect_close(safe_newton(lambda x: x**2 - 4, lambda x: 2 * x, 2, 5), 2.0, what="root of x² − 4 on [2, 5]")
def test_no_bracket():
"""x² + 1 on [−1, 1] has no sign change: ValueError"""
try:
safe_newton(lambda x: x**2 + 1, lambda x: 2 * x, -1, 1)
except ValueError:
return
raise AssertionError("expected ValueError when f(a) and f(b) have the same sign")
def test_max_steps():
"""Stopping after max_steps without converging raises RuntimeError"""
try:
safe_newton(lambda x: x**2 - 2, lambda x: 2 * x, 1, 2, max_steps=2)
except RuntimeError:
return
raise AssertionError("expected RuntimeError after 2 steps without converging")