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.