You are on page 1of 43

2/7/19

CIV 6745: Computational Methods in Water


Engineering

WEEK 2

Numerical discretization and integration of


Ordinary Differential Equations

OUTLINE

MORNING SESSIONS

• Framework for simulation of earth systems


• Ordinary differential equations (ODEs)
• Finite-difference numerical schemes
• Forward-Euler, backward-Euler and central differentiation
• Key properties of numerical discretization: accuracy, stability,
convergence and error-propagation
• Explicit vs. implicit time discretization and grid/time
dependency

1
2/7/19

OUTLINE

AFTERNOON SESSIONS

• Code writing
• Computer execution
• Visualization and data analysis
• Hand out of coursework #2

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Physical Model Simplifying
Observations Assumptions

Mathematical
Results
Model

Solution
Computation
Method

2
2/7/19

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Model

• The conceptual model (CM) represents our understanding of


the behavior of the systems and is based on observations
(data) of the physical processes that are believed to be
happening in the real world.

• Building a CM is a key step as it requires understanding the


physical behavior of the system and the laws of physics that
govern that.

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Model Simplifying
Assumptions

• After the CM is laid out, simplifications and assumptions are


made to create a MATHEMATICAL MODEL of the system

3
2/7/19

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Model Simplifying
Assumptions

• The MATHEMATICAL MODEL


components are:
Mathematical
o Model domain Model
o Governing Equations
o Auxiliary conditions
o Forcing terms
o System parameters

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS
COMPONENTS of MATHEMATICAL MODELS

o Model domain: defined by the boundaries of the physical


system under investigation
o Governing Equations: typically ODEs (one independent
variable, such as time t), PDEs (more independent
variables, such as position x,y,x and time t) or systems of
ODEs and/or PDEs
o Auxiliary conditions: Initial state of the system (at time
t=0), boundary conditions on system variables.
o Forcing terms: conditions of external stress
o System parameters: physical properties of the system,
which typically appear as coefficients in the governing
equations

4
2/7/19

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Model Simplifying
Assumptions

• The SOLUTION to the


MATHEMATICAL MODEL is achieved
through “integration” of ODEs/PDEs Mathematical
• Solution methods can be categorized Model
as:
o analytical
o semi-analytical
o numerical Solution
Method

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Model Simplifying
COMPUTATION Assumptions
produces
RESULTS!

Mathematical
Results
Model

Solution
Computation
Method

5
2/7/19

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Physical Model Simplifying
Observations Assumptions

IMPORTANT: MODEL VALIDATION


The model reliability must be verified by
Mathematical
Results comparing results to physical
Modelof the
observations (measurements
simulated variables). Most often
discrepancies are found. Why?
Solution
Computation
Method

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS
IMPORTANT: the model reliability must be verified by
comparing results to physical observations
(measurements of the simulated variables). Most often
discrepancies are found. Why?

• Uncertainties in
• formulation of the CM
• formulation of the mathematical model
• parameters and forcing terms
• assigning initial and boundary conditions
• Numerical errors in the solution and/or in the computation
• Uncertainty in observations (or measurement errors).

6
2/7/19

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Physical Model Simplifying
Observations Assumptions

Discrepancies between results and physical


Mathematical
Results
observations may lead to revising the CM Model
and
“improving” the simulation model to be “closer”
to the real system.

Solution
Computation
Method

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS
• In general, the mathematical model is an abstraction of reality
• Often observations are used to “calibrate” the mathematical
model, that is, tweak sources of uncertainty (parameters,
initial/boundary conditions, source/sink terms) so that the
model results look similar to the observations (data)
• Ideally the model should be used simulate (forecast) the
behavior of a system under any forcing conditions, other than
those that have been observed in reality.
• Models are often used for water management
• In this module, we will focus mainly on numerical methods
used for the solution of mathematical models simulating
surface and subsurface processes.

7
2/7/19

FRAMEWORK FOR SIMULATION


OF EARTH SYSTEMS

Conceptual
Physical Model Simplifying
Observations Assumptions

CIV6745 Mathematical
Results
Model

Solution
Computation
Method

ORDINARY DIFFERENTIAL EQUATIONS


• These equations generally contain
• one independent variable (e.g. time or space)
• one dependent variable (state of the system)
• at least one derivative of the dependent variable of 1st
order or greater:

Two examples

!" !
1) = ! !, ! ! 1st order ODE
!"

!! ! ! !! !
2)
= ! !, ! ! , 2nd order ODE
!" ! !"

8
2/7/19

ORDINARY DIFFERENTIAL EQUATIONS


• Example: completely mixed batch reactor (CMBR)
Elementary chemical reaction ! → !
In this example, a 1st order ODE is used to
represent a solute A in a closed system,
whose rate of “disappearance” is directly
proportional to its own concentration…

!! !
= −! ∙ ! !
!"
Initial condition: ! 0 = !!

! =T ! = ML!! ! = T !!

ORDINARY DIFFERENTIAL EQUATIONS


• Integration of 1st order ODEs can be carried out by separation
of variables when the ODE has the form:

!" #
=% # ∙' " # Initial condition: ! 0 = !!
!#
• Solving procedure

