Separable equations and Euler's method
Differential equations and separable equations (lesson 6.1.1)
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.
Linear equations and solve_ivp
Linear equations and integrating factors (lesson 6.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
Euler's method, and a step that halves itself
Write two functions for an equation , where f(t, y) returns the slope.
euler(f, t0, y0, t_end, n)takesnEuler steps from tot_end, each of length . It returns two NumPy arrays of length : the times and the values . Ift_endis beforet0, is negative and the steps run backwards. RaiseValueErrorunlessnis a positive whole number.euler_converge(f, t0, y0, t_end, tol, n0=10)starts withn0steps and doubles the number until two answers in a row att_enddiffer by less thantol. Return the last answer and its number of steps, as a pair.
Euler's error is about proportional to (lesson 6.1.1), so the gap between two answers in a row is about the error of the second. For from to with tol=1e-3, the second function stops at 2,560 steps, about 0.00053 below .
See the tests
import math
import numpy as np
def growth(t, y):
return 0.5 * y
def test_four_steps():
"""Four steps of 0.5 for y' = 0.5y from 1 give 1.25^4 = 2.441406"""
ts, ys = euler(growth, 0, 1, 2, 4)
expect_close(ys[-1], 1.25**4, what="y after 4 steps")
def test_arrays():
"""The times run from t0 to t_end in n + 1 values, with y0 first"""
ts, ys = euler(growth, 0, 1, 2, 4)
expect_equal(len(ts), 5, what="len(ts)")
expect_equal(len(ys), 5, what="len(ys)")
expect_close(ts, [0, 0.5, 1, 1.5, 2], what="ts")
expect_close(ys[0], 1.0, what="ys[0]")
def test_slope_depends_on_t():
"""Two steps of 0.1 for y' = t + y from y(0) = 1 give 1.1, then 1.22"""
ts, ys = euler(lambda t, y: t + y, 0, 1, 0.2, 2)
expect_close(ys, [1, 1.1, 1.22], what="ys")
def test_backwards():
"""2,000 steps back from y(2) = e to t = 0 for y' = 0.5y land near 1"""
ts, ys = euler(growth, 2, math.e, 0, 2000)
expect_close(ts[-1], 0.0, what="last time")
expect_close(ys[-1], 1.0, what="y at t = 0", rel=1e-3)
def test_bad_n():
"""n = 0 and n = 2.5 raise ValueError"""
for n in (0, 2.5):
try:
euler(growth, 0, 1, 2, n)
except ValueError:
continue
raise AssertionError(f"expected ValueError for n = {n}")
def test_converge():
"""For y' = 0.5y to t = 2 with tol 0.001, the steps double to 2,560 and land 0.00053 below e"""
value, n = euler_converge(growth, 0, 1, 2, 1e-3)
expect_equal(n, 2560, what="number of steps")
expect_close(value, math.e, what="answer", rel=3e-4)
def test_converge_settling():
"""For y' = 0.8(2 - y) from 4 to t = 5 with tol 10^-5, the answer is within 10^-5 of 2 + 2e^-4"""
value, n = euler_converge(lambda t, y: 0.8 * (2 - y), 0, 4, 5, 1e-5)
expect_close(value, 2 + 2 * math.exp(-4), what="answer", rel=0, abs_tol=1e-5)