Rates of change and tangents
Rates of change and tangents (lesson 2.2.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.
The derivative and finite differences
The derivative and finite differences (lesson 2.2.2)
0 of 9 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
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
Forward, backward and central differences
Write forward_diff(f, x, h), backward_diff(f, x, h) and central_diff(f, x, h), the three estimates from lesson 2.2.2. Each should also work when x is a NumPy array.
The tests compare them with known derivatives. They also check how fast each error shrinks: halving should about halve the forward error and quarter the central one.
See the tests
import math
import numpy as np
def _cube(x):
return x**3
def test_forward():
"""Forward difference of x³ at 1 with h = 0.1 is 3.31"""
expect_close(forward_diff(_cube, 1.0, 0.1), 3.31, what="forward_diff(x³, 1, 0.1)")
def test_backward():
"""Backward difference of x³ at 1 with h = 0.1 is 2.71"""
expect_close(backward_diff(_cube, 1.0, 0.1), 2.71, what="backward_diff(x³, 1, 0.1)")
def test_central():
"""Central difference of x³ at 1 with h = 0.1 is 3.01"""
expect_close(central_diff(_cube, 1.0, 0.1), 3.01, what="central_diff(x³, 1, 0.1)")
def test_central_more_accurate():
"""For e^x at 0 (true slope 1), the central difference is closer than the forward and backward ones"""
fwd = abs(forward_diff(math.exp, 0.0, 0.01) - 1)
bwd = abs(backward_diff(math.exp, 0.0, 0.01) - 1)
cen = abs(central_diff(math.exp, 0.0, 0.01) - 1)
assert cen < fwd and cen < bwd, f"central error {cen:.2e} should be below forward {fwd:.2e} and backward {bwd:.2e}"
def test_error_orders():
"""For x³ at 1, halving h from 0.01 about halves the forward error and quarters the central one"""
fwd = abs(forward_diff(_cube, 1.0, 0.01) - 3) / abs(forward_diff(_cube, 1.0, 0.005) - 3)
cen = abs(central_diff(_cube, 1.0, 0.01) - 3) / abs(central_diff(_cube, 1.0, 0.005) - 3)
assert 1.9 < fwd < 2.1, f"the forward error should shrink about 2 times, it shrank {fwd:.3f} times"
assert 3.9 < cen < 4.1, f"the central error should shrink about 4 times, it shrank {cen:.3f} times"
def test_arrays():
"""All three work on an array of points: x² has slope 2x"""
x = np.array([0.0, 1.0, 2.5])
expect_close(central_diff(lambda t: t**2, x, 1e-3), 2 * x, what="central_diff(x², [0, 1, 2.5])")
expect_close(forward_diff(lambda t: t**2, x, 1e-3), 2 * x + 1e-3, what="forward_diff(x², [0, 1, 2.5])")
expect_close(backward_diff(lambda t: t**2, x, 1e-3), 2 * x - 1e-3, what="backward_diff(x², [0, 1, 2.5])")