* + +
!" !"
= % & ∙ !& → = % - ∙ !-
# " *, # " .

9
2/7/19

ORDINARY DIFFERENTIAL EQUATIONS


• Example: completely mixed batch reactor (CMBR)

!" !
= −! ∙ ! ! ! 0 = !!
!"
• Separation of variables

* + +
!" !"
= −% ∙ !' → = −% ∙ !-
" *, " .

% '
→ ln $ %& = −+ ∙ - '. → ln $ / − ln $. = − + ∙ / →

$ / $ /
ln = −+ ∙ / → = 0 12∙' → $ / = $. ∙ 0 12∙'
$. $.

ORDINARY DIFFERENTIAL EQUATIONS


• Example: completely mixed batch reactor (CMBR)
! ! = !! ∙ ! !!∙!
• A close-form analytical solution is
obtained
• This requires a finite number of
operations to calculate exactly
• The solute concentration decreases
exponentially over time, and tends
asymptotically (at large time) to zero.
• The rate constant K indicates how
fast the decay occurs (the largest K
the fastest the disappearance of the
solute).

Play with the spreadsheet “CMBR ODE Problem.xlsx” posted on MOLE

10
2/7/19

ORDINARY DIFFERENTIAL EQUATIONS


• Example: completely mixed batch reactor (CMBR)
! ! = !! ∙ ! !!∙!

function CMBR_ex(C0,K,T_out)

t = 0:0.01:T_out;
C = C0 .*exp(-K.*t);

plot(t,C./C0)

ORDINARY DIFFERENTIAL EQUATIONS

11
2/7/19

NUMERICAL ERRORS
• Round-off errors
These are due to computers being able to represent
numbers only with a finite number of decimal digits
(a.k.a. machine precision)

