18-330 – Exercise 1: Rungeโ€“Kutta methods (Solution)

$ 29.99
Category:

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.

Be the first to review “18-330 – Exercise 1: Rungeโ€“Kutta methods (Solution)”

Your email address will not be published. Required fields are marked *