Lecture 4 - Numerical Errors
description
Transcript of Lecture 4 - Numerical Errors
![Page 1: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/1.jpg)
Lecture 4 - Numerical ErrorsLecture 4 - Numerical Errors
CVEN 302
June 10, 2002
![Page 2: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/2.jpg)
Lecture’s GoalsLecture’s Goals
• Understanding Computer Errors
• Uncertainty in Data and Input
• Well-defined problem
• Numerical Errors– Round-off Error– Truncation Error
![Page 3: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/3.jpg)
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.
![Page 4: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/4.jpg)
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.
![Page 5: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/5.jpg)
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’
![Page 6: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/6.jpg)
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
![Page 7: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/7.jpg)
Unavoidable Errors in ComputingUnavoidable Errors in Computing
• Numerical Errors are based on the mathematics of the problem.
– Round-off Errors
– Truncation Errors
![Page 8: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/8.jpg)
Numerical ErrorsNumerical Errors
Round-off errors occur in computer calculation whenever digits to the right of the decimal are discarded.
![Page 9: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/9.jpg)
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 ????????
![Page 10: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/10.jpg)
Numerical ErrorsNumerical Errors
Truncation error is introduced whenever a number computational uses a formula involving discrete values.
![Page 11: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/11.jpg)
Numerical ErrorsNumerical Errors
Example:
> x = tan(pi/6)> y = sin(pi/6)/cos(pi/6)
where,
> x - y = 1.1102e-16 ????????
![Page 12: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/12.jpg)
Computer ErrorsComputer Errors
Why do you want to know about errors in
computer programs?
To recognize what is a good algorithm!
![Page 13: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/13.jpg)
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
![Page 14: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/14.jpg)
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)
![Page 15: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/15.jpg)
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
![Page 16: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/16.jpg)
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.
![Page 17: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/17.jpg)
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
![Page 18: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/18.jpg)
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.
![Page 19: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/19.jpg)
Measuring ErrorsMeasuring Errors
Absolute Error
Eabs = | x - xtrue |
Relative Error
Erel = | x - xtrue | / | xref |
![Page 20: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/20.jpg)
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)
![Page 21: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/21.jpg)
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.
![Page 22: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/22.jpg)
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.
![Page 23: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/23.jpg)
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.
![Page 24: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/24.jpg)
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)!)
![Page 25: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/25.jpg)
expSeriesPlotexpSeriesPlot
The program shows how the solution
converges on the true value of exp(x) as a
series expansion.
exp(x) = 1 + ( xn / n!)
![Page 26: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/26.jpg)
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
![Page 27: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/27.jpg)
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
![Page 28: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/28.jpg)
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
![Page 29: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/29.jpg)
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.
![Page 30: Lecture 4 - Numerical Errors](https://reader036.fdocuments.in/reader036/viewer/2022081513/5681439a550346895db017fb/html5/thumbnails/30.jpg)
HomeworkHomework
• Modify demoTaylor program