850 likes | 1.84k Views
Lecture 4 - Numerical Errors. CVEN 302 June 10, 2002. Lecture’s Goals. Understanding Computer Errors Uncertainty in Data and Input Well-defined problem Numerical Errors Round-off Error Truncation Error. Unavoidable Errors in Computing. Hardware problems: Example: Pentium TM Chip.
E N D
Lecture 4 - Numerical Errors CVEN 302 June 10, 2002
Lecture’s Goals • Understanding Computer Errors • Uncertainty in Data and Input • Well-defined problem • Numerical Errors • Round-off Error • Truncation Error
Unavoidable 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 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 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 Problems Example Same 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 Computing • Numerical Errors are based on the mathematics of the problem. • Round-off Errors • Truncation Errors
Numerical Errors Round-off errors occur in computer calculation whenever digits to the right of the decimal are discarded.
Numerical 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 Errors Truncation error is introduced whenever a number computational uses a formula involving discrete values.
Numerical Errors Example: > x = tan(pi/6) > y = sin(pi/6)/cos(pi/6) where, > x - y = 1.1102e-16 ????
Computer Errors Why do you want to know about errors in computer programs? To recognize what is a good algorithm!
Digital 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 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 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 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 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: 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.
Absolute Error Eabs = | x - xtrue | Relative Error Erel = | x - xtrue | / | xref | Measuring Errors
Convergence 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)>d (error) • NOT_CONVERGED change to abs(r-rold)/rold >dabs (absolute error) • NOT_CONVERGED change to abs((r-rold)/rold)>drel (relative error)
Testing 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 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 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 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)!)
expSeriesPlot The program shows how the solution converges on the true value of exp(x) as a series expansion. exp(x) = 1 + S ( xn / n!)
Taylor 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:
demoTaylor The program looks at successive derivatives.
Round-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.
More Accurate More Precise Accuracy 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.
Homework • Modify demoTaylor program