Functions of two variables
Functions of two variables: surfaces and level curves (lesson 4.1.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.
Partial derivatives
Partial derivatives (lesson 4.1.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
Partial derivatives by central differences
Write two functions. In both, f takes one argument per variable, so f(*point) gives its value. point is a list such as [1, 2] or [2, 3, 4].
partials(f, point, h=1e-5)returns a list with one central difference for each variable, as in lesson 4.1.2. Move that coordinate by and keep the others fixed.second_partial(f, point, i, j, h=1e-4)returns the second partial in variablei, then variablej(counting from 0). Fori == juse the second central difference; otherwise use the four corner points.
Raise ValueError if h is not positive. Do not change the list point: build a new list for each moved point.
The tests use the bowl , the box volume , and from lessons 4.1.1 and 4.1.2. One test needs 8 correct digits, which a one-sided difference cannot reach.
See the tests
import math
def test_bowl():
"""x² + 2y² at (1, 1) has partials 2 and 4"""
expect_close(partials(lambda x, y: x**2 + 2 * y**2, [1, 1]), [2, 4], what="partials of x² + 2y² at (1, 1)", rel=1e-8)
def test_three_variables():
"""The box volume xyz at (2, 3, 4) has partials 12, 8 and 6"""
expect_close(partials(lambda x, y, z: x * y * z, [2, 3, 4]), [12, 8, 6], what="partials of xyz at (2, 3, 4)", rel=1e-8)
def test_central_accuracy():
"""e^(xy) at (1, 2) is accurate to 8 digits, which a one-sided difference is not"""
e2 = math.exp(2)
expect_close(partials(lambda x, y: math.exp(x * y), [1, 2]), [2 * e2, e2], what="partials of e^(xy) at (1, 2)", rel=1e-8)
def test_point_unchanged():
"""The point passed in is left as it was"""
point = [1.0, 2.0]
partials(lambda x, y: x * y, point)
second_partial(lambda x, y: x * y, point, 0, 1)
expect_equal(point, [1.0, 2.0], what="the point after the calls")
def test_second_partials():
"""x²y + 3y² at (2, 1): fxx = 2, fyy = 6 and fxy = 4"""
f = lambda x, y: x**2 * y + 3 * y**2
expect_close(second_partial(f, [2, 1], 0, 0), 2, what="fxx", rel=1e-5)
expect_close(second_partial(f, [2, 1], 1, 1), 6, what="fyy", rel=1e-5)
expect_close(second_partial(f, [2, 1], 0, 1), 4, what="fxy", rel=1e-5)
def test_mixed_symmetric():
"""For e^(xy) at (1, 2) the mixed partial is 3e², in either order"""
f = lambda x, y: math.exp(x * y)
expect_close(second_partial(f, [1, 2], 0, 1), 3 * math.exp(2), what="fxy", rel=1e-5)
expect_close(second_partial(f, [1, 2], 1, 0), 3 * math.exp(2), what="fyx", rel=1e-5)
def test_three_variable_mixed():
"""For xyz at (2, 3, 4) the mixed partial in x and z is y = 3"""
expect_close(second_partial(lambda x, y, z: x * y * z, [2, 3, 4], 0, 2), 3, what="the xz partial of xyz", rel=1e-6)
def test_bad_step():
"""A step of 0 raises ValueError"""
try:
partials(lambda x, y: x + y, [1, 1], h=0)
except ValueError:
return
raise AssertionError("expected ValueError for h = 0")