Title: Lecture 4 Numerical Errors
1Lecture 4 - Numerical Errors
2Lectures Goals
- Understanding Computer Errors
- Uncertainty in Data and Input
- Well-defined problem
- Numerical Errors
- Round-off Error
- Truncation Error
3Unavoidable Errors in Computing
- Hardware problems
- Example PentiumTM Chip
Documentation is critical for any code that is
not going to be used and immediately discarded.
Documentation takes the form of comment
statements that describe the input and output
parameters of a function as well as the steps
performed in the analysis.
4Unavoidable Errors in Computing
- Some software bugs are caused by deterministic
errors in the execution of the problem. - Example
- Problems in the built-in functions such as sine
or - cosine and a series of operational commands.
5Matlab Numerical Problems
example gtformat long e gt2.6 0.2 ans
2.800000000000e000 gtans 0.2 ans
3.000000000000e000 gtans 0.2 ans
3.200000000001e000
Note The program
has changed the value of ans
6Matlab Numerical Problems
Example Same method but different
results. gtformat long e gt2.6 0.6 ans
3.200000000000e000 gtans 0.6 ans
3.800000000000e000 gtans 0.6 ans
4.400000000000e000 gtans 0.6 ans 5
7Unavoidable Errors in Computing
- Numerical Errors are based on the mathematics of
the problem. - Round-off Errors
- Truncation Errors
-
8Numerical Errors
Round-off errors occur in computer calculation
whenever digits to the right of the decimal are
discarded.
9Numerical Errors
Example gt b 1/3 gt b
0.333333 gt b3 - 1 0 or gt b 4/3
- 1 gt b 0.33333 gt b3 - 1
-2.2204e-16 ????
10Numerical Errors
Truncation error is introduced whenever a number
computational uses a formula involving discrete
values.
11Numerical Errors
Example gt x tan(pi/6) gt y
sin(pi/6)/cos(pi/6) where, gt x - y
1.1102e-16 ????
12Computer Errors
- Why do you want to know about errors in
- computer programs?
- To recognize what is a good algorithm!
13Digital Representation of Numbers
- Bits, Bytes and Words - the binary language of
the computer programmer, and electrical engineer. - Bit is a single unit of information (0 or 1)
- Byte is a combination of 8 bits
- Word is 32, 64, or 128 bit pieces of information
14Types of Variables
- Integer - two types a regular (16 bits) or a long
(32 bits) - Float - two types a single precision (32 bits)
and double precision(64 bits) - Complex - two single precision real numbers (64
bits)
15Numerical Errors
- The type of variable determines the size of the
under and overflow limits. - Underflow is the lowest value the computer can
reach without major problems. - Overflow is the highest value the computer can
reach without major problems. - Matlab commands to see the limits are realmax or
realmin
16Example Float Point
- The program halfDif (x,y)
- Shows how the numbers converge by halving
- the difference and at what point does the delta
- term becomes insignificant.
17Examples Finite Precision Arithmetic
- An earlier example was the difference between a
using 1/3 and 4/3 to show round-off errors. - An example program (epprox) shows the convergence
on the exponential term of 1. - exp(1) 1 (1/n) n
18Example epprox
- The program has 2 round-off errors. The first
error is a relatively minor one and second is
catastrophic. - The minor error is due to the inability to
exactly represent 1/n with powers of 2. - The major round-off error occurs due to round-off
of (1 1/n) term at a high power.
19Measuring Errors
- Absolute Error
- Eabs x - xtrue
- Relative Error
- Erel x - xtrue / xref
20Convergence of Iterative Sequences
- Using the example problem we will look at four
test - cases for convergence for newtsqrt (program)
Change - NOT_CONVERGED change to rrold (comparison)
- NOT_CONVERGED change to (r - rold)gtd (error)
- NOT_CONVERGED change to
- abs(r-rold)/rold gtdabs (absolute error)
- NOT_CONVERGED change to
- abs((r-rold)/rold)gtdrel (relative error)
21Testing example newtsqrt.m
- The program newtsqrt has a driver testSqrt
- program. The program will input data into the
- function and compare the results with the
- actual results.
22Truncation Error in Algorithms
- Truncation error results from approximating
- continuous mathematical expressions with
- discrete algebraic formulas. Unlike round-off,
- which is controlled by the hardware and
- computer language being used, truncation
- error is under the control of the programmer.
23Set of examples of truncation errors
- Sinser.m is a program for calculating sin(x)
using a series expansion. - expSeriesPlot shows the convergence of the
absolute error for an exp() as a series
expansion. - demoTaylor shows a Taylor Series expansion for a
simple equation with a variable delta step and
different number of derivatives. - fidiff is a finite difference program to test
both round-off and truncation errors.
24Sinser Example
- The program examines how the addition of
- more terms helps the sinusoid to converge .
- sin(x) S (-1)k-1 (x2k-1 /(2k-1)!)
25expSeriesPlot
- The program shows how the solution
- converges on the true value of exp(x) as a
- series expansion.
- exp(x) 1 S ( xn / n!)
26Taylor Series
- A series of Taylor series are used to represent
- the function at a location with each series
- containing an addition derivative. The
- function is
-
27demoTaylor
- The program looks at successive derivatives.
28Round-off and Truncation with Finite Difference
The program fidiff(x) is a simple finite
difference program to calculate the first order
derivative of an exponential function with
variable stepsize.
29Accuracy and Precision
- Accuracy - How closely a measured or computed
value agrees with the true value - Precision - How closely individual measured or
computed values agree with each other
Precision is getting them close together.
Accuracy is getting all your shots near the
target.
30Homework
- Modify demoTaylor program