APSC 256
Numerical Methods for Analysis
Roundoff and Truncation Errors
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 1
Accuracy and Precision
• Commonly misunderstood terms by the layperson
• What does the Oxford dictionary say?
• Accuracy?
• the degree to which the result of a measurement, calculation, or specification
conforms to the correct value or a standard.
• Precision?
• refinement in a measurement, calculation, or specification, especially
as represented by the number of digits given.
Not bad!
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 2
Accuracy and Precision
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 3
Error Measures
• True Error
𝐸𝑡 =
• How useful is this? Discuss?
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 4
Error Measures
• True relative error
𝜖𝑡 =
• Approximate relative error
𝜖𝑎 =
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 5
Error Measures
• Stopping criterion 𝜖𝑠
• Stop when:
𝜖𝑎 ≤ 𝜖𝑠
• It can be shown that the stopping criterion, 𝜖𝑠, is related to the
number of base-10 significant figures, 𝑛, through the following
relationship:
𝜖𝑎 = 𝜖𝑠 𝜖𝑠 = 0.5 × 102−𝑛 %
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 6
Error Measures: Example
• Consider the following series approximation to 𝑒𝑥
• We can write some simple MATLAB code to evaluate this series for
any number of terms, and then calculate 𝜖𝑡 and 𝜖𝑎
• But how do we know the true value of 𝑒𝑥 which would need to
calculate 𝜖𝑡?
• Discuss
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 7
Error Measures: Example
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 8
% Approximate exp(x) using a series
% MATLAB factorial function is called 'factorial'
n = 3; % Number of significant figures at which to stop
es = (0.5 * 10 ^ (2-n)); % This is a percentage
x = 0.5;
sum = 0.0;
true = exp(x); % We assume MATLAB calculates exp(x) very accurately
fprintf("Terms Results etrue%% eapprox%%n");
for k = 0:10 % Loop no more than 11 times
sum = sum + 1/factorial(k)*x^k;
et = (true - sum)/true*100.0;
if k == 0 % Cannot calculate approximate error without
% an previous result, so skip when k = 0
fprintf(" %d %.15f %6.3f N/An",k+1,sum,et)
else
ea = (sum - sum_save)/sum*100.0; % Previous approximation is sum_save
fprintf(" %d %.15f %7.4f %7.4fn",k+1,sum,et,ea)
if (abs(ea) <= es)
break; % Exit the loop, we have reached es
end
end
sum_save = sum;
end
Terms Results etrue% eapprox%
1 1.000000000000000 39.347 N/A
2 1.500000000000000 9.0204 33.3333
3 1.625000000000000 1.4388 7.6923
4 1.645833333333333 0.1752 1.2658
5 1.648437500000000 0.0172 0.1580
6 1.648697916666667 0.0014 0.0158
Sources of Error in Numerical Methods
• Two of the most important sources of error to understand in
numerical methods are roundoff error and truncation error
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 9
Roundoff Errors
• A simple base 10 representation (with normalization)
• Overflow errors
• Underflow errors
• Roundoff errors
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 10
Roundoff Errors: A simple base-10
representation
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 11
Overflow Errors
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 12
Underflow errors
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 13
Roundoff Errors
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 14
𝑠𝑚𝑑𝑚0. 𝑑𝑚1 × 10𝑠𝑒𝑑𝑒0
Consider
0.1 1.0
+1.0 × 10−1
+1.0 × 10±0
0.11
+1.1 × 10−1
0.99
+9.9 × 10−1
Resolution
Roundoff error (worst case)
Roundoff Errors
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 15
1.0 10.0
+1.0 × 10±0
+1.0 × 10+1
1.1
+1.1 × 10±0
9.9
+9.9 × 10±0
Resolution
Roundoff error (worse case)
10 100
+1.0 × 10+1
+1.0 × 10+2
11
+1.1 × 10+1
99
+9.9 × 10+1
Resolution
Roundoff error (worse case)
Roundoff Errors
Range Resolution Roundoff Error
0.1 - 1.0
1.0 - 10.0
10.0 - 100.0
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 16
• Observations?
Roundoff Errors
• What about the true relative roundoff error that we see?
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 17
0.1 0.11
0.105
𝜖𝑡 =
0.104999999 − 0.1
0.104999999
× 100 ≅ 4.8%
𝜖𝑡 =
0.105 − 0.11
0.105
× 100 ≅ 4.8%
Round down
Round up
0.99 1.0
0.995
𝜖𝑡 =
0.994999999 − 0.99
0.994999999
× 100 ≅ 0.5%
𝜖𝑡 =
0.995 − 1.0
0.995
× 100 ≅ 0.5%
Round down
Round up
Roundoff Errors
• We can do similar calculations for other ranges
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 18
Range Interval 𝝐𝒕
0.1 - 1.0
0.1 – 0.11 ≅ 5 %
0.99 – 1.0 ≅ 0.5 %
1.0 - 10.0
1.0 – 1.1 ≅ 5 %
9.9 - 10 ≅ 0.5 %
10.0 - 100.0
10.0 - 11 ≅ 5 %
99 - 100 ≅ 0.5 %
Machine precision
Maximum true relative error
This is a very useful metric to know
when doing complex numerical
calculations
Note that the exponent digits have a
significant impact on the largest and
smallest values but no effect on the
machine precision
Round Errors
• Example
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 19
𝑠𝑚𝑑𝑚0. 𝑑𝑚1 × 10𝑠𝑒𝑑𝑒0𝑑𝑒1
Roundoff Errors
• ….are real, even in numerical systems which high precision
APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 20
>> a = 1 - 3*(4/3 - 1)
a =
2.220446049250313e-16
a = 0.0;
for i = 1:10
a = a + 0.1;
end
a == 1
ans =
logical
0
• Roundoff errors cannot be ignored, and they are very real, but with care
they can be managed
Truncation Errors
Part I
Dr. Nicholas Swart
UBCO School of Engineering
Copyright 2021, Prof. Swart, UBCO Engineering Do not copy
without permission
21
Truncation Errors
• Truncation errors are different from round-off errors
• Round-off errors arise because of finite precision in computer storage of
floating-point numbers
• Truncation errors on the other hand arise from the mathematical
approximations employed in the numerical method
• The term “truncation” implies that we have truncated or pruned in some
fashion a mathematical calculation which has then led to an approximation
to that true answer
• One of the most common reasons we employ mathematical
approximations is to make numerical methods more efficient
• In some cases, it is not even possible to work with a non-truncated
mathematical method due to roundoff error
• We run out of “numerical steam” and the solution gets swamped by roundoff error
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
22
Truncation Errors: Example
• We illustrate the concept of truncation error by example
• Consider the very beautiful Taylor series, which is a polynomial approximation of
a function 𝑓(𝑥) about 𝑥 = 𝑎 ,
𝑓(𝑥) = 𝑓(𝑎) +
𝑓′(𝑎)
1!
(𝑥 − 𝑎) +
𝑓′′(𝑎)
2!
(𝑥 − 𝑎)2
+
𝑓′′′(𝑎)
3!
(𝑥 − 𝑎)3
+. . . = ෍
𝑛=0
∞
𝑓(𝑛)
(𝑎)
𝑛!
(𝑥 − 𝑎)𝑛
• The MacLaurin series is the Taylor series when 𝑎 = 0 ,
𝑓(𝑥) = 𝑓(0) +
𝑓′(0)
1!
𝑥 +
𝑓′′(0)
2!
𝑥2
+
𝑓′′′(0)
3!
𝑥3
+. . . = ෍
𝑛=0
∞
𝑓(𝑛)
(0)
𝑛!
𝑥𝑛
• In general, unless 𝑓 is a polynomial, an infinite number of terms is required in the
Taylor series in order to exactly calculate 𝑓(𝑥)
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
23
Truncation Errors: Example
• The Taylor series is extremely useful in numerical methods, and often
we use a slightly different notation for the series,
𝑓(𝑥𝑖+1) = 𝑓(𝑥𝑖) +
𝑓′(𝑥𝑖)
1!
ℎ +
𝑓′′(𝑥𝑖)
2!
ℎ2
+
𝑓′′′(𝑥𝑖)
3!
ℎ3
+. . . = ෍
𝑛=0
∞
𝑓(𝑛)
(𝑥𝑖)
𝑛!
ℎ𝑛
where ℎ = 𝑥𝑖+1 − 𝑥𝑖 . We often refer to ℎ as the “step size”, and by default ℎ is
always assumed to be specified as a positive value
• The usefulness of this notation will be come apparent soon, but
remember that the above form of the Taylor series is no different
than the more common form seen in Calculus textbooks
• The form shown here lends itself slightly better to numerical methods
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
24
Truncation Errors: Example
• Let’s put the Taylor series to the side for the moment and ask
ourselves how we might numerically approximate the derivate to a
function 𝑓(𝑥) at some point 𝑥 = 𝑥𝑖.
• Derivatives are very commonly required in engineering problems
• Even though it is often possible to find an analytical solution for 𝑓′
𝑥
there are other reasons why we need to take numerical derivatives
which we will see later in the course
• Let’s look at three simple approaches
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
25
Truncation Errors: Example
• Intuitively, these approaches seem like they might work, but were not certain
• Intuitively, these approaches seem like they might be more accurate as ℎ → 0
• Intuitively, the approach on the right seems like it might be more accurate
Intuition alone is insufficient for engineering analysis. How do we convert intuition to
mathematical rigour?
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
26
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
ℎ
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1
ℎ 𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1
2ℎ
Truncation Errors: Example
• Let’s return to the Taylor series ,
𝑓(𝑥𝑖+1) = 𝑓(𝑥𝑖) +
𝑓′(𝑥𝑖)
1!
ℎ +
𝑓′′(𝑥𝑖)
2!
ℎ2
+
𝑓′′′(𝑥𝑖)
3!
ℎ3
+. . . = ෍
𝑛=0
∞
𝑓(𝑛)
(𝑥𝑖)
𝑛!
ℎ𝑛
• It would be obviously impractical (compute time and also roundoff error)
to use an infinite number of terms in the Taylor series when calculating
𝑓 𝑥𝑖+1 so we truncate the series, which results in an approximation of
𝑓 𝑥𝑖+1 .
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
27
Truncation Errors: Example
• Zeroth – order approximation,
𝑓(𝑥𝑖+1) ≈ 𝑓(𝑥𝑖)
• First-order approximation
𝑓(𝑥𝑖+1) ≈ 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖)ℎ
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
28
• Second-order approximation
𝑓(𝑥𝑖+1) ≈ 𝑓(𝑥𝑖) +
𝑓′(𝑥𝑖)
1!
ℎ +
𝑓′′(𝑥𝑖)
2!
ℎ2
• Third-order approximation
𝑓(𝑥𝑖+1) ≈ 𝑓(𝑥𝑖) +
𝑓′(𝑥𝑖)
1!
ℎ +
𝑓′′(𝑥𝑖)
2!
ℎ2 +
𝑓′′′(𝑥𝑖)
3!
ℎ3
• In each of these approximation we have thrown away higher-order terms
in the Taylor series, which introduces error. But how much error?
Truncation Errors: Example
• The terms that are truncated in the Taylor series can be grouped together into a single term called
𝑅𝑛,
𝑓 𝑥𝑖+1 = 𝑓(𝑥𝑖) +
𝑓′(𝑥𝑖)
1!
ℎ +
𝑓′′(𝑥𝑖)
2!
ℎ2
+
𝑓′′′(𝑥𝑖)
3!
ℎ3
+. . .
𝑓(𝑛)
(𝑥𝑖)
𝑛!
ℎ𝑛
+ 𝑅𝑛
• Through some careful mathematical analysis, it can be shown that,
𝑅𝑛 =
𝑓(𝑛+1)(𝜉)
(𝑛 + 1)!
ℎ𝑛+1
where 𝜉 lies between 𝑥𝑖 and 𝑥𝑖+1.
• This is known as the Lagrange form of the remainder of the Taylor series
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
29
Truncation Errors: Example
• Let’s write the series with the remainder term explicitly,
𝑓 𝑥𝑖+1 = 𝑓(𝑥𝑖) +
𝑓′(𝑥𝑖)
1!
ℎ +
𝑓′′(𝑥𝑖)
2!
ℎ2
+
𝑓′′′(𝑥𝑖)
3!
ℎ3
+. . .
𝑓(𝑛)
(𝑥𝑖)
𝑛!
ℎ𝑛
+
𝑓(𝑛+1)
(𝜉)
(𝑛 + 1)!
ℎ𝑛+1
• This is a very beautiful expression
• It shows us how to find an n’th order polynomial approximation of 𝑓(𝑥) and tell us
what the remainder (error) will be
• Keep in mind, we don’t know the exact value of 𝜉. If we did, we would
know the exact value of the remainder. But very importantly, we do know
that 𝜉 lies between 𝑥𝑖 and 𝑥𝑖+1.
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
30
Truncation Errors: Example
• Let’s now look at a 1st order approximation to 𝑓 𝑥𝑖+1 , that is to say
the approximation when 𝑛 = 1 ,
𝑓 𝑥𝑖+1 = 𝑓(𝑥𝑖) +
𝑓′(𝑥𝑖)
1!
ℎ +
𝑓(2)(𝜉)
2!
ℎ2
, for 𝜉 between 𝑥𝑖 and 𝑥𝑖+1
• Then solve this equation for 𝑓′(𝑥𝑖) ,
𝑓′ 𝑥𝑖 =
𝑓 𝑥𝑖+1 −𝑓 𝑥𝑖
ℎ
−
𝑓(2)(𝜉)
2!
ℎ
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
31
Truncation Errors: Example
• Copying the previous equation.
𝑓′ 𝑥𝑖 =
𝑓 𝑥𝑖+1 −𝑓 𝑥𝑖
ℎ
−
𝑓(2)(𝜉)
2!
ℎ , for 𝜉 between 𝑥𝑖 and 𝑥𝑖+1
• Let’s truncate the remainder term,
𝑓′
𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
ℎ
• And here we now see the original derivative method we proposed earlier
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
32
Truncation Errors: Example
• What about the error term we truncated (threw away) that led to the approximation? What can we say
about it?
−
𝑓(2)(𝜉)
2!
ℎ , for 𝜉 between 𝑥𝑖 and 𝑥𝑖+1
• We don’t know the exact value of 𝜉, but for any given 𝑥𝑖 and 𝑥𝑖+1 it is a constant. We write the error term as
follows,
𝛽ℎ
• Recall that ℎ = 𝑥𝑖+1 − 𝑥𝑖, which we referred to as the step size. For a given 𝛽 we know the following,
ℎ ↑ ⇒ 𝛽ℎ ↑ by the same proportion
ℎ ↓ ⇒ 𝛽ℎ ↓ by the same proportion
• In other words, if we double ℎ then 𝛽ℎ doubles, and if we decrease ℎ by half then 𝛽ℎ decreases by half
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
33
Truncation Errors: Example
• Continuing ….
• In other words, even though we don’t know the exact value of 𝛽, and hence don’t know
the exact error (𝑅_𝑛), we know how 𝛽ℎ behaves as a function of ℎ.
• There is an important mathematical notation that is used to describe how a function
behaves for a given input variable. It is called “big-Oh” notation, and in this case, we say
that 𝑅𝑛 is big-Oh ℎ which we write as,
𝑅𝑛 is 𝑂(ℎ)
• We say 𝑅𝑛 is “Order ℎ”
• NOTE: Big-Oh has a mathematically formal definition, and is used heavily in computer
science, but we do not need to concern ourselves with those details for the purposes of
this course.
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
34
Truncation Errors: Example
• In summary we have an approximation to the derivative,
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
ℎ
• We say that for this approximation, the error is 𝑂 ℎ , which means
that as we decrease ℎ in order to improve the accuracy of the result,
the error will drop by approximately the same factor that we
decreased ℎ by.
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
35
Truncation Errors: Example
• Using a very similar analysis we can derive the second derivative
formula that we saw at the start of this section,
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1
ℎ
• Be careful, we always want ℎ to be positive, and here ℎ = 𝑥𝑖 − 𝑥𝑖−1.
• If you proceed through the mathematical analysis (I recommend that
you do so) you will find that this formula also has an error of 𝑂 ℎ .
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
36
Truncation Errors: Example
• Let’s turn to the final integration formula we saw earlier,
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1
2ℎ
• We can derive this formula by using the following two 2nd order Taylor series,
𝑓 𝑥𝑖+1 = 𝑓 𝑥𝑖 +
𝑓′ 𝑥𝑖
1!
ℎ +
𝑓′′ 𝑥𝑖
2!
ℎ2 +
𝑓′′′ 𝜉1
3!
ℎ3 , for 𝜉1 between 𝑥𝑖 and 𝑥𝑖+1
𝑓 𝑥𝑖−1 = 𝑓 𝑥𝑖 −
𝑓′ 𝑥𝑖
1!
ℎ +
𝑓′′ 𝑥𝑖
2!
ℎ2
−
𝑓′′′ 𝜉2
3!
ℎ3
, for 𝜉2 between 𝑥𝑖−1 and 𝑥𝑖
• Note we assume ℎ = 𝑥𝑖+1 − 𝑥𝑖 = 𝑥𝑖 − 𝑥𝑖−1
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
37
Truncation Errors: Example
• Now subtract as follows:
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 = 2
𝑓′ 𝑥𝑖
1!
ℎ +
𝑓′′′ 𝜉1
3!
ℎ3 +
𝑓′′′ 𝜉2
3!
ℎ3
• Solving for 𝑓′ 𝑥𝑖 gives,
𝑓′ 𝑥𝑖 =
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1
2ℎ
−
1
2 ⋅ 3!
𝑓′′′ 𝜉1 + 𝑓′′′ 𝜉2 ℎ2
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
38
Truncation Errors: Example
• In this case we can treat the error similarly to how we did so before. We don’t
know the exact values of 𝜉1 and 𝜉2, but for any given 𝑥𝑖 and 𝑥𝑖+1, 𝜉1 is a constant,
and similarly for any given 𝑥𝑖−1 and 𝑥𝑖, 𝜉2 is a constant. We write the error term
as follows,
𝛽ℎ2
• We now have another derivative formula and the associated big-Oh for the error,
𝑓′
𝑥𝑖 =
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1
2ℎ
• Where the error is 𝑂 ℎ2
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
39
Truncation Errors: Summary
Error 𝑂(ℎ)
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
40
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
ℎ
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1
ℎ
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1
2ℎ
Error 𝑂(ℎ) Error 𝑂(ℎ2)
• The error in the first two methods decreases as 𝑂(ℎ), whereas the error in the third method decreases as 𝑂 ℎ2
• The 2nd method is therefore better as ℎ is decreased
• What is especially attractive is that we have improved the method while still only requiring 2 function evaluations.
Truncation Errors
Part II
Dr. Nicholas Swart
UBCO School of Engineering
Copyright 2021, Prof. Swart, UBCO Engineering Do not copy
without permission
41
Truncation Errors Example: Recap from Part II
Error 𝑂(ℎ)
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
42
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
ℎ
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1
ℎ
𝑓′ 𝑥𝑖 ≈
𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1
2ℎ
Error 𝑂(ℎ) Error 𝑂(ℎ2)
• The error in the first two methods decreases as 𝑂(ℎ), whereas the error in the third method decreases as 𝑂 ℎ2
• The 2nd method therefore performs substantially better as ℎ is decreased
• What is especially attractive is that we have improved the performance while still only requiring 2 function evaluations.
Forward Difference Backward Difference Centered Difference
Application of Derivative Formulae
• Consider the following function,
𝑓 𝑥 = 3𝑥3
− 2𝑥2
+ 7𝑥 − 10
• We can easily find the analytical derivative of this function,
𝑓′ 𝑥 = 9𝑥2 − 4𝑥 + 7
• Let’s examine how the three different derivative formulae perform
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
43
Application of Derivative Formulae
• Estimate 𝑓′(2) using the
three derivative formulae
for ℎ from 10 to 0.01 in
steps of 0.01
• Exact: 𝑓′ 2 = 35
• Calculate the % absolute
true relative error for each
method
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
44
Application of Derivative Formulae
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
45
Exact h FD BD CD FD_et BD_et CD_et
35.000 10.00 35.160 175.000 335.000 1314.2857% 400.0000% 857.1429%
35.000 9.99 35.160 174.560 334.400 1312.1151% 398.7437% 855.4294%
35.000 9.98 35.160 174.121 333.801 1309.9463% 397.4891% 853.7177%
35.000 9.97 35.160 173.683 333.203 1307.7791% 396.2363% 852.0077%
35.000 9.96 35.160 173.245 332.605 1305.6137% 394.9851% 850.2994%
35.000 9.95 35.160 172.807 332.007 1303.4500% 393.7357% 848.5929%
35.000 9.94 35.160 172.371 331.411 1301.2880% 392.4880% 846.8880%
35.000 9.93 35.160 171.935 330.815 1299.1277% 391.2420% 845.1849%
35.000 9.92 35.160 171.499 330.219 1296.9691% 389.9977% 843.4834%
35.000 9.91 35.160 171.064 329.624 1294.8123% 388.7551% 841.7837%
35.000 9.90 35.160 170.630 329.030 1292.6571% 387.5143% 840.0857%
35.000 9.89 35.160 170.196 328.436 1290.5037% 386.2751% 838.3894%
35.000 9.88 35.160 169.763 327.843 1288.3520% 385.0377% 836.6949%
Sample data: Large ℎ
Application of Derivative Formulae
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
46
Exact h FD BD CD FD_et BD_et CD_et
35.000 0.13 35.160 32.971 35.051 6.0877% 5.7980% 0.1449%
35.000 0.12 35.160 33.123 35.043 5.6091% 5.3623% 0.1234%
35.000 0.11 35.160 33.276 35.036 5.1323% 4.9249% 0.1037%
35.000 0.10 35.160 33.430 35.030 4.6571% 4.4857% 0.0857%
35.000 0.09 35.160 33.584 35.024 4.1837% 4.0449% 0.0694%
35.000 0.08 35.160 33.739 35.019 3.7120% 3.6023% 0.0549%
35.000 0.07 35.160 33.895 35.015 3.2420% 3.1580% 0.0420%
35.000 0.06 35.160 34.051 35.011 2.7737% 2.7120% 0.0309%
35.000 0.05 35.160 34.207 35.007 2.3071% 2.2643% 0.0214%
35.000 0.04 35.160 34.365 35.005 1.8423% 1.8149% 0.0137%
35.000 0.03 35.160 34.523 35.003 1.3791% 1.3637% 0.0077%
35.000 0.02 35.160 34.681 35.001 0.9177% 0.9109% 0.0034%
35.000 0.01 35.160 34.840 35.000 0.4580% 0.4563% 0.0009%
Sample data: Small ℎ
Application of Derivative Formulae: Matlab
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
47
f = @(x) 3*x.^3 - 2*x.^2 + 7*x - 10; % Function to differentiate
df = @(x) 9*x.^2 - 4*x + 7; % Analytical derivative
re = @(t,e) abs((t - e) ./ t) * 100.0; % Function for calculating relative error
x = 2; % Point to take derivative
h = 0.01:0.01:10.0; % Sweep h
df_fd = (f(x+h) - f(x))./h; % Forward difference
df_bd = (f(x) - f(x-h))./h; % Backward difference
df_cd = (f(x+h) - f(x-h))./(2.0*h); % Centered difference
df_exact = df(x); % Exact answer
re_fd = re(df_exact,df_fd); %Calculate absolute relative errors
re_bd = re(df_exact,df_bd);
re_cd = re(df_exact,df_cd);
plot(h,re_fd,h,re_bd,h,re_cd);
set(gca, 'XDir','reverse');
set(gca, 'XScale', 'log');
set(gca, 'YScale', 'log');
set(gca,'FontSize',16)
legend({'Forward Difference','Backward Difference','Centered Difference'},'Fontsize',18);
xlabel('h','FontSize',18);
ylabel('% Absolute True Relative Error','FontSize',18);
grid;
fprintf(' Exact h FD BD CD FD_et BD_et CD_etn');
for l = length(h):-1:1
fprintf('%6.3f %5.2f %6.3f %5.3f %5.3f %7.4f%% %7.4f%% %7.4f%%n',...
df_exact,h(l),df_fd(1),df_bd(l),df_cd(l),re_fd(l),re_bd(l),re_cd(l));
end
Some Formulae for Second Derivatives
Type Formula Error
Forward Difference 𝑓"(𝑥𝑖) ≈
𝑓(𝑥𝑖+2) − 2𝑓(𝑥𝑖+1) + 𝑓(𝑥𝑖)
ℎ2
𝑂 ℎ
Backward Difference 𝑓"(𝑥𝑖) ≈
𝑓(𝑥𝑖) − 2𝑓(𝑥𝑖−1) + 𝑓(𝑥𝑖−2)
ℎ2
𝑂 ℎ
Centered Difference 𝑓"(𝑥𝑖) ≈
𝑓(𝑥𝑖+1) − 2𝑓(𝑥𝑖) + 𝑓(𝑥𝑖−1)
ℎ2
𝑂 ℎ2
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
48
Formulae also exist for 𝑓 3
, 𝑓 4
, …
Derivative Formulae: A Final Word
• The formulae shown previously are often sufficiently accurate for many
problems, especially the centered difference formulae
• However sometimes even higher accuracy is required, and formulae exist
with even better error properties.
• Example,
𝑓′(𝑥𝑖) ≈
−𝑓 𝑥𝑖+2 + 8𝑓 𝑥𝑖+1 − 8𝑓 𝑥𝑖−1 + 𝑓 𝑥𝑖−2
12ℎ
Error is 𝑂 ℎ4
The computational cost?  2x the number of function evaluations
Copyright 2021, Prof. Swart, UBCO Engineering
Do not copy without permission
49
Derivative Formulae: A Final Word
Copyright 2021, Prof. Swart, UBCO Engineering Do not copy
without permission
50
WOW!
Errors: Summary
Copyright 2021, Prof. Swart, UBCO Engineering Do not copy
without permission
51
• In the prior sections, we saw roundoff errors arise from
digital approximations of values, and truncation errors
arise from approximations of mathematical expressions.
• In general, both these errors will be present
• Roundoff errors accumulate as you execute more and
more steps/computations, and so it is advantageous to
execute as few steps/computations as possible or be clever
to find ways to ensure that roundoff error does not
accumulate
• Truncation errors arise from our inability to apply 'true'
mathematical expressions, and it can be advantageous to
use as many steps/computations as possible with step sizes
that are as small as possible.
• Given these opposing demands, we must carefully balance
roundoff and truncation errors to find an optimal point
with minimized total error. You can see such balancing in
the following log-log plot of error versus step size.

