Professional Documents
Culture Documents
CIV 6745: Computational Methods in Water Engineering Week 2
CIV 6745: Computational Methods in Water Engineering Week 2
WEEK 2
OUTLINE
MORNING SESSIONS
1
2/7/19
OUTLINE
AFTERNOON SESSIONS
• Code writing
• Computer execution
• Visualization and data analysis
• Hand out of coursework #2
Conceptual
Physical Model Simplifying
Observations Assumptions
Mathematical
Results
Model
Solution
Computation
Method
2
2/7/19
Conceptual
Model
Conceptual
Model Simplifying
Assumptions
3
2/7/19
Conceptual
Model Simplifying
Assumptions
4
2/7/19
Conceptual
Model Simplifying
Assumptions
Conceptual
Model Simplifying
COMPUTATION Assumptions
produces
RESULTS!
Mathematical
Results
Model
Solution
Computation
Method
5
2/7/19
Conceptual
Physical Model Simplifying
Observations Assumptions
• 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
Conceptual
Physical Model Simplifying
Observations Assumptions
Solution
Computation
Method
7
2/7/19
Conceptual
Physical Model Simplifying
Observations Assumptions
CIV6745 Mathematical
Results
Model
Solution
Computation
Method
Two examples
!" !
1) = ! !, ! ! 1st order ODE
!"
!! ! ! !! !
2)
= ! !, ! ! , 2nd order ODE
!" ! !"
8
2/7/19
!! !
= −! ∙ ! !
!"
Initial condition: ! 0 = !!
! =T ! = ML!! ! = T !!
!" #
=% # ∙' " # Initial condition: ! 0 = !!
!#
• Solving procedure
* + +
!" !"
= % & ∙ !& → = % - ∙ !-
# " *, # " .
9
2/7/19
!" !
= −! ∙ ! ! ! 0 = !!
!"
• Separation of variables
* + +
!" !"
= −% ∙ !' → = −% ∙ !-
" *, " .
% '
→ ln $ %& = −+ ∙ - '. → ln $ / − ln $. = − + ∙ / →
$ / $ /
ln = −+ ∙ / → = 0 12∙' → $ / = $. ∙ 0 12∙'
$. $.
10
2/7/19
function CMBR_ex(C0,K,T_out)
t = 0:0.01:T_out;
C = C0 .*exp(-K.*t);
plot(t,C./C0)
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
• 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.
v v
Noise
v v
Accuracy
13
2/7/19
OUR FOCI
1. truncation errors
3. computational cost
14
2/7/19
!"($)
In numerical methods, finite difference approximation to is used
!$
,-(() - ( + ∆( − - ( 6
≅ = -23345$ (
,( ∆(
15
2/7/19
16
2/7/19
Geometric Interpretation
!" ! ! ! + ∆! − ! !
−! ∙ ! ! ∗ = ≅
!" ∆!
• 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
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
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);
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);
20
2/7/19
21
2/7/19
= !! ∙ 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
!! = !! ∙ 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!
23
2/7/19
2
1 − ! ∙ ∆! < −1 → ∆! >
!
Check these aspects with the spreadsheet “CMBR ODE Problem.xlsx” posted
on MOLE
24
2/7/19
function FE_CMBR2(K,C0,T_final,dt)
% …
% 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)
% 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
function FE_CMBR3(K,C0,T_final,dt)
26
2/7/19
function FE_CMBR3(K,C0,T_final,dt)
!" ! ! ! + ∆! − ! !
−! ∙ ! ! ∗ = ≅
!" ∆!
• 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
28
2/7/19
function BE_CMBR1(K,C0,T_final,dt)
function BE_CMBR3(K,C0,T_final,dt)
29
2/7/19
30
2/7/19
!! = ! 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
= !! 1 + ! ∙ ∆! + !! 1 + ! ∙ ∆! − !!!! = !! 1 + ! ∙ ∆!
• The round-off error turns out to propagate with the same
equation as the the backward Euler scheme
!!!
!!!! = !! 1 + ! ∙ ∆! = !! 1 + ! ∙ ∆!
32
2/7/19
!" ! ! ! + ∆! − ! !
−! ∙ ! ! ∗ = ≅
!" ∆!
• 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
34
2/7/19
function WE_CMBR3(K,C0,T_final,dt,nu)
function WE_CMBR3(K,C0,T_final,dt,nu)
35
2/7/19
36
2/7/19
…
!
1 − 1 − ! ∙ ! ∙ ∆! 1 − 1 − ! ∙ ! ∙ ∆!
!! = ! ! ∙ ∆! = !!!! ∙ = ⋯ = !! ∙
1 + ! ∙ ! ∙ ∆! 1 + ! ∙ ! ∙ ∆!
lim !! = ⋯ = !! ∙ ! !!∙!∙∆! = !! ∙ ! !!∙!
∆!→!
Even the weighted-Euler scheme is convergent!
37
2/7/19
38
2/7/19
function WE_CMBR4(K,C0,T_final,dt,nu)
39
2/7/19
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.
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
SYSTEMS OF ODEs
• This will look like:
!!,! ⋯ !!,! !! ! + ∆! !!
⋯ ⋯ ⋯ ∙ ⋮ = ⋮
!!,! ⋯ !!,! !! ! + ∆! !!
41
2/7/19
Tutorials
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.
43