A4 Python for engineers · Practice
Runge-Kutta and step size
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
Write rk4_step(f, t, y, h) and use it to integrate from over one time constant (tau = 2) in n = 5 steps. Set v to the final value and err to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Integrate from to with Euler and with RK4, using n = 20 and then 2 * n steps. Set ratio_euler and ratio_rk4 to (error with n steps) / (error with 2n steps) for each method.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Simulate a series RLC (R = 20 Ω, L = 10 mH, C = 1 µF) driven by a 1 V step from rest, with RK4, state [v, i], step 1 µs, for 2000 steps. Set v to the array of capacitor voltages after each step and peak to its largest value.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A pendulum of length Lp = 0.5 m obeys with = 9.81 m/s². Released from rest at th0 = 45°, integrate it with RK4 (state [theta, omega], step 1 ms) until has crossed zero going downward twice. Set T to the time between those two crossings (interpolate linearly within each step), and ratio to T divided by the small-angle period .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Integrate from to with RK4, once with n = 5 steps (y1) and once with 2 * n (y2). For a fourth-order method, the error in y2 (that is, y2 minus the exact answer) is about . Set est to that estimate and true_err to y2 - math.exp(-1).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Use RK4 with 100 steps to integrate (a = 1) from over ; set y to the result.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Two RK4 runs with step and gave y1 = 0.6065 and y2 = 0.6065. Set est to the estimated error of y2, .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4