Chapter 3 Approximation and Round-Off Errors
Chapter 3 Approximation and Round-Off Errors
Off-page
Break that is continued on another page
connector
Uses code like statements in place of
graphical symbols of the flow chart
Advantages
Easier
Easier to modify and share
Sequence
Selection
Repetition
The computer code is to be
implemented one instruction at a time.
𝐼𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛1 𝐼𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛1
𝐼𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛2
𝐼𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛2 𝐼𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛3
𝐼𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛3
flowchart pseudocode
Provides means to split the program’s
flow into branches based on the
outcome of logical condition
Examples
IF / THEN
IF / THEN / ELSE
CASE
Conditio True
n?
False
True block IF condition THEN
True block
ENDIF
flowchart pseudocode
False True
Conditio
n?
IF condition THEN
True block
False block True block ELSE
False block
ENDIF
flowchart pseudocode
False Condition True
1?
flowchart pseudocode
The table below describes the assignment of
grades based on an exam score.
90 and above A
80-89 B
70-79 C
60-69 D
below 60 F
-- correct grade assignment
IF Score >= 90 THEN
Ada.Text_IO.Put (Item=>'A');
ELSIF Score >= 80 THEN
Ada.Text_IO.Put (Item=>B');
ELSIF Score >= 70 THEN
Ada.Text_IO.Put (Item=>'C');
ELSIF Score >= 60 THEN
Ada.Text_IO.Put (Item=>'D');
ELSE
Ada.Text_IO.Put (Item=>'F');
END IF;
Condition
1?
SELECT CASE Test Expression
CASE Value 1
Block 1
CASE Value 2
Block 2
Block 1 Block 2 Block 3 Block 4
CASE Value 3
Block 3
CASE ELSE
Block 4
END SELECT
flowchart pseudocode
Provides a means to implement instructions
repeatedly.
Provides construction loops
DO
Block 1 Block 1
IF Condition EXIT
Block 2
Condition True ENDDO
1?
False
Block 2
flowchart pseudocode
DOFOR i = start, finish,
True
i > finish i=start step
i =i
+start
False Block
Block ENDDO
flowchart pseudocode
The roots of a quadratic equation
𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0
can be determined with the quadratic formula
𝑥1 −𝑏 ± 𝑏 2 − 4𝑎𝑐
=
𝑥2 2𝑎
Develop an algorithm that does the following
Prompts the user for the coefficients a, b and c
Implements the quadratic formula, guarding against
all eventualities (for example, avoiding division by
zero and allowing for complex roots)
Displays the solution, that is, the values for x
Allows the user the option to return to step 1 and
repeat the process
If the discriminant is positive, then there are
two distinct real roots.
If the discriminant is zero, then the two
roots are equal.
If the discriminant is negative, then there
are two distinct complex roots.
Case 1: If the discriminant is positive,
#include <math.h>
int main()
discriminant = b*b-4*a*c;
if (discriminant > 0)
root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);
else if (discriminant == 0)
Truncation Error
Discrepancy introduced by approximation
Omission of the remaining significant figures
Accuracy
Refers to how closely a computed or measured
value agrees with the true value
Precision
Refers to how closely individual computed or
measured values agree with each other
Truncation errors
Approximations are used to represent exact
mathematical procedures
Round-off errors
Numbers having limited significant figures are
used to represent exact numbers
𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒 = 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛 + 𝑒𝑟𝑟𝑜𝑟
𝐸𝑡 = 10000 − 9999 = 1 𝑐𝑚
𝐸𝑡 = 10 − 9 = 1 𝑐𝑚
𝜀𝑎 < 𝜀𝑠
𝜀𝑠 = 0.5 × 102−𝑛 %
𝑛 = 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑠𝑖𝑔𝑛𝑖𝑓𝑖𝑐𝑎𝑛𝑡 𝑓𝑖𝑔𝑢𝑟𝑒𝑠
In mathematics, functions can often be represented
by infinite series. For example, the exponential
function can be computed using
𝑥 2 𝑥 3 𝑥 𝑛
𝑒𝑥 = 1 + 𝑥 + + + ⋯+ 𝑀𝑎𝑐𝑙𝑎𝑢𝑟𝑖𝑛 𝑆𝑒𝑟𝑖𝑒𝑠
2! 3! 𝑛!
Starting with the simplest version ex=1, add terms
one at a time to estimate e0.5.Compute the true and
relative errors. Note that e0.5=1.648721… Add terms
until the absolute value of the approximate error
estimate (εa) falls below pre-specified error criterion
εs conforming to three significant figures.
Solving for pre-specified tolerance 𝑥
𝑥2 𝑥3 𝑥𝑛
𝑒 =1+𝑥+ + + ⋯+
2! 3! 𝑛!
𝜀𝑠 = 0.5 × 102−𝑛 %
𝜀𝑠 = 0.5 × 102−3 %
𝜀𝑠 = 0.05%
1st estimate
𝑒 0.5 = 1
𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒 − 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛
𝜀𝑡 = 100%
𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒
1.648721 − 1
𝜀𝑡 = 100%
1.648721
𝜀𝑡 = 39.34%
𝑥
𝑥2 𝑥3 𝑥𝑛
𝑒 =1+𝑥+ + + ⋯+
2nd estimate 2! 3! 𝑛!
𝑒 0.5 = 1 + 𝑥 𝜀𝑠 = 0.05%
𝑒 0.5 = 1 + 0.5
𝑒 0.5 = 1.5
1.648721 − 1.5
𝜀𝑡 = 100%
1.648721
𝜀𝑡 = 9.02%
𝑥2 𝑥4 𝑥6 𝑥8
cos 𝑥 = 1 − + − + ⋯
2 4! 6! 8!
Evaluate 𝑒 −5 using two approaches given below
and compare with the true value of
6.737947x10-3. Use 20 terms to evaluate each
series and compute true and approximate
relative errors as terms are added.
𝑥2 𝑥3
𝑒 −𝑥 =1−𝑥+ − +⋯
2 3!
−𝑥
1 1
𝑒 = 𝑥=
𝑒 𝑥2 𝑥3
1 + 𝑥 + 2 + 3! + ⋯
Use the Maclaurin series expansion for the sin x
to estimate sin(pi/3)