[go: up one dir, main page]

0% found this document useful (0 votes)
75 views50 pages

Chapter 3 Approximation and Round-Off Errors

Here are the key steps to compute ex using the Maclaurin series approximation: 1. The Maclaurin series for ex is: ex = 1 + x + x2/2! + x3/3! + ... + xn/n! 2. Choose the number of terms n to use in the approximation. More terms yields greater accuracy. 3. Compute each term in the series: - 1st term is 1 - 2nd term is x - 3rd term is x2/2! = x2/2 - 4th term is x3/3! = x3/6 4. Sum all the terms to obtain the approximation for
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views50 pages

Chapter 3 Approximation and Round-Off Errors

Here are the key steps to compute ex using the Maclaurin series approximation: 1. The Maclaurin series for ex is: ex = 1 + x + x2/2! + x3/3! + ... + xn/n! 2. Choose the number of terms n to use in the approximation. More terms yields greater accuracy. 3. Compute each term in the series: - 1st term is 1 - 2nd term is x - 3rd term is x2/2! = x2/2 - 4th term is x3/3! = x3/6 4. Sum all the terms to obtain the approximation for
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Chapter 2

 Setof instructions that direct the computer


to perform a certain task.
 Simple information representation
 Advanced information representation
 Mathematical formulas
 Input / output
 Logical representation
 Modular programming
 Set
of rules that prescribe good style for the
programmer
 Control structures
 Sequence
 Selection
 Repetition
 Graphical
representation of
an algorithm.
 Employs series of
blocks and arrows
which represents
operation or step
in algorithm
 Used for expressing and communicating
algorithms
 Useful in planning, unraveling or
communicating the logic of the program
 Excellent pedagogical tools.
Symbol Name Function

Terminal Beginning or end of program

Flowlines Flow of logic

Process Calculations or data manipulations

Input/output Inputs or outputs of data information

Comparison, question or decision that


Decision
determines alternative paths
Symbol Name Function

Junction Confluence of flowlines

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?

True IF Condition 1 THEN


False Condition
Block 1 Block 1
2?
ELSEIF Condition 2
Block 2
True Block 2 ELSEIF Condition 3
False Condition
3?
Block 3
ELSE
Block 4
Block 4
ENDIF
Block 3

flowchart pseudocode
The table below describes the assignment of
grades based on an exam score.

Exam Score Grade Assigned

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,

 r1 = (-b +?k)/ 2a and r2 =(b +?k)/ 2a are the two roots.

 Case 2: If the discriminant is zero,

 r1 = r2 = (-b / 2a)are the two roots.

 Case 3: If the discriminant is negative,

 r1 = (-b +i ?k)/ 2a and r2 =(b + i?k)/ 2a are the two roots.


#include <stdio.h>

#include <math.h>

int main()

double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

printf("Enter coefficients a, b and c: ");

scanf(“%lf %lf %lf”,&a, &b, &c);

discriminant = b*b-4*a*c;

// condition for real and different roots

if (discriminant > 0)

// sqrt() function returns square root

root1 = (-b+sqrt(discriminant))/(2*a);

root2 = (-b-sqrt(discriminant))/(2*a);

printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);

//condition for real and equal roots

else if (discriminant == 0)

root1 = root2 = -b/(2*a);


Chapter 3
 How much error is present in our calculations
and is it tolerable?
 Identification
 Quantification
 Minimization of errors
 Round-off Error
 Computers can represent only quantities with
finite number of digits

 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
𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒 = 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛 + 𝑒𝑟𝑟𝑜𝑟

Rearranging the equation

𝑒𝑟𝑟𝑜𝑟 = 𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒 − 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛


or
𝐸𝑡 = 𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒 − 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛

𝐸𝑡 = 𝑒𝑥𝑎𝑐𝑡 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝑡ℎ𝑒 𝑒𝑟𝑟𝑜𝑟


𝑡𝑟𝑢𝑒 𝑒𝑟𝑟𝑜𝑟
𝑇𝑟𝑢𝑒 𝑓𝑟𝑎𝑐𝑡𝑖𝑜𝑛𝑎𝑙 𝑟𝑒𝑙𝑎𝑡𝑖𝑣𝑒 𝑒𝑟𝑟𝑜𝑟 =
𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒
𝑡𝑟𝑢𝑒 𝑒𝑟𝑟𝑜𝑟
𝜀𝑡 = 100%
𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒

𝜀𝑡 = 𝑡𝑟𝑢𝑒 𝑝𝑒𝑟𝑐𝑒𝑛𝑡 𝑟𝑒𝑙𝑎𝑡𝑖𝑣𝑒 𝑒𝑟𝑟𝑜𝑟


 Suppose that you have the task
of measuring the lengths of a
bridge and a rivet and come up
with 9,999 and 9 cm
respectively. If the true values
are 10,000 and 10 cm
respectively, compute
a) The true error
b) The true percent relative error
for each case
a) The error for measuring the bridge

𝐸𝑡 = 10000 − 9999 = 1 𝑐𝑚

The error for measuring the rivet

𝐸𝑡 = 10 − 9 = 1 𝑐𝑚

b) The percent relative error for the bridge is


1𝑐𝑚
𝜀𝑡 = 100% = 0.01%
10,000𝑐𝑚

for the rivet


1𝑐𝑚
𝜀𝑡 = 100% = 10%
10𝑚
 Error normalized to a an approximate value
𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑒 𝑒𝑟𝑟𝑜𝑟
𝜀𝑎 = 100%
𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛

 Percent relative error


𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛 − 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛
𝜀𝑎 = 100%
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛
 Pre-specified Percent Tolerance (εs)

𝜀𝑎 < 𝜀𝑠

𝜀𝑠 = 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%

𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛 − 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛


𝜀𝑎 = 100%
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛
1.5 − 1
𝜀𝑎 = 100% = 33.3%
1.5
Terms Result εt ( % ) εa ( % )
1 1 39.3
2 1.5 9.02 33.3
3 1.625 1.44 7.69
4 1.645833333 0.175 1.27
5 1.648437500 0.0172 0.158
6 1.648697917 0.0014 0.0158

After sixth terms are included, the approximation


error falls below εs=0.05%
 Determine the number of terms necessary to
approximate cos x to 8 significant figures
using the Maclaurine series approximation.
Calculate the approximation using a value of
x=0.3π.

𝑥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)

Compute the true and approximate percent


relative errors. Add terms until the absolute value
of the approximate error estimate falls below an
error criterion conforming to 5 significant figures
 Determine the number of terms necessary to
approximate sin x to 8 significant figures
using the McLaurin series approximation.
Calculate the approximation using a value of
x=0.2π.

You might also like