Upload
clement-brooks
View
224
Download
1
Embed Size (px)
Citation preview
Lecture 4 - Numerical ErrorsLecture 4 - Numerical Errors
CVEN 302
June 10, 2002
Lecture’s GoalsLecture’s Goals
• Understanding Computer Errors
• Uncertainty in Data and Input
• Well-defined problem
• Numerical Errors– Round-off Error– Truncation Error
Unavoidable Errors in ComputingUnavoidable 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.
Unavoidable Errors in ComputingUnavoidable 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.
Matlab Numerical ProblemsMatlab Numerical Problems
example>format long e>2.6 + 0.2
ans = 2.800000000000e+000>ans + 0.2
ans = 3.000000000000e+000>ans + 0.2
ans = 3.200000000001e+000 ^Note The program has changed the value of ‘ans’
Matlab Numerical ProblemsMatlab Numerical Problems
ExampleSame method but different results.
>format long e>2.6 + 0.6
ans = 3.200000000000e+000>ans + 0.6
ans = 3.800000000000e+000>ans + 0.6
ans = 4.400000000000e+000>ans + 0.6
ans = 5
Unavoidable Errors in ComputingUnavoidable Errors in Computing
• Numerical Errors are based on the mathematics of the problem.
– Round-off Errors
– Truncation Errors
Numerical ErrorsNumerical Errors
Round-off errors occur in computer calculation whenever digits to the right of the decimal are discarded.
Numerical ErrorsNumerical Errors
Example:> b = 1/3> b = 0.333333> b*3 - 1 = 0
or> b =4/3 - 1> b = 0.33333> b*3 - 1 = -2.2204e-16 ????????
Numerical ErrorsNumerical Errors
Truncation error is introduced whenever a number computational uses a formula involving discrete values.
Numerical ErrorsNumerical Errors
Example:
> x = tan(pi/6)> y = sin(pi/6)/cos(pi/6)
where,
> x - y = 1.1102e-16 ????????
Computer ErrorsComputer Errors
Why do you want to know about errors in
computer programs?
To recognize what is a good algorithm!
Digital Representation of NumbersDigital 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
Types of VariablesTypes 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)
Numerical ErrorsNumerical 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
Example: Float PointExample: 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.
Examples: Finite Precision ArithmeticExamples: 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
Example: epproxExample: 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.
Measuring ErrorsMeasuring Errors
Absolute Error
Eabs = | x - xtrue |
Relative Error
Erel = | x - xtrue | / | xref |
Convergence of Iterative SequencesConvergence of Iterative Sequences
Using the example problem we will look at four test
cases for convergence for newtsqrt (program) Change:
– NOT_CONVERGED change to r~=rold (comparison)– NOT_CONVERGED change to (r - rold)> (error)– NOT_CONVERGED change to
abs(r-rold)/rold >abs (absolute error)
– NOT_CONVERGED change to
abs((r-rold)/rold)>rel (relative error)
Testing example: newtsqrt.mTesting 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.
Truncation Error in AlgorithmsTruncation 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.
Set of examples of truncation errorsSet 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.
Sinser ExampleSinser Example
The program examines how the addition of
more terms helps the sinusoid to converge .
sin(x) = (-1)k-1 (x2k-1 /(2k-1)!)
expSeriesPlotexpSeriesPlot
The program shows how the solution
converges on the true value of exp(x) as a
series expansion.
exp(x) = 1 + ( xn / n!)
Taylor SeriesTaylor 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:
x1
1xf
demoTaylordemoTaylor
The program looks at successive derivatives.
0
3
0
2
003
0
2
002
0001
00
*!3
*!2
*
*!2
*
where, *
1
1
xfh
xfh
xfhxfxP
xfh
xfhxfxP
x -xΔhxfhxfxP
xxf
Round-off and Truncation with Round-off and Truncation with Finite DifferenceFinite Difference
The program fidiff(x) is a simple finite difference program to calculate the first order derivative of an exponential function with variable stepsize.
dh
xfdhxf
dx
xexpd
Accuracy and PrecisionAccuracy 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
More More AccurateAccurate
More More PrecisePrecise
Precision is getting them close together.
Accuracy is getting all your shots near the target.
HomeworkHomework
• Modify demoTaylor program