• Examples (https://en.wikipedia.org/wiki/Round-off_error)

NUMERICAL ERRORS
• Round-off errors
These are due to computers being able to represent
numbers only with a finite number of decimal digits
(a.k.a. machine precision)
• Truncation errors
These are introduced with numerical methods (e.g.
numerical integration), which typically employ
mathematical approximations to represent exact
operations and quantities.
• Analytical solutions (as in the CMBR example) are usually
affected by round-off errors only.
• Numerical solutions are usually affected by both round-off
errors and truncation errors.

12
2/7/19

ACCURACY vs. PRECISION


• Accuracy
It refers to how closely computed or measured values
replicate the true value of a certain variable or parameter.
Inaccuracy, or bias, represents a systematic deviation from
the true value.

• Precision
It refers to how closely computed or measured values are
in agreement with one another. Imprecision, or noise,
refers to the magnitude of the scatter of computed or
measured values.

ACCURACY vs. PRECISION


Bias
Precision

v v
Noise

v v

Accuracy

13
2/7/19

OUR FOCI

1. truncation errors

2. accuracy of numerical solutions

3. computational cost

NUMERICAL INTEGRATION OF ODEs


• Truncation error example: consider f(x) = x2:
!"($)
Its exact derivative is = 2(. At (0 = 0.5:
!$
!"($,)
= 2×0.5 = 1
!$
!"($)
• In numerical methods, “finite difference” approximation to is
!$
used
/0((0) 0 (0 + ∆( − 0 (0 9
≅ = 056678$ (0
/( ∆(

Therefore, we can estimate [ (0 = 0.5 and taking ∆( = 0.1):


9
0.5 + 0.1 : − 0.5 :
/0((0)
056678$ (, = = 1. 1 ≠ 1 =
0.1 /(

Not exact, but rather close to…

14
2/7/19

NUMERICAL INTEGRATION OF ODEs


• Truncation error example: consider f(x) = x2:
!"($)
Its exact derivative is = 2(. At (0 = 0.5:
!$
!"($,)
!$
= 2×0.5 = 1
!"($)
• In numerical methods, “finite difference” approximation to is
!$
used
/0((0) 0 (0 + ∆( − 0 (0 9
≅ = 056678$ (0
/( ∆(

Therefore, we can estimate [ (0 = 0.5 and taking ∆( = 0.001):


9
0.5 + 0.001 : − 0.5 :
/0((0)
056678$ (, = = 1. 001 ≠ 1 =
0.01 /(

The smaller ∆( the smaller the error!

NUMERICAL INTEGRATION OF ODEs


• Truncation error example, consider f(x) = x2:
!"($)
Its exact derivative
AB(CD)is = 2(. At (0 = 0:
!$
!"($*) CD
AC GHC
!$
= 2×0 = 0 6
B EFF

!"($)
In numerical methods, finite difference approximation to is used
!$

,-(() - ( + ∆( − - ( 6
≅ = -23345$ (
,( ∆(

Therefore, we can estimate (at (0 = 0 and taking ∆( = 1/4):


:
1
6 AB(C
− 0 : )1 ,-((0)
-23345$6
-23345$((** = 4 → 1
D
=
4 ; when ∆( → 0
= 0.015625 ≠ 0 =
,(
4 AC
Not zero, but closer when ∆( is smaller

15
2/7/19

NUMERICAL INTEGRATION OF ODEs


• Let us consider the Taylor series expansion of a function f(x):
!
! ! ! !! ∆! !
! !! + ∆! = ! !! + ∙ =
!" ! !!
!!!
!" !! ! ! ! !! ∆! ! ! ! ! !! ∆! !
= ! !! + ∙ ∆! + ∙ + ∙ + ⋯
!" !" ! ! !" ! !

• From this equation, we can estimate:


!
!" !! ! !! + ∆! − ! !! ! ! ! !! ∆! !!!
= − ∙
!" ∆! !" ! !!
!!!

NUMERICAL INTEGRATION OF ODEs


• If one adopts the approximation:
!" !! ! !! + ∆! − ! !!

!" ∆!
!
! ! ! !! ∆! !!!
the term − ∙ represents the truncation
!" ! !!
!!!
error made in the calculation of the derivative of f(x) at x0 .

• Note that the truncation error is zero if f(x) is a linear function


around at x0 (its derivatives starting from the 2nd order are all
zero).
• The truncation error is thus more significant or less significant
depending on how the function departs from linearity around x0.

16
2/7/19

NUMERICAL INTEGRATION OF ODEs


• Order of approximation is the lowest order with respect to ∆!
of the terms that are neglected in the calculation of !" !!
• In this case, we deal with a 1st order approximation !"

Geometric Interpretation

• The truncation error is a function


of:
• magnitude of Dx
• degree of curvature of f(x) at x0

• To limit the truncation error, Dx will


have to be chosen “small”
• How small? It depends on the
degree of curvature of f(x) at x0.
• The choice of Dx affects accuracy
and also computational cost

NUMERICAL INTEGRATION OF ODEs


Forward-Euler Scheme

• Let us consider a 1st order approximation of the CMBR ODE:

!" ! ! ! + ∆! − ! !
−! ∙ ! ! ∗ = ≅
!" ∆!
• t* is the time at which the concentration is evaluated in the
scheme:
! ≤ ! ∗ ≤ ! + ∆!
• if t*=t, then we have the forward-Euler scheme (“forward”
because the derivative dC/dt is calculated “looking” forward in time from the
point where C is calculated)

17
2/7/19

NUMERICAL INTEGRATION OF ODEs


Forward-Euler Scheme
! ! + ∆! − ! !
−! ∙ ! ! ≅ → ! ! + ∆! = ! ! ∙ 1 − ! ∙ ∆!
∆!

• In a finite-difference (FD) approach, this equation can be


applied recursively over time to calculate the concentration C
at any time, starting from t=0, when C=C0, and progressing with
time using a prescribed time step Dt

NUMERICAL INTEGRATION OF ODEs


• How to think of it? If you know the present state of the weather (W0) and want
to estimate the weather state after 10 days (W10), a single step in time is
unlikely to be accurate (and stable for a numerical method as we will see
later).
t = t0 + 10 W(t) ? (unknown)
W10

W9
OR
W2

W1
t0 = Today W(t0) = W0 (Known)

• Here, we iterate calculations starting from t0 with a time step of ∆" = 1 (day)
until reaching the desired final time, which is t0 + 10. This takes 10 steps or
iterations.
• Doing the same, but with ∆" = 1/2 (day) will take 20 iterations to reach t0 +
10. However, this is likely to improve accuracy of the forecast.

18
2/7/19

NUMERICAL INTEGRATION OF ODEs


Procedure of the FD Forward-Euler Scheme

1. Input data: !, !!, !! , ! !!


2. Calculate time step size: ∆! ≅ !
3. Initialize time: ! = 0
4. Set ! ! = !!
5. Print out (i.e. plot): !, ! !
6. Calculate concentration at next time step:
! ! + ∆! = ! ! ∙ 1 − ! ∙ ∆!
7. Print out (plot): ! + ∆!, ! ! + ∆!
8. Update time counter: ! ← ! + ∆!
9. If ! = !! : stop
otherwise: go back to 6.

function FE_CMBR1(K,C0,T_final,n_iter)

dt = T_final/n_iter;

% Allocate vects.
t_now = zeros(1,n_iter+1);
C_now = zeros(1,n_iter+1);

% Initialise the current time to 0:


n_now = 1;
t_now(n_now) = 0;
C_now(n_now) = C0;

while t_now <= T_final


% Plot the concentration with time:
t = t_now(1:n_now);
C = C_now(1:n_now);
%
figure(1)
plot(t,C,'o:');
xlabel('t')
ylabel('C(t)')
pause(0.1)

% Calculated the concentration at next time


step:
C_now(n_now+1) = C_now(n_now) *(1-K*dt);
%
t_now(n_now+1) = t_now(n_now) + dt;
n_now = n_now +1;
end

19
2/7/19

function FE_CMBR1(K,C0,T_final,n_iter)

dt = T_final/n_iter;

% Allocate vects.
t_now = zeros(1,n_iter+1);
C_now = zeros(1,n_iter+1);

% Initialise the current time to 0:


n_now = 1;
t_now(n_now) = 0;
C_now(n_now) = C0;

while t_now <= T_final


% Plot the concentration with time:
t = t_now(1:n_now);
C = C_now(1:n_now);
%
figure(1)
plot(t,C,'or');
xlabel('t')
ylabel('C(t)')

% Calculated the concentration at next time step:


C_now(n_now+1) = C_now(n_now) *(1-K*dt);
%
t_now(n_now+1) = t_now(n_now) + dt;
n_now = n_now +1;
end

NUMERICAL INTEGRATION OF ODEs


Procedure of the Forward-Euler Scheme
Let’s look at the spreadsheet “CMBR ODE Problem.xlsx” posted on MOLE
to better understand how this sequence works:

20
2/7/19

NUMERICAL INTEGRATION OF ODEs


Procedure of the Forward-Euler Scheme
Let’s look at the spreadsheet “CMBR ODE Problem.xlsx” posted on MOLE
to better understand this sequence:

The accuracy of the scheme depends on the choice of Dt !

NUMERICAL INTEGRATION OF ODEs


Accuracy of the Forward-Euler Scheme

• The accuracy of the scheme is related to how well the “true”


analytical solution is replicated.
• A numerical scheme is said to converge if:
lim ! ! = !!!"# !
∆!→!
that is, if the solution obtained with the numerical scheme
converges to the “true” (analytical) solution when the
integration step Dx becomes very small.

21
2/7/19

NUMERICAL INTEGRATION OF ODEs


Accuracy of the Forward-Euler Scheme
• In the CMBR problem:
"# = " ( ∆') = ") ∙ ( 1 − - ∙ ∆')

". = " ( 2 ∙ ∆') = "# ∙ ( 1 − - ∙ ∆') = ") ∙ ( 1 − - ∙ ∆') .


"0 = " ( 1 ∙ ∆') = "02# ∙ ( 1 − - ∙ ∆') = ⋯ = ") ∙ ( 1 − - ∙ ∆') 0



# 0∙:∙∆7 =>:∙∆7
lim "0 = lim ") ∙ ( 1 − - ∙ ∆') 0 = ") ∙ lim 9( 1 − - ∙ ∆') :∙∆7 ; =

∆7→) ∆7→) ∆7→)

A :∙0∙∆7 A :∙0∙∆7
= ") ∙ lim ?( 1 − @) BC = ") ∙ ? lim ( 1− @) BC = ") ∙ [E 2# ]:∙0∙∆7
∆7→) ∆7→)


lim "0 = ") ∙ E 2:∙0∙∆7 = ") ∙ E 2:∙7 Exact
∆7→)

This shows the forward Euler scheme is convergent (accurate for small Dt)!

NUMERICAL INTEGRATION OF ODEs


Stability of the Forward-Euler Scheme
• Stability is met when the initial round-off error remains
limited as n becomes very large.
• In the CMBR problem:
Round-off errors: !! = !!∗ − !! !!!! = !!!!

− !!!!

Note: the “ * ” symbol denotes the numerical (truncated) solution


without round-off errors

!!!! = !!∗ ∙ 1 − ! ∙ ∆! → !!!! = !!∗ ∙ 1 − ! ∙ ∆! − !!!! = !! + !! ∙ 1 − ! ∙ ∆! − !!!!

= !! ∙ 1 − ! ∙ ∆! − !!!! + !! ∙ 1 − ! ∙ ∆! = !! ∙ 1 − ! ∙ ∆!

• This shows that the round-off error propagates with the same equation
as the forward Euler scheme
!!!
!!!! = !! ∙ 1 − ! ∙ ∆! = !! ∙ 1 − ! ∙ ∆!

22
2/7/19

NUMERICAL INTEGRATION OF ODEs


Stability of the Forward-Euler Scheme

!! = !! ∙ 1 − ! ∙ ∆! !
• According to this equation the round-off error will diverge if:
1 − ! ∙ ∆! > 1
• That is, if:
1 − ! ∙ ∆! > 1 → −! ∙ ∆! > 0 ← This can never happen!

2
1 − ! ∙ ∆! < −1 → ! ∙ ∆! > 2 → ∆! >
!
!
∆! > ← In this case, the scheme will result “unstable” as it will
! produce a “propagation” of round-off errors!

NUMERICAL INTEGRATION OF ODEs


Stability of the Forward-Euler Scheme
2
• Note that if ∆! > ! the numerical solution !! = !! ∙ 1 − ! ∙ ∆! !
results to diverge!

• NOTE: this result is not general.


There exist schemes that are
unstable and still converge,
although this is not typical.

• The solution oscillates if:


1
1 − ! ∙ ∆! < 0 → ∆! >
!

23
2/7/19

NUMERICAL INTEGRATION OF ODEs


Stability of the Forward-Euler Scheme
1
• The solution oscillates if: 1 − ! ∙ ∆! < 0 → ∆! >
!

• Oscillations are bounded if


1 2
−1 ≤ 1 − ! ∙ ∆! ≤ 0 → ≤ ∆! ≤
! !

• Oscillations are unbounded if

2
1 − ! ∙ ∆! < −1 → ∆! >
!
Check these aspects with the spreadsheet “CMBR ODE Problem.xlsx” posted
on MOLE

NUMERICAL INTEGRATION OF ODEs


Accuracy of the Forward-Euler Scheme
Assume K=2:
1
a. The solution is stable and convergent if ∆! < = 0.5
!
1 2
a. The solution is stable but oscillating if = 0.5 ≤ ∆! ≤ = 1
! !
2
a. The solution is both unstable and oscillating if ∆! > =1
!
a. b. c.

24
2/7/19

NUMERICAL INTEGRATION OF ODEs


Accuracy of the Forward-Euler Scheme
Assume K=2:
1
a. The solution is stable and convergent if ∆! < = 0.5
!
1 2
a. The solution is stable but oscillating if = 0.5 ≤ ∆! ≤ = 1
! !
2
a. The solution is both unstable and oscillating if ∆! > =1
!
a. b. c.
• The forward-Euler scheme in the case of the CMBR
problem is conditionally stable.
• This result is not general! The analysis may lead to
different results for other types of ODE.

function FE_CMBR2(K,C0,T_final,dt)

% Find the n_iter for this time step


n_iter = round(T_final/dt); % should be rounded...

% …
% SAME as FE_CMBR1
% …

% Analyse stability:
if dt > 2/K
disp('Solution oscillates and is unstable')
elseif dt > 1/K
disp('Solution oscillates but is stable')
else
disp('Solution is smooth and stable')
end

end

25
2/7/19

function FE_CMBR2(K,C0,T_final,dt)

% Find the n_iter for this time step


n_iter = round(T_final/dt); % should be rounded...

% Analyse stability:
if dt > 1/K
disp('Solution is expected to oscillate')
elseif dt > 2/K
disp('Solution is domained by round-off error')
else
disp('Solution is stable')
end
% …
% SAME as BEFORE
% …
end

Another useful version of the Forward Euler code in MATLAB

function FE_CMBR3(K,C0,T_final,dt)

% could be done without “n_iter”

% … same stability check

% Initialise the current time to 0 (scalars)


t_now = 0;
C_now = C0;

while t_now <= T_final


% Plot the concentration with time
plot(t_now,C_now,'or'); hold on (point-wise plot)
axis([0 T_final 0 C0]);
xlabel('t')
ylabel('C(t)')
pause(0.1)

% Calculated the concentration at next time step:


C_now = C_now *(1-K*dt);
% Update time
t_now = t_now + dt;
end

26
2/7/19

Another useful version of the Forward Euler code in MATLAB

function FE_CMBR3(K,C0,T_final,dt)

% could be done without “n_iter”

% … same stability check

% Initialise the current time to 0 (scalars)


t_now = 0;
C_now = C0;

while t_now <= T_final


% Plot the concentration with time
plot(t_now,C_now,'or'); hold on (point-wise plot)
axis([0 T_final 0 C0]);
xlabel('t')
ylabel('C(t)')
pause(0.1)

% Calculated the concentration at next time step:


C_now = C_now *(1-K*dt);
% Update time
t_now = t_now + dt;
end

NUMERICAL INTEGRATION OF ODEs


Backward-Euler Scheme

• Let us consider a 1st order approximation of the CMBR ODE:

!" ! ! ! + ∆! − ! !
−! ∙ ! ! ∗ = ≅
!" ∆!
• t* is the time at which the concentration is evaluated in the
scheme:
! ≤ ! ∗ ≤ ! + ∆!
• if t*=t+Dt we obtain the backward-Euler scheme (“backward”
because the derivative dC/dt is calculated looking backward from the point
where C is calculated)

27
2/7/19

NUMERICAL INTEGRATION OF ODEs


Backward-Euler Scheme
! ! + ∆! − ! ! ! !
−! ∙ ! ! + ∆! ≅ → ! ! + ∆! =
∆! 1 + ! ∙ ∆!

• This equation can be applied recursively over time to calculate


the concentration C at any time, starting from t=0, when C=C0,
and progressing with a prescribed time step Dt

NUMERICAL INTEGRATION OF ODEs


Procedure of the Backward-Euler Scheme

1. Input data: !, !!, !! , ! !!


2. Calculate time step: ∆! ≅ !
3. Initialize time: ! = 0
4. Set ! ! = !!
5. Print out (i.e. plot): !, ! !
6. Calculate concentration at next time step:
! !
! ! + ∆! =
1 + ! ∙ ∆!
7. Print out )plot): ! + ∆!, ! ! + ∆!
8. Update time counter: ! ← ! + ∆!
9. If ! = !! : stop
otherwise: go back to 6.

28
2/7/19

function BE_CMBR1(K,C0,T_final,dt)

% Initialise the current time to 0:


t_now = 0;
C_now = C0;

while t_now <= T_final


% Plot the concentration with time:
plot(t_now,C_now,'o'); hold on
axis([0 T_final 0 C0])
xlabel('t')
ylabel('C(t)')
pause(0.1)

% Calculated the concentration at next time step:


C_now = C_now /(1+K*dt);
% Update time
t_now = t_now + dt;
end

function BE_CMBR3(K,C0,T_final,dt)

% Initialise the current time to 0:


t_now = 0;
C_now = C0;

while t_now <= T_final


% Plot the concentration with time:
plot(t_now,C_now,'o'); hold on
axis([0 T_final 0 C0])
xlabel('t')
ylabel('C(t)')
pause(0.1)

% Calculated the concentration at next time step:


C_now = C_now /(1+K*dt);
% Update time
t_now = t_now + dt;
end

29
2/7/19

NUMERICAL INTEGRATION OF ODEs


Procedure of the Backward-Euler Scheme
Let’s look at the spreadsheet “CMBR ODE Problem.xlsx” posted on MOLE
to better understand this sequence:

NUMERICAL INTEGRATION OF ODEs


Procedure of the Backward-Euler Scheme
Let’s look at the spreadsheet “CMBR ODE Problem.xlsx” posted on MOLE
to better understand this sequence:

The accuracy of the scheme results still dependent on the


choice of Dt , but no oscillations are observed!

30
2/7/19

NUMERICAL INTEGRATION OF ODEs


Accuracy of the Backward-Euler Scheme

• The accuracy of the scheme is related to how well the “true”


analytical solution is replicated.
• Remember: a numerical scheme is said to converge if:
lim ! ! = !!"#$ !
∆!→!

that is, if the solution obtained with the numerical scheme
converges to the “true” (analytical) solution when the
integration step Dx is very small.

NUMERICAL INTEGRATION OF ODEs


Accuracy of the Backward-Euler Scheme
• In the CMBR problem:
!! = ! ∆! = !! 1 + ! ∙ ∆!

!! = ! 2 ∙ ∆! = !! 1 + ! ∙ ∆! = !! 1 + ! ∙ ∆! !

!! = ! ! ∙ ∆! = !!!! 1 + ! ∙ ∆! = ⋯ = !! 1 + ! ∙ ∆! !

! !∙!∙∆! !!!∙∆!
lim !! = lim !! 1 + ! ∙ ∆! ! = !! ∙ lim 1 1 + ! ∙ ∆! !∙∆! =
∆!→! ∆!→! ∆!→!

! !∙!∙∆! ! !∙!∙∆!
= !! ∙ lim!→! 1 1 + ! ! = !! ∙ lim 1 1 + ! ! = !! ∙ ! !! !∙!∙∆!

!→!


lim !! = !! ∙ ! !!∙!∙∆! = !! ∙ ! !!∙!
∆!→!

This shows that even the backward-Euler scheme is convergent!

31
2/7/19

NUMERICAL INTEGRATION OF ODEs


Stability of the Backward-Euler Scheme
• Stability is met when the initial round-off error remains
limited as n becomes very large.
• In the CMBR problem:
Round-off errors: !! = !!∗ − !! !!!! = !!!!

− !!!!

Note: the * symbol denotes the numerical (truncated) solution without


round-off errors

!!!! = !!∗ 1 + ! ∙ ∆! → !!!! = !!∗ 1 + ! ∙ ∆! − !!!! = !! + !! 1 + ! ∙ ∆! − !!!!

= !! 1 + ! ∙ ∆! + !! 1 + ! ∙ ∆! − !!!! = !! 1 + ! ∙ ∆!
• The round-off error turns out to propagate with the same
equation as the the backward Euler scheme
!!!
!!!! = !! 1 + ! ∙ ∆! = !! 1 + ! ∙ ∆!

NUMERICAL INTEGRATION OF ODEs


Stability of the Backward-Euler Scheme
!! = !! 1 + ! ∙ ∆! !

• According to this equation, the round-off error will never
diverge since
1 + ! ∙ ∆! > 1 → ! ∙ ∆! > 0 This is always true!

• In this case, the scheme will result unconditionally stable


as round-off errors cannot propagate!
• This explains why oscillations are never observed,
regardless the prescribed value of Dt!

32
2/7/19

NUMERICAL INTEGRATION OF ODEs


Stability of the Backward-Euler Scheme
!!
• Note that the numerical solution !! = never diverges
1 + ! ∙ ∆! !
• Such result is not general, but valid only within the hypotheses
of the CMBR model and its ODE.

NUMERICAL INTEGRATION OF ODEs


Weighted-Euler Scheme

• Let us consider a 1st order approximation of the CMBR ODE:

!" ! ! ! + ∆! − ! !
−! ∙ ! ! ∗ = ≅
!" ∆!
• t* is the time at which the concentration is evaluated in the
scheme:
! ≤ ! ∗ ≤ ! + ∆!
• if t<t*<t+Dt we have the weighted-Euler scheme (“weighted”
because C is calculated inside the [t,t+Dt] interval)
• This method is in fact a mix between the forward and backward
Euler schemes

33
2/7/19

NUMERICAL INTEGRATION OF ODEs


Weighted-Euler Scheme
! ! ∗ ≅ 1 − ! ∙ ! ! + ! ∙ ! ! + ∆! 0≤!≤1

! ! + ∆! − ! !
−! ∙ 1 − ! ∙ ! ! + ! ∙ ! ! + ∆! ≅ →
∆!

1 − 1 − ! ∙ ! ∙ ∆!
! ! + ∆! = ! ! ∙
1 + ! ∙ ! ∙ ∆!

• For n=0 the forward-Euler scheme is obtained
• For n=1 the backward-Euler scheme is obtained
• This equation can be applied recursively over time to calculate
the concentration C at any time, starting from t=0, when C=C0,
and progressing with a prescribed time step Dt

NUMERICAL INTEGRATION OF ODEs


Procedure of the Weighted-Euler Scheme

1. Input data: !, !!, !! , !, ! !


!
2. Calculate time step: ∆! ≅ !
3. Initialize time: ! = 0
4. Set ! ! = !!
5. Print out: !, ! !
6. Calculate concentration at next time step:
1 − 1 − ! ∙ ! ∙ ∆!
! ! + ∆! = ! ! ∙
1 + ! ∙ ! ∙ ∆!
7. Print out: ! + ∆!, ! ! + ∆!
8. Update time counter: ! ← ! + ∆!
9. If ! = !! : stop
otherwise: go back to 6.

34
2/7/19

function WE_CMBR3(K,C0,T_final,dt,nu)

% Initialise the current time to 0:


t_now = 0;
C_now = C0;

while t_now <= T_final


% Plot the concentration with time:
plot(t_now,C_now,'o'); hold on
axis([0 T_final 0 C0])
xlabel('t')
ylabel('C(t)')
pause(0.1)

% Calculated the concentration at next time step:


C_now = C_now *(1-(1-nu)*K*dt)/(1+nu*K*dt);
% Update time
t_now = t_now + dt;
end

function WE_CMBR3(K,C0,T_final,dt,nu)

% Initialise the current time to 0:


t_now = 0;
C_now = C0;

while t_now <= T_final


% Plot the concentration with time:
plot(t_now,C_now,'o'); hold on
axis([0 T_final 0 C0])
xlabel('t')
ylabel('C(t)')
pause(0.1)

% Calculated the concentration at next time step:


C_now = C_now *(1-(1-nu)*K*dt)/(1+nu*K*dt);
% Update time
t_now = t_now + dt;
end

35
2/7/19

NUMERICAL INTEGRATION OF ODEs


Procedure of the Weighted-Euler Scheme
Let’s look at the spreadsheet “CMBR ODE Problem.xlsx” posted on MOLE
to better understand this sequence:

NUMERICAL INTEGRATION OF ODEs


Procedure of the Weighted-Euler Scheme
Let’s look at the spreadsheet “CMBR ODE Problem.xlsx” posted on MOLE
to better understand this sequence:

• The accuracy of the scheme results dependent on the


choice of Dt !
• The scheme more accurate than both the forward and
backward Euler.

36
2/7/19

NUMERICAL INTEGRATION OF ODEs


Accuracy of the weighted-Euler Scheme

• The accuracy of the scheme is related to how well the “true”


analytical solution is replicated.
• A numerical scheme is said to converge if:
lim ! ! = !!!"# !
∆!→!
that is, if the solution obtained with the numerical scheme
converges to the “true” (analytical) solution when the
integration step Dx is very small.

NUMERICAL INTEGRATION OF ODEs


Accuracy of the weighted-Euler Scheme
• In the CMBR problem it can be shown that:
1 − 1 − ! ∙ ! ∙ ∆!
!! = ! ∆! = !! ∙
1 + ! ∙ ! ∙ ∆!


!
1 − 1 − ! ∙ ! ∙ ∆! 1 − 1 − ! ∙ ! ∙ ∆!
!! = ! 2 ∙ ∆! = !! ∙ = !! ∙
1 + ! ∙ ! ∙ ∆! 1 + ! ∙ ! ∙ ∆!



!
1 − 1 − ! ∙ ! ∙ ∆! 1 − 1 − ! ∙ ! ∙ ∆!
!! = ! ! ∙ ∆! = !!!! ∙ = ⋯ = !! ∙
1 + ! ∙ ! ∙ ∆! 1 + ! ∙ ! ∙ ∆!


lim !! = ⋯ = !! ∙ ! !!∙!∙∆! = !! ∙ ! !!∙!
∆!→!

Even the weighted-Euler scheme is convergent!

37
2/7/19

NUMERICAL INTEGRATION OF ODEs


Stability of the Weighted-Euler Scheme
• Stability is met when the initial round-off error remains
limited as n becomes very large.
• In the CMBR problem, the round-off error is shown to
propagate with the same equation as the the weighted-Euler
scheme
!
1 − 1 − ! ∙ ! ∙ ∆! 1 − 1 − ! ∙ ! ∙ ∆!
!! = !!!! ∙ = !! ∙
1 + ! ∙ ! ∙ ∆! 1 + ! ∙ ! ∙ ∆!

NUMERICAL INTEGRATION OF ODEs


Stability of the Weighted-Euler Scheme
• According to the previous equation the round-off error will
remain finite if:
1 − 1 − ! ∙ ! ∙ ∆!
≤ 1
1 + ! ∙ ! ∙ ∆!
• One can show that this condition is met if
0.5 ≤ ! ≤ 1
or 2
! ∙ ∆! ≤ !"# 0 ≤ ! < 0.5
1−2∙!

38
2/7/19

NUMERICAL INTEGRATION OF ODEs


Stability of the weighted-Euler Scheme
• The solution oscillates if:
!! !!! ∙!∙∆! !
≤ 0 → ! ∙ ∆! >
!!!∙!∙∆! !!!
• Oscillations are bounded if:
! !
< ! ∙ ∆! ≤ !"# 0 ≤ ! < 0.5
!!! !!!∙!
!
< ! ∙ ∆! !"# 0.5 ≤ ! ≤ 1
!!!

• Oscillations are unbounded if:


!
! ∙ ∆! > and 0 ≤ ! < 0.5
!!!∙!

function WE_CMBR4(K,C0,T_final,dt,nu)

if (nu >= 0) && (nu < 0.5) % Stability analysis


%
if (K*dt > 1/(1-nu)) && (K*dt <= 2/(1-2*nu))
disp('The method is unstable');
else
disp('The method is stable');
end
%
elseif (nu >= 0.5) && (nu <= 1)
%
if K*dt > 1/(1-nu)
disp('The method is unstable');
else
disp('The method is stable');
end
%
else
disp('Choose "nu" value between 0 and 1');
end

% the rest remains unchanged!

39
2/7/19

NUMERICAL INTEGRATION OF ODEs


Accuracy of the weighted-Euler Scheme
Assume K=2.5 and n=0.4:
1
a. Stability and convergence if ∆! <
!∙ 1−!
= 0.67

1 2
a. Stability but oscillation if !∙ 1−!
= 0.67 ≤ ∆! ≤
!∙ 1−2∙!
=4

2
a. Instability and oscillation if ∆! > =4
!∙ 1−2∙!
a. b. c.

NUMERICAL INTEGRATION OF ODEs


Accuracy of the weighted-Euler Scheme
Assume K=2.5 and n=0.4:
1
a. Stability and convergence if ∆! <
!∙ 1−!
= 0.67

1 2
a. Stability but oscillation if !∙ 1−!
= 0.67 ≤ ∆! ≤
!∙ 1−2∙!
=4

2
a. Instability and oscillation if ∆! > =4
!∙ 1−2∙!
a. b. c.
• The Weighted-Euler scheme in the case of the CMBR
problem is conditionally stable.
• This result is not general! The analysis may lead to
different results for other types of ODE.

40
2/7/19

SYSTEMS OF ODEs
• These systems are formed by multiple ODEs and contain
• one independent variable
• more dependent variables
• at least one derivative for each dependent variable of first
order or greater:
!!! !
= !! !; !! ! , … , !! ! !! 0 = !!,!
!"
⋯=⋯ ⋯=⋯
!!! !
= !! !; !! ! , … , !! ! !! 0 = !!,!
!"
ODE’s Initial Conditions

• The application of a finite-difference approximation to each


ODE produces a system of n linear equations on the n
unknowns: !! ! + ∆! , … , !! ! + ∆!

SYSTEMS OF ODEs
• This will look like:
!!,! ⋯ !!,! !! ! + ∆! !!
⋯ ⋯ ⋯ ∙ ⋮ = ⋮
!!,! ⋯ !!,! !! ! + ∆! !!

41
2/7/19

NUMERICAL INTEGRATION OF ODEs


References

1. Press W.H., S.A. Teukolsky, W.T. Vetterling, B.P. Flannery


(2007). Numerical Recipes: The Art of Scientific
Computing, Third Edition , Cambridge University Press,
ISBN-10: 0521880688.
2. Rushton, K.R. (2003). Groundwater Hydrology:
Conceptual and Computational Models. John Wiley and
Sons Ltd., ISBN-13: 9780470850046.
3. Anderson, M.P., and W.W. Woessner (1992) Applied
Groundwater Modeling: Simulation of Flow and Advective
Transport, Academic Press, ISBN-13: 9780120594856.

Tutorials

1. Consider the CMBR problem with the following parameters:


K = 2.0000, C0 = 1.000, Tf = 10.00, and n = 100 (or dt = Tf/n)

2. Develop a MATLAB code that can save and plot the


analytical solution.

3. Construct an Forward-Euler finite-difference method to solve


the CMBR problem.

4. Construct a Backward-Euler finite-difference method to solve


the CMBR problem.

5. Plot on the same figure the results produced in 1. – 4.


(Hint: refer to the lectures)

42
2/7/19

Coursework
Consider the ordinary differential equation (ODE):
!"($)
= ' 1 − "($)
!$
Where ' = 0.116 yr -1 and the initial condition "- = " 0 = 5

1. Derive the analytical solution for the given ODE. Produce a MATLAB code
that can save and plot the analytical solution for a selected Tf that is relatively
big (e.g. Tf = 100 yrs).
2. Construct a well-commented MATLAB code that uses the forward Euler
method for solving the given ODE. Use time steps ∆$ of 3, 10 and 20 years.
Plot the profiles of " vs. $ for each time step and for the analytical solution.
Discuss the effect of the time step size on the accuracy of your results.
3. Repeat the work in 2., but using a backward Euler method to solve the
ODE. Discuss the effect of the time step size on the accuracy of your results.

Reporting: Write up a short memo with numbered answers to all questions. Use
Equations and Figures as necessary and provide a copy of your code(s) as an
appendix. There is no particular format to follow, just make sure your answers are
clear and well presented.

! " + ∆" − ! "


= ' 1 − !(")
∆"
! " + ∆" = ! " + ' 1 − !(") ∆"

! " + ∆" − ! "


= ' 1 − !(" + ∆")
∆"
! " + ∆" − ! "
= ' − ' !(" + ∆")
∆"
! " + ∆" '
! " + ∆" =
1 + '∆"

43

You might also like