Partial differential equations
What a partial differential equation is (lesson 6.3.1)
0 of 10 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
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.
Conditions
Initial, boundary and terminal conditions (lesson 6.3.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
Residuals by central differences
Write two functions that test a candidate solution at one point, as in lesson 6.3.1. Here u is a function of two arguments, u(x, t).
heat_residual(u, x, t, h=1e-4)returns at . Estimate by the central difference in and by the second difference in , with steph.transport_residual(u, c, x, t, h=1e-5)returns at , with central differences in and in .
Raise ValueError if h is not positive.
The tests use the solutions and failures of lesson 6.3.1. They need about six correct digits, which a one-sided difference cannot reach.
See the tests
import math
def test_heat_solution():
"""e⁻ᵗ sin x solves the heat equation: its residual at (1, 0.5) is about 0"""
r = heat_residual(lambda x, t: math.exp(-t) * math.sin(x), 1.0, 0.5)
expect_close(r, 0.0, what="heat_residual of e^-t sin x at (1, 0.5)", abs_tol=1e-6)
def test_heat_failure():
"""e⁻ᵗ sin 2x fails: its residual at (1, 0.5) is 3e^(−0.5) sin 2, about 1.654550"""
r = heat_residual(lambda x, t: math.exp(-t) * math.sin(2 * x), 1.0, 0.5)
expect_close(r, 3 * math.exp(-0.5) * math.sin(2), what="heat_residual of e^-t sin 2x at (1, 0.5)", rel=1e-6)
def test_heat_polynomial():
"""x² + 2t solves the heat equation: its residual at (3, 2) is about 0"""
r = heat_residual(lambda x, t: x**2 + 2 * t, 3.0, 2.0)
expect_close(r, 0.0, what="heat_residual of x^2 + 2t at (3, 2)", abs_tol=1e-5)
def test_heat_frozen():
"""x² does not solve the heat equation: its residual is −2 everywhere"""
r = heat_residual(lambda x, t: x**2, 0.7, 0.3)
expect_close(r, -2.0, what="heat_residual of x^2 at (0.7, 0.3)", rel=1e-6)
def test_transport_solution():
"""e^(−(x − 2t)²) solves u_t + 2u_x = 0: its residual at (2.5, 1) is about 0"""
r = transport_residual(lambda x, t: math.exp(-(x - 2 * t) ** 2), 2, 2.5, 1.0)
expect_close(r, 0.0, what="transport_residual of the bump with c = 2", abs_tol=1e-7)
def test_transport_wrong_speed():
"""The same bump with c = −2 leaves a residual of about 3.115203 at (2.5, 1)"""
r = transport_residual(lambda x, t: math.exp(-(x - 2 * t) ** 2), -2, 2.5, 1.0)
expect_close(r, 4 * math.exp(-0.25), what="transport_residual of the bump with c = -2", rel=1e-6)
def test_transport_wrong_direction():
"""(x + 2t)² moves left: with c = 2 its residual at (1, 1) is 8x + 16t = 24"""
r = transport_residual(lambda x, t: (x + 2 * t) ** 2, 2, 1.0, 1.0)
expect_close(r, 24.0, what="transport_residual of (x + 2t)^2 with c = 2", rel=1e-6)
def test_bad_step():
"""A step that is zero or negative raises ValueError in both functions"""
for name, call in [("heat_residual", lambda: heat_residual(lambda x, t: x, 1.0, 1.0, h=0)),
("transport_residual", lambda: transport_residual(lambda x, t: x, 1, 1.0, 1.0, h=-1e-5))]:
try:
call()
except ValueError:
continue
raise AssertionError(f"{name} should raise ValueError for a step that is not positive")