Diffusion and the heat equation
Diffusion and the heat equation (lesson 6.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.
The fundamental solution
The fundamental solution (lesson 6.4.2)
0 of 8 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
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
The heat equation from any start
Write heat_solution(u0, x, t). It returns for on the whole line, started from the profile u0 (a function of one number), by lesson 6.4.2's integral .
- Integrate with
quadover a window of 12 widths on each side of , not over a fixed range. For a tiny the bell is too narrow forquadto find in a wide range. - At return
u0(x). For raiseValueError: the equation cannot be run backwards from a start.
The tests try the step, the kink, , (which becomes ), a box near its edge, , and a negative .
See the tests
import math
from scipy.stats import norm
def _step(y):
return 1.0 if y > 0 else 0.0
def _kink(y):
return max(y, 0.0)
def _box(y):
return 1.0 if -1 < y < 1 else 0.0
def test_step():
"""The step from 0 to 1 gives N(1) = 0.841345 at x = 1, t = 0.5"""
expect_close(heat_solution(_step, 1, 0.5), norm.cdf(1), what="heat_solution(step, 1, 0.5)", rel=1e-8)
def test_kink():
"""The kink max(x, 0) is lifted to sqrt(t/pi) = 0.398942 at x = 0, t = 0.5"""
expect_close(heat_solution(_kink, 0, 0.5), math.sqrt(0.5 / math.pi), what="heat_solution(kink, 0, 0.5)", rel=1e-8)
def test_exponential():
"""e^x becomes e^(x + t): e^2 at x = 1, t = 1"""
expect_close(heat_solution(math.exp, 1, 1), math.exp(2), what="heat_solution(exp, 1, 1)", rel=1e-8)
def test_square():
"""x^2 becomes x^2 + 2t: 2 at x = 1, t = 0.5"""
expect_close(heat_solution(lambda y: y * y, 1, 0.5), 2.0, what="heat_solution(x^2, 1, 0.5)", rel=1e-8)
def test_box_near_its_edge():
"""A box of height 1 on (-1, 1) gives 0.760250 at x = 0.9, t = 0.01"""
exact = norm.cdf(1.9 / math.sqrt(0.02)) - norm.cdf(-0.1 / math.sqrt(0.02))
expect_close(heat_solution(_box, 0.9, 0.01), exact, what="heat_solution(box, 0.9, 0.01)", rel=1e-7)
def test_tiny_time():
"""At t = 0.000001 the kink has barely moved: 0.5 at x = 0.5"""
expect_close(heat_solution(_kink, 0.5, 1e-6), 0.5, what="heat_solution(kink, 0.5, 1e-6)", rel=1e-6)
def test_time_zero():
"""At t = 0 the answer is the starting profile itself"""
expect_close(heat_solution(_step, 0.3, 0), 1.0, what="heat_solution(step, 0.3, 0)")
def test_negative_time():
"""A negative time raises ValueError"""
try:
heat_solution(_kink, 0, -0.1)
except ValueError:
return
raise AssertionError("expected ValueError: the heat equation cannot be run backwards from u0")