LectureSlides3.pdf

  • 1.
    APSC 256 Numerical Methodsfor Analysis Roundoff and Truncation Errors APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 1
  • 2.
    Accuracy and Precision •Commonly misunderstood terms by the layperson • What does the Oxford dictionary say? • Accuracy? • the degree to which the result of a measurement, calculation, or specification conforms to the correct value or a standard. • Precision? • refinement in a measurement, calculation, or specification, especially as represented by the number of digits given. Not bad! APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 2
  • 3.
    Accuracy and Precision APSC256 Slides - Copyright UBC Okanagan and Nicholas Swart 3
  • 4.
    Error Measures • TrueError 𝐸𝑡 = • How useful is this? Discuss? APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 4
  • 5.
    Error Measures • Truerelative error 𝜖𝑡 = • Approximate relative error 𝜖𝑎 = APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 5
  • 6.
    Error Measures • Stoppingcriterion 𝜖𝑠 • Stop when: 𝜖𝑎 ≤ 𝜖𝑠 • It can be shown that the stopping criterion, 𝜖𝑠, is related to the number of base-10 significant figures, 𝑛, through the following relationship: 𝜖𝑎 = 𝜖𝑠 𝜖𝑠 = 0.5 × 102−𝑛 % APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 6
  • 7.
    Error Measures: Example •Consider the following series approximation to 𝑒𝑥 • We can write some simple MATLAB code to evaluate this series for any number of terms, and then calculate 𝜖𝑡 and 𝜖𝑎 • But how do we know the true value of 𝑒𝑥 which would need to calculate 𝜖𝑡? • Discuss APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 7
  • 8.
    Error Measures: Example APSC256 Slides - Copyright UBC Okanagan and Nicholas Swart 8 % Approximate exp(x) using a series % MATLAB factorial function is called 'factorial' n = 3; % Number of significant figures at which to stop es = (0.5 * 10 ^ (2-n)); % This is a percentage x = 0.5; sum = 0.0; true = exp(x); % We assume MATLAB calculates exp(x) very accurately fprintf("Terms Results etrue%% eapprox%%n"); for k = 0:10 % Loop no more than 11 times sum = sum + 1/factorial(k)*x^k; et = (true - sum)/true*100.0; if k == 0 % Cannot calculate approximate error without % an previous result, so skip when k = 0 fprintf(" %d %.15f %6.3f N/An",k+1,sum,et) else ea = (sum - sum_save)/sum*100.0; % Previous approximation is sum_save fprintf(" %d %.15f %7.4f %7.4fn",k+1,sum,et,ea) if (abs(ea) <= es) break; % Exit the loop, we have reached es end end sum_save = sum; end Terms Results etrue% eapprox% 1 1.000000000000000 39.347 N/A 2 1.500000000000000 9.0204 33.3333 3 1.625000000000000 1.4388 7.6923 4 1.645833333333333 0.1752 1.2658 5 1.648437500000000 0.0172 0.1580 6 1.648697916666667 0.0014 0.0158
  • 9.
    Sources of Errorin Numerical Methods • Two of the most important sources of error to understand in numerical methods are roundoff error and truncation error APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 9
  • 10.
    Roundoff Errors • Asimple base 10 representation (with normalization) • Overflow errors • Underflow errors • Roundoff errors APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 10
  • 11.
    Roundoff Errors: Asimple base-10 representation APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 11
  • 12.
    Overflow Errors APSC 256Slides - Copyright UBC Okanagan and Nicholas Swart 12
  • 13.
    Underflow errors APSC 256Slides - Copyright UBC Okanagan and Nicholas Swart 13
  • 14.
    Roundoff Errors APSC 256Slides - Copyright UBC Okanagan and Nicholas Swart 14 𝑠𝑚𝑑𝑚0. 𝑑𝑚1 × 10𝑠𝑒𝑑𝑒0 Consider 0.1 1.0 +1.0 × 10−1 +1.0 × 10±0 0.11 +1.1 × 10−1 0.99 +9.9 × 10−1 Resolution Roundoff error (worst case)
  • 15.
    Roundoff Errors APSC 256Slides - Copyright UBC Okanagan and Nicholas Swart 15 1.0 10.0 +1.0 × 10±0 +1.0 × 10+1 1.1 +1.1 × 10±0 9.9 +9.9 × 10±0 Resolution Roundoff error (worse case) 10 100 +1.0 × 10+1 +1.0 × 10+2 11 +1.1 × 10+1 99 +9.9 × 10+1 Resolution Roundoff error (worse case)
  • 16.
    Roundoff Errors Range ResolutionRoundoff Error 0.1 - 1.0 1.0 - 10.0 10.0 - 100.0 APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 16 • Observations?
  • 17.
    Roundoff Errors • Whatabout the true relative roundoff error that we see? APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 17 0.1 0.11 0.105 𝜖𝑡 = 0.104999999 − 0.1 0.104999999 × 100 ≅ 4.8% 𝜖𝑡 = 0.105 − 0.11 0.105 × 100 ≅ 4.8% Round down Round up 0.99 1.0 0.995 𝜖𝑡 = 0.994999999 − 0.99 0.994999999 × 100 ≅ 0.5% 𝜖𝑡 = 0.995 − 1.0 0.995 × 100 ≅ 0.5% Round down Round up
  • 18.
    Roundoff Errors • Wecan do similar calculations for other ranges APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 18 Range Interval 𝝐𝒕 0.1 - 1.0 0.1 – 0.11 ≅ 5 % 0.99 – 1.0 ≅ 0.5 % 1.0 - 10.0 1.0 – 1.1 ≅ 5 % 9.9 - 10 ≅ 0.5 % 10.0 - 100.0 10.0 - 11 ≅ 5 % 99 - 100 ≅ 0.5 % Machine precision Maximum true relative error This is a very useful metric to know when doing complex numerical calculations Note that the exponent digits have a significant impact on the largest and smallest values but no effect on the machine precision
  • 19.
    Round Errors • Example APSC256 Slides - Copyright UBC Okanagan and Nicholas Swart 19 𝑠𝑚𝑑𝑚0. 𝑑𝑚1 × 10𝑠𝑒𝑑𝑒0𝑑𝑒1
  • 20.
    Roundoff Errors • ….arereal, even in numerical systems which high precision APSC 256 Slides - Copyright UBC Okanagan and Nicholas Swart 20 >> a = 1 - 3*(4/3 - 1) a = 2.220446049250313e-16 a = 0.0; for i = 1:10 a = a + 0.1; end a == 1 ans = logical 0 • Roundoff errors cannot be ignored, and they are very real, but with care they can be managed
  • 21.
    Truncation Errors Part I Dr.Nicholas Swart UBCO School of Engineering Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 21
  • 22.
    Truncation Errors • Truncationerrors are different from round-off errors • Round-off errors arise because of finite precision in computer storage of floating-point numbers • Truncation errors on the other hand arise from the mathematical approximations employed in the numerical method • The term “truncation” implies that we have truncated or pruned in some fashion a mathematical calculation which has then led to an approximation to that true answer • One of the most common reasons we employ mathematical approximations is to make numerical methods more efficient • In some cases, it is not even possible to work with a non-truncated mathematical method due to roundoff error • We run out of “numerical steam” and the solution gets swamped by roundoff error Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 22
  • 23.
    Truncation Errors: Example •We illustrate the concept of truncation error by example • Consider the very beautiful Taylor series, which is a polynomial approximation of a function 𝑓(𝑥) about 𝑥 = 𝑎 , 𝑓(𝑥) = 𝑓(𝑎) + 𝑓′(𝑎) 1! (𝑥 − 𝑎) + 𝑓′′(𝑎) 2! (𝑥 − 𝑎)2 + 𝑓′′′(𝑎) 3! (𝑥 − 𝑎)3 +. . . = ෍ 𝑛=0 ∞ 𝑓(𝑛) (𝑎) 𝑛! (𝑥 − 𝑎)𝑛 • The MacLaurin series is the Taylor series when 𝑎 = 0 , 𝑓(𝑥) = 𝑓(0) + 𝑓′(0) 1! 𝑥 + 𝑓′′(0) 2! 𝑥2 + 𝑓′′′(0) 3! 𝑥3 +. . . = ෍ 𝑛=0 ∞ 𝑓(𝑛) (0) 𝑛! 𝑥𝑛 • In general, unless 𝑓 is a polynomial, an infinite number of terms is required in the Taylor series in order to exactly calculate 𝑓(𝑥) Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 23
  • 24.
    Truncation Errors: Example •The Taylor series is extremely useful in numerical methods, and often we use a slightly different notation for the series, 𝑓(𝑥𝑖+1) = 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖) 1! ℎ + 𝑓′′(𝑥𝑖) 2! ℎ2 + 𝑓′′′(𝑥𝑖) 3! ℎ3 +. . . = ෍ 𝑛=0 ∞ 𝑓(𝑛) (𝑥𝑖) 𝑛! ℎ𝑛 where ℎ = 𝑥𝑖+1 − 𝑥𝑖 . We often refer to ℎ as the “step size”, and by default ℎ is always assumed to be specified as a positive value • The usefulness of this notation will be come apparent soon, but remember that the above form of the Taylor series is no different than the more common form seen in Calculus textbooks • The form shown here lends itself slightly better to numerical methods Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 24
  • 25.
    Truncation Errors: Example •Let’s put the Taylor series to the side for the moment and ask ourselves how we might numerically approximate the derivate to a function 𝑓(𝑥) at some point 𝑥 = 𝑥𝑖. • Derivatives are very commonly required in engineering problems • Even though it is often possible to find an analytical solution for 𝑓′ 𝑥 there are other reasons why we need to take numerical derivatives which we will see later in the course • Let’s look at three simple approaches Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 25
  • 26.
    Truncation Errors: Example •Intuitively, these approaches seem like they might work, but were not certain • Intuitively, these approaches seem like they might be more accurate as ℎ → 0 • Intuitively, the approach on the right seems like it might be more accurate Intuition alone is insufficient for engineering analysis. How do we convert intuition to mathematical rigour? Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 26 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖 ℎ 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1 ℎ 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 2ℎ
  • 27.
    Truncation Errors: Example •Let’s return to the Taylor series , 𝑓(𝑥𝑖+1) = 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖) 1! ℎ + 𝑓′′(𝑥𝑖) 2! ℎ2 + 𝑓′′′(𝑥𝑖) 3! ℎ3 +. . . = ෍ 𝑛=0 ∞ 𝑓(𝑛) (𝑥𝑖) 𝑛! ℎ𝑛 • It would be obviously impractical (compute time and also roundoff error) to use an infinite number of terms in the Taylor series when calculating 𝑓 𝑥𝑖+1 so we truncate the series, which results in an approximation of 𝑓 𝑥𝑖+1 . Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 27
  • 28.
    Truncation Errors: Example •Zeroth – order approximation, 𝑓(𝑥𝑖+1) ≈ 𝑓(𝑥𝑖) • First-order approximation 𝑓(𝑥𝑖+1) ≈ 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖)ℎ Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 28 • Second-order approximation 𝑓(𝑥𝑖+1) ≈ 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖) 1! ℎ + 𝑓′′(𝑥𝑖) 2! ℎ2 • Third-order approximation 𝑓(𝑥𝑖+1) ≈ 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖) 1! ℎ + 𝑓′′(𝑥𝑖) 2! ℎ2 + 𝑓′′′(𝑥𝑖) 3! ℎ3 • In each of these approximation we have thrown away higher-order terms in the Taylor series, which introduces error. But how much error?
  • 29.
    Truncation Errors: Example •The terms that are truncated in the Taylor series can be grouped together into a single term called 𝑅𝑛, 𝑓 𝑥𝑖+1 = 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖) 1! ℎ + 𝑓′′(𝑥𝑖) 2! ℎ2 + 𝑓′′′(𝑥𝑖) 3! ℎ3 +. . . 𝑓(𝑛) (𝑥𝑖) 𝑛! ℎ𝑛 + 𝑅𝑛 • Through some careful mathematical analysis, it can be shown that, 𝑅𝑛 = 𝑓(𝑛+1)(𝜉) (𝑛 + 1)! ℎ𝑛+1 where 𝜉 lies between 𝑥𝑖 and 𝑥𝑖+1. • This is known as the Lagrange form of the remainder of the Taylor series Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 29
  • 30.
    Truncation Errors: Example •Let’s write the series with the remainder term explicitly, 𝑓 𝑥𝑖+1 = 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖) 1! ℎ + 𝑓′′(𝑥𝑖) 2! ℎ2 + 𝑓′′′(𝑥𝑖) 3! ℎ3 +. . . 𝑓(𝑛) (𝑥𝑖) 𝑛! ℎ𝑛 + 𝑓(𝑛+1) (𝜉) (𝑛 + 1)! ℎ𝑛+1 • This is a very beautiful expression • It shows us how to find an n’th order polynomial approximation of 𝑓(𝑥) and tell us what the remainder (error) will be • Keep in mind, we don’t know the exact value of 𝜉. If we did, we would know the exact value of the remainder. But very importantly, we do know that 𝜉 lies between 𝑥𝑖 and 𝑥𝑖+1. Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 30
  • 31.
    Truncation Errors: Example •Let’s now look at a 1st order approximation to 𝑓 𝑥𝑖+1 , that is to say the approximation when 𝑛 = 1 , 𝑓 𝑥𝑖+1 = 𝑓(𝑥𝑖) + 𝑓′(𝑥𝑖) 1! ℎ + 𝑓(2)(𝜉) 2! ℎ2 , for 𝜉 between 𝑥𝑖 and 𝑥𝑖+1 • Then solve this equation for 𝑓′(𝑥𝑖) , 𝑓′ 𝑥𝑖 = 𝑓 𝑥𝑖+1 −𝑓 𝑥𝑖 ℎ − 𝑓(2)(𝜉) 2! ℎ Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 31
  • 32.
    Truncation Errors: Example •Copying the previous equation. 𝑓′ 𝑥𝑖 = 𝑓 𝑥𝑖+1 −𝑓 𝑥𝑖 ℎ − 𝑓(2)(𝜉) 2! ℎ , for 𝜉 between 𝑥𝑖 and 𝑥𝑖+1 • Let’s truncate the remainder term, 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖 ℎ • And here we now see the original derivative method we proposed earlier Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 32
  • 33.
    Truncation Errors: Example •What about the error term we truncated (threw away) that led to the approximation? What can we say about it? − 𝑓(2)(𝜉) 2! ℎ , for 𝜉 between 𝑥𝑖 and 𝑥𝑖+1 • We don’t know the exact value of 𝜉, but for any given 𝑥𝑖 and 𝑥𝑖+1 it is a constant. We write the error term as follows, 𝛽ℎ • Recall that ℎ = 𝑥𝑖+1 − 𝑥𝑖, which we referred to as the step size. For a given 𝛽 we know the following, ℎ ↑ ⇒ 𝛽ℎ ↑ by the same proportion ℎ ↓ ⇒ 𝛽ℎ ↓ by the same proportion • In other words, if we double ℎ then 𝛽ℎ doubles, and if we decrease ℎ by half then 𝛽ℎ decreases by half Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 33
  • 34.
    Truncation Errors: Example •Continuing …. • In other words, even though we don’t know the exact value of 𝛽, and hence don’t know the exact error (𝑅_𝑛), we know how 𝛽ℎ behaves as a function of ℎ. • There is an important mathematical notation that is used to describe how a function behaves for a given input variable. It is called “big-Oh” notation, and in this case, we say that 𝑅𝑛 is big-Oh ℎ which we write as, 𝑅𝑛 is 𝑂(ℎ) • We say 𝑅𝑛 is “Order ℎ” • NOTE: Big-Oh has a mathematically formal definition, and is used heavily in computer science, but we do not need to concern ourselves with those details for the purposes of this course. Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 34
  • 35.
    Truncation Errors: Example •In summary we have an approximation to the derivative, 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖 ℎ • We say that for this approximation, the error is 𝑂 ℎ , which means that as we decrease ℎ in order to improve the accuracy of the result, the error will drop by approximately the same factor that we decreased ℎ by. Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 35
  • 36.
    Truncation Errors: Example •Using a very similar analysis we can derive the second derivative formula that we saw at the start of this section, 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1 ℎ • Be careful, we always want ℎ to be positive, and here ℎ = 𝑥𝑖 − 𝑥𝑖−1. • If you proceed through the mathematical analysis (I recommend that you do so) you will find that this formula also has an error of 𝑂 ℎ . Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 36
  • 37.
    Truncation Errors: Example •Let’s turn to the final integration formula we saw earlier, 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 2ℎ • We can derive this formula by using the following two 2nd order Taylor series, 𝑓 𝑥𝑖+1 = 𝑓 𝑥𝑖 + 𝑓′ 𝑥𝑖 1! ℎ + 𝑓′′ 𝑥𝑖 2! ℎ2 + 𝑓′′′ 𝜉1 3! ℎ3 , for 𝜉1 between 𝑥𝑖 and 𝑥𝑖+1 𝑓 𝑥𝑖−1 = 𝑓 𝑥𝑖 − 𝑓′ 𝑥𝑖 1! ℎ + 𝑓′′ 𝑥𝑖 2! ℎ2 − 𝑓′′′ 𝜉2 3! ℎ3 , for 𝜉2 between 𝑥𝑖−1 and 𝑥𝑖 • Note we assume ℎ = 𝑥𝑖+1 − 𝑥𝑖 = 𝑥𝑖 − 𝑥𝑖−1 Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 37
  • 38.
    Truncation Errors: Example •Now subtract as follows: 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 = 2 𝑓′ 𝑥𝑖 1! ℎ + 𝑓′′′ 𝜉1 3! ℎ3 + 𝑓′′′ 𝜉2 3! ℎ3 • Solving for 𝑓′ 𝑥𝑖 gives, 𝑓′ 𝑥𝑖 = 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 2ℎ − 1 2 ⋅ 3! 𝑓′′′ 𝜉1 + 𝑓′′′ 𝜉2 ℎ2 Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 38
  • 39.
    Truncation Errors: Example •In this case we can treat the error similarly to how we did so before. We don’t know the exact values of 𝜉1 and 𝜉2, but for any given 𝑥𝑖 and 𝑥𝑖+1, 𝜉1 is a constant, and similarly for any given 𝑥𝑖−1 and 𝑥𝑖, 𝜉2 is a constant. We write the error term as follows, 𝛽ℎ2 • We now have another derivative formula and the associated big-Oh for the error, 𝑓′ 𝑥𝑖 = 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 2ℎ • Where the error is 𝑂 ℎ2 Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 39
  • 40.
    Truncation Errors: Summary Error𝑂(ℎ) Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 40 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖 ℎ 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1 ℎ 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 2ℎ Error 𝑂(ℎ) Error 𝑂(ℎ2) • The error in the first two methods decreases as 𝑂(ℎ), whereas the error in the third method decreases as 𝑂 ℎ2 • The 2nd method is therefore better as ℎ is decreased • What is especially attractive is that we have improved the method while still only requiring 2 function evaluations.
  • 41.
    Truncation Errors Part II Dr.Nicholas Swart UBCO School of Engineering Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 41
  • 42.
    Truncation Errors Example:Recap from Part II Error 𝑂(ℎ) Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 42 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖 ℎ 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1 ℎ 𝑓′ 𝑥𝑖 ≈ 𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1 2ℎ Error 𝑂(ℎ) Error 𝑂(ℎ2) • The error in the first two methods decreases as 𝑂(ℎ), whereas the error in the third method decreases as 𝑂 ℎ2 • The 2nd method therefore performs substantially better as ℎ is decreased • What is especially attractive is that we have improved the performance while still only requiring 2 function evaluations. Forward Difference Backward Difference Centered Difference
  • 43.
    Application of DerivativeFormulae • Consider the following function, 𝑓 𝑥 = 3𝑥3 − 2𝑥2 + 7𝑥 − 10 • We can easily find the analytical derivative of this function, 𝑓′ 𝑥 = 9𝑥2 − 4𝑥 + 7 • Let’s examine how the three different derivative formulae perform Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 43
  • 44.
    Application of DerivativeFormulae • Estimate 𝑓′(2) using the three derivative formulae for ℎ from 10 to 0.01 in steps of 0.01 • Exact: 𝑓′ 2 = 35 • Calculate the % absolute true relative error for each method Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 44
  • 45.
    Application of DerivativeFormulae Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 45 Exact h FD BD CD FD_et BD_et CD_et 35.000 10.00 35.160 175.000 335.000 1314.2857% 400.0000% 857.1429% 35.000 9.99 35.160 174.560 334.400 1312.1151% 398.7437% 855.4294% 35.000 9.98 35.160 174.121 333.801 1309.9463% 397.4891% 853.7177% 35.000 9.97 35.160 173.683 333.203 1307.7791% 396.2363% 852.0077% 35.000 9.96 35.160 173.245 332.605 1305.6137% 394.9851% 850.2994% 35.000 9.95 35.160 172.807 332.007 1303.4500% 393.7357% 848.5929% 35.000 9.94 35.160 172.371 331.411 1301.2880% 392.4880% 846.8880% 35.000 9.93 35.160 171.935 330.815 1299.1277% 391.2420% 845.1849% 35.000 9.92 35.160 171.499 330.219 1296.9691% 389.9977% 843.4834% 35.000 9.91 35.160 171.064 329.624 1294.8123% 388.7551% 841.7837% 35.000 9.90 35.160 170.630 329.030 1292.6571% 387.5143% 840.0857% 35.000 9.89 35.160 170.196 328.436 1290.5037% 386.2751% 838.3894% 35.000 9.88 35.160 169.763 327.843 1288.3520% 385.0377% 836.6949% Sample data: Large ℎ
  • 46.
    Application of DerivativeFormulae Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 46 Exact h FD BD CD FD_et BD_et CD_et 35.000 0.13 35.160 32.971 35.051 6.0877% 5.7980% 0.1449% 35.000 0.12 35.160 33.123 35.043 5.6091% 5.3623% 0.1234% 35.000 0.11 35.160 33.276 35.036 5.1323% 4.9249% 0.1037% 35.000 0.10 35.160 33.430 35.030 4.6571% 4.4857% 0.0857% 35.000 0.09 35.160 33.584 35.024 4.1837% 4.0449% 0.0694% 35.000 0.08 35.160 33.739 35.019 3.7120% 3.6023% 0.0549% 35.000 0.07 35.160 33.895 35.015 3.2420% 3.1580% 0.0420% 35.000 0.06 35.160 34.051 35.011 2.7737% 2.7120% 0.0309% 35.000 0.05 35.160 34.207 35.007 2.3071% 2.2643% 0.0214% 35.000 0.04 35.160 34.365 35.005 1.8423% 1.8149% 0.0137% 35.000 0.03 35.160 34.523 35.003 1.3791% 1.3637% 0.0077% 35.000 0.02 35.160 34.681 35.001 0.9177% 0.9109% 0.0034% 35.000 0.01 35.160 34.840 35.000 0.4580% 0.4563% 0.0009% Sample data: Small ℎ
  • 47.
    Application of DerivativeFormulae: Matlab Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 47 f = @(x) 3*x.^3 - 2*x.^2 + 7*x - 10; % Function to differentiate df = @(x) 9*x.^2 - 4*x + 7; % Analytical derivative re = @(t,e) abs((t - e) ./ t) * 100.0; % Function for calculating relative error x = 2; % Point to take derivative h = 0.01:0.01:10.0; % Sweep h df_fd = (f(x+h) - f(x))./h; % Forward difference df_bd = (f(x) - f(x-h))./h; % Backward difference df_cd = (f(x+h) - f(x-h))./(2.0*h); % Centered difference df_exact = df(x); % Exact answer re_fd = re(df_exact,df_fd); %Calculate absolute relative errors re_bd = re(df_exact,df_bd); re_cd = re(df_exact,df_cd); plot(h,re_fd,h,re_bd,h,re_cd); set(gca, 'XDir','reverse'); set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log'); set(gca,'FontSize',16) legend({'Forward Difference','Backward Difference','Centered Difference'},'Fontsize',18); xlabel('h','FontSize',18); ylabel('% Absolute True Relative Error','FontSize',18); grid; fprintf(' Exact h FD BD CD FD_et BD_et CD_etn'); for l = length(h):-1:1 fprintf('%6.3f %5.2f %6.3f %5.3f %5.3f %7.4f%% %7.4f%% %7.4f%%n',... df_exact,h(l),df_fd(1),df_bd(l),df_cd(l),re_fd(l),re_bd(l),re_cd(l)); end
  • 48.
    Some Formulae forSecond Derivatives Type Formula Error Forward Difference 𝑓"(𝑥𝑖) ≈ 𝑓(𝑥𝑖+2) − 2𝑓(𝑥𝑖+1) + 𝑓(𝑥𝑖) ℎ2 𝑂 ℎ Backward Difference 𝑓"(𝑥𝑖) ≈ 𝑓(𝑥𝑖) − 2𝑓(𝑥𝑖−1) + 𝑓(𝑥𝑖−2) ℎ2 𝑂 ℎ Centered Difference 𝑓"(𝑥𝑖) ≈ 𝑓(𝑥𝑖+1) − 2𝑓(𝑥𝑖) + 𝑓(𝑥𝑖−1) ℎ2 𝑂 ℎ2 Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 48 Formulae also exist for 𝑓 3 , 𝑓 4 , …
  • 49.
    Derivative Formulae: AFinal Word • The formulae shown previously are often sufficiently accurate for many problems, especially the centered difference formulae • However sometimes even higher accuracy is required, and formulae exist with even better error properties. • Example, 𝑓′(𝑥𝑖) ≈ −𝑓 𝑥𝑖+2 + 8𝑓 𝑥𝑖+1 − 8𝑓 𝑥𝑖−1 + 𝑓 𝑥𝑖−2 12ℎ Error is 𝑂 ℎ4 The computational cost?  2x the number of function evaluations Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 49
  • 50.
    Derivative Formulae: AFinal Word Copyright 2021, Prof. Swart, UBCO Engineering Do not copy without permission 50 WOW!
  • 51.
    Errors: Summary Copyright 2021,Prof. Swart, UBCO Engineering Do not copy without permission 51 • In the prior sections, we saw roundoff errors arise from digital approximations of values, and truncation errors arise from approximations of mathematical expressions. • In general, both these errors will be present • Roundoff errors accumulate as you execute more and more steps/computations, and so it is advantageous to execute as few steps/computations as possible or be clever to find ways to ensure that roundoff error does not accumulate • Truncation errors arise from our inability to apply 'true' mathematical expressions, and it can be advantageous to use as many steps/computations as possible with step sizes that are as small as possible. • Given these opposing demands, we must carefully balance roundoff and truncation errors to find an optimal point with minimized total error. You can see such balancing in the following log-log plot of error versus step size.