Description
1. Consider a RungeโKutta method starting at (๐ก๐,๐ฅ๐) that takes an Euler step of length โ/2 to (๐ก๐+1/2,๐ฅ๐+1/2) and then uses the new evaluation at that point to take a complete Euler step from (๐ก๐,๐ฅ๐) of length โ.
Find the order of this method and write down its Butcher tableau. We will refer to it as the โmidpoint methodโ.
2. Define a type RKMethod to represent a general explicit RungeโKutta method defined by a Butcher tableau as follows:
struct RKMethod{T} c::Vector{T} b::Vector{T} a::Matrix{T} s::Int # number of stages end
Make it into a function by completing the function
function (method::RKMethod)(f, x, t, h) … end
to execute one step of the corresponding RungeโKutta method with initial condition ๐ฅ at time ๐ก and step size โ. Your code should work for both scalar and vector ๐ฅ and a possibly vector-valued function ๐ = ๐(๐ก,๐ฅ)
(Assume that ๐ is a lower-triangular matrix, corresponding to an explicit method.)
3. Define RK methods euler, midpoint and RK4 using their respective tableaus.
4. Write a routine integrate with the signature function integrate(method, f, x0, t0, t_final, h) where method is a RK method as defined above and โ is a fixed step size.
Make sure that the final step lands exactly at the final time by taking that final step as a special case. 5. Use each method on the ODE ๐ฅฬ = 1.5๐ฅ with ๐ฅ0 = 2 and integrate from ๐ก = 0 for a time ๐กfinal = 3.
Find the rate of convergence of the numerical solution to the exact solution as โ โ 0 for each method. Do they correspond to our analytical expectations?
๐ขฬ(๐ก) = exp [๐ก โ ๐ข sin(๐ข)].
Integrate it using RK4 from ๐ก = 0 to ๐ก = 5 with a step size โ = 10โ2. Now integrate it with a step size โ = 10โ3.
Plot both solutions ๐ฅ(๐ก) as a function of ๐ก. What do you observe? What do you think is happening?
Exercise 2: Adaptivity in the Euler method
In this exercise we will invetigate adaptivity in ODE solvers by taking the simplest case: an adaptive Euler method.
1. Consider one step of the Euler method. Write down the local (single-step) error in terms of the step size โ and the unknown constant ๐ถ. Call the approximation obtained at the end of the step ๐ฅ(1).
2. Now consider taking two consecutive Euler steps of size โ/2. Would you expect this to give a better or a worse approximation to the true solution? Write down the total error after taking the two steps, assuming that the constant ๐ถ is the same for both. Call the approximation at the end of this combined step ๐ฅ(2).
3. Define ฮ๐ฅ as the difference between the two approximations. Use this to find the step size โโฒ that will give an error per unit time of a given size
๐.
4. Use this derivation to write an adaptive Euler integrator adaptive_euler(f, t0, t_final, epsilon) that tries to keep the global error less than ๐, using an update rule similar to the one we discussed in class. Add a multiplicative factor 0.9 to that rule to make the method behave better.
5. Use it to integrate the same ODE as in exercise 1. Plot the step size taken as a function of time.
6. Now integrate the equation for the van der Pol oscillator:
๐ฅฬ โ ๐(1 โ ๐ฅ2)๐ฅฬ + ๐ฅ = 0,
with ๐ = 5. Use initial conditions ๐ฅ0 = 0 and ๐ฅฬ0 = 1.
7. Plot trajectories in (๐ฅ,๐ฅ)ฬ phase space and (separately) the solution ๐ฅ(๐ก) as a function of ๐ก.
8. Plot the step size as a function of time. What do you observe? How do you interpret this?
Exercise 3: SIR model In this exercise we will study the SIR model of the dynamics of an infectious disease outbreak (โepidemicโ) in a population.
1. Use e.g. RK4 to solve the SIR equations:
๐ = โ๐ฝ๐ ๐ผฬ (1)
๐ผ = ๐ฝ๐ ๐ผ โ ๐พ๐ผฬ (2)
๐
= ๐พ๐ผฬ (3)
Here ๐ผ is the proportion of the population which is infectious. ๐ฝ is the rate of contact between susceptible and infectious individuals, and ๐พ the recovery rate.
Use ๐0 = 0.99 and ๐ผ0 = 0.01.
2. Make an interactive visualization, varying ๐ฝ and ๐พ in, say, the range 0 to 1.
3. What do you observe? Can you interpret this?
4. By summing the equations we see that ๐ + ๐ผ + ๐
should be constant (equal to the total population, assuming no births or deaths). For representative parameter values ๐ฝ = 0.1 and ๐พ = 0.05, how well does the numerics conserve this quantity?
Reviews
There are no reviews yet.