Practical
Practical
Objectives
The goal of this practical is to develop our first code to solve Ordinary Differential Equations
(ODEs), using Euler’s method. By doing this, we will also remind ourselves with the basic
syntax of Matlab, and the use of for loops.
Here we will solve a simple first order ODE, for which we already the exact analytical solution,
so that the approximate numerical solutions can be compared to the exact one.
In Exercise 1 a large number of small steps is given so you can see the process and see where
to start.
• your code will not run the first time (never does!), it is normal: go back and check what
the bug is !
• the computer is not ”wrong”: it does just what you tell it to do;
• use Matlab’s Help (type doc) when in doubt about syntax or how to use a specific function;
• write your code in a file instead of the command window, so you can remember what you
did;
1
Exercise 1: A simple landslide problem
Let us consider a very (very!) crude model of landslide, by looking at the motion of the center
of mass along a slope, driven by gravity. This physical model is expressed mathematically
by a governing equation relating the speed v of the mass as a function of time t, and other
parameters1 :
dv
− + g sin(θ) − ηv(t) = 0, (1)
dt
where g is the acceleration of gravity, θ is the slope angle, and η is a ”viscosity” coefficient which
characterises the friction at the base of the moving mass. To complete the system, we need an
initial condition:
v(0) = 0. (2)
Although solving this equation exactly is feasible using basic calculus, we can also solve it
approximately using Euler’s method.
Main goal: Solve Equation 1 numerically for t between 0 and 120 seconds, using: g =
9.8 m s−2 , θ = π/6, and η = 0.1 s−1 .
Questions:
(2) Using Euler’s method, write a discretised version of Equation 1, relating the speed at time
tn+1 to the speed at time tn .
(3) In Matlab, write a function f (in a separate file called f.m) returning the value of dv/dt
as a function of t, v, and other parameters.
(4) In Matlab, start a script (in a separate file called main.m), in which you assign values to
the parameters. We will choose g = 9.8 m s−2 , θ = π/6, and η = 0.1 s−1 .
(5) In your main.m, assign a value for your time step (let us choose Dt=1 to start with) and
generate the time vector ranging from t = 0 to t = 120 s.
(6) In your main.m, write a for loop to compute v(tn+1 ) based on your numerical scheme (see
Question 2 and 3; make use of your f !)
Plot both your numerical solution and the analytical solution in the same plot. Compare.
(8) Compute and plot the absolute and the relative error at each time. How does this change
when changing the time step (try increasing/decreasng by a factor of 10) ?
(9) Generate a log-log plot showing the relative error (taken at a given time t = 5 s, for instance)
as a function of the time step. Discuss.
1
In this course it does not really matter where this equation comes from, but you can figure it out as a
homework problem...
2
Exercise 2: More of the same
The idea here is to solve the same problem as in Exercise 1, but using the modified Euler
(also called midpoint) method. This re-uses a lot of the same code as before !
(1) Write the discretised version of Equation 1 using the midpoint method.
(2) Copy and paste your existing main.m in a new file, and modify the update scheme (what
is inside the for loop) to use the midpoint approximation.
(3) For a given value of the step size, compare the accuracy both numerical methods.
(4) In a synoptic plot, show how the relative error changes with the step size for both methods.
Discuss.