section 4 roundoff and truncation errorweb.engr.oregonstate.edu/~webbky/mae4020_5020_files/section 4...

41
MAE 4020/5020 – Numerical Methods with MATLAB SECTION 4: ROUNDOFF AND TRUNCATION ERRORS

Upload: others

Post on 20-Apr-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

MAE 4020/5020 – Numerical Methods with MATLAB

SECTION 4: ROUNDOFF AND TRUNCATION ERRORS

Page 2: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

Definitions of Error2

K. Webb  MAE 4020/5020

Page 3: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

3

True Error

K. Webb  MAE 4020/5020

Absolute error – the difference between an approximation and the true value

Relative error – the true error as a percentage of the true value

Both definitions require knowledge of the true value! If we had that, why would we be approximating?

Page 4: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

4

Approximating the Error

K. Webb  MAE 4020/5020

Since we don’t know the true value, we can only approximate the error

Often, approximations are made iteratively Approximate the error as the change in the approximate value from one iteration to the next

Page 5: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

5

Relative Approximate Error

K. Webb  MAE 4020/5020

We don’t know the true value, so we can’t calculate the true error – approximate the error

Relative approximate error – an approximation of the error relative to the approximation itself

Page 6: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

6

Stopping Criterion

K. Webb  MAE 4020/5020

For iterative approximations, continue to iterate until the relative approximate error magnitude is less than a specified stopping criterion

For accuracy to at least  significant figures set the stopping criterion to

Page 7: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

Roundoff Error7

K. Webb  MAE 4020/5020

Page 8: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

8

Roundoff Errors

K. Webb  MAE 4020/5020

Roundoff errors occur due to the way in which computers represent numerical values

Computer representation of numerical values is limited in terms of:Magnitude – there are upper and lower bounds on the magnitude of numbers that can be represented

Precision – not all numbers can be represented exactly

Certain types of mathematical manipulations are more susceptible to roundoff error than others

Page 9: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

9

Number Systems – Decimal 

K. Webb  MAE 4020/5020

We are accustomed to the decimal number system A base‐10 number system Ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Each digit represents an integer power of 10

7281.366 x 10‐2

3 x 10‐1

1 x 10‐0

8 x 101

2 x 102

7 x 103

Page 10: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

10

Binary Number System

K. Webb  MAE 4020/5020

Computers represent numbers in binary format A base‐2 number system Two digits: 0, 1

Easy to store binary values in computer hardware – an on or off switch – a  high or low voltage

One digit is a bit – eight bits is a byte Each bit represents an integer power of 2

101101011 x 20 = 10 x 21 = 01 x 22 = 40 x 23 = 01 x 24 = 161 x 25 = 320 x 26 = 01 x 27 = 128

= (181)10

Page 11: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

11

IEEE Double‐Precision Format

K. Webb  MAE 4020/5020

By default, MATLAB uses double‐precision floating point to store numeric values ‐ double 64‐bit binary word

52 bits11 bits

Mantissa – (f)Signed 

Exponent – (e)Sign bit

Page 12: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

12

IEEE Double‐Precision Format

K. Webb  MAE 4020/5020

Mantissa Only the fractional portion of the mantissa stored Bit to the left of the binary point assumed to be 1 Normalized numbers Really a 53‐bit value

Exponent 11‐bit signed integer value: ‐1022 … 1023 Two special cases: e = 0x000 (i.e. all zeros): zero if f = 0, subnormal #’s if f 0 e = 0x7FF (i.e. all ones): ∞ if f = 0, NaN if f 0

Page 13: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

13

Normalized numbers

K. Webb  MAE 4020/5020

Leading zeros are removedMost significant digit (must be a 1 in binary) moved to the left of the binary point

53rd bit of the mantissa (always 1) needn’t be stored

Maximum mantissa

1.111……11

Not stored

Page 14: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

14

Subnormal Numbers

K. Webb  MAE 4020/5020

If  , always, then the smallest number that could be represented is: 2 .

If we allow for  , then the most significant bit is somewhere to the right of the binary point Leading zeros – not normalized … subnormal Allows for smaller numbers, filling in the hole around zero

Subnormal numbers represented by setting the exponent to zero

Smallest subnormal number:2 2

Page 15: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

15

Doubles – Range 

K. Webb  MAE 4020/5020

Maximum value:

1 2 2 .

Minimum normal value:2 .

Minimum subnormal value:2 2

Precision –machine epsilon.

Page 16: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

16

Roundoff Error – Mathematical Operations

K. Webb  MAE 4020/5020

Certain types of mathematical operations are more susceptible to roundoff errors: Subtractive cancellation – subtracting of two nearly‐equal numbers results in a loss of significant digits

Large computations – even if the roundoff error from a single operation is small, the cumulative error from many operations may be significant

Adding large and small numbers – as in an infinite series

Inner products – (i.e. dot product) very common operation – solution of linear systems of equations

Page 17: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

Truncation Error17

K. Webb  MAE 4020/5020

Page 18: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

18

Truncation Errors

K. Webb  MAE 4020/5020

Errors that result from the use of an approximation in place of an exact mathematical procedure E.g. numerical integration, or the approximation derivatives with finite‐difference approximations

To understand how truncation errors arise, and to gain an understanding of their magnitudes, we’ll make use of the Taylor Series

Page 19: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

19

Taylor Series

K. Webb  MAE 4020/5020

Taylor’s Theorem – any smooth (i.e., continuously differentiable) function can be approximated as a polynomial

Taylor Series

This infinite series is an equality An exact representation of any smooth function as a polynomial

An infinite‐order polynomial – impractical

Page 20: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

20

Taylor Series Approximation

K. Webb  MAE 4020/5020

Can approximate a function as a polynomial by truncatingthe Taylor series after a finite number of terms

2! ⋯ !where  is the step size

Chapra

Page 21: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

21

Taylor Series Truncation Error

K. Webb  MAE 4020/5020

Can account for error by lumping the  and higher‐order terms into a single term, 

2! ⋯ !

is the error associated with truncating after  terms

1 !

is some (unknown) value of  between  and 

Page 22: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

22

Derivative Mean‐Value Theorem

K. Webb  MAE 4020/5020

If  and  are continuous on  , then there is a point on this interval,  , where  is the slope of the line joining  and 

Chapra

Page 23: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

23

Truncation Error – Dependence on Step Size

K. Webb  MAE 4020/5020

1 !

We don’t know  , so we don’t know We do know it’s proportional to  , where  is the step size

Error is on the order of 

If  (first‐order approx.), halving the step size will quarter the error

Page 24: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

• Discretizing equations• Finite‐difference approximations

Truncation Errors in Practice24

K. Webb  MAE 4020/5020

Page 25: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

25

Discretization of Equations

K. Webb  MAE 4020/5020

As engineers, many of the mathematical expressions we are interested in are differential equationsWe know how to evaluate derivatives analytically Need an approximation for the derivative operation in order to solve numerically

Discretization – conversion of a continuous function, e.g. differentiation, to a discrete approximation for numerical evaluation

Page 26: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

26

Finite Difference Approximations

K. Webb  MAE 4020/5020

Recall the definition of a derivative

lim→

Remove the limit to  approximate this numerically

This is the forward difference approximation Uses value at  and forward one step at  to approximate the derivative at 

Page 27: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

27

Discretizing Equations – Example 

K. Webb  MAE 4020/5020

A free‐falling object (bungee jumper example from the text) can be modeled as 

where  is velocity,  is mass,  is gravitational acceleration, and   is a lumped drag coefficient

This is a non‐linear ordinary differential equation (ODE), which can be solved analytically to yield

tanh ∙

Page 28: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

28

Discretizing Equations – Example 

K. Webb  MAE 4020/5020

To solve numerically instead, approximate the derivative operation with a finite difference

Solving for  and using  to denote the time step yields

We’ve transformed the differential equation to a difference equation An algebraic equation Can be solved iteratively – using a loop

Page 29: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

29

Discretizing Equations – Example 

K. Webb  MAE 4020/5020

The term in the square brackets is the original diff. eq., i.e. it is 

The difference equation is a first‐order Taylor series approximation

Where we know that the error is on the order of the step size squared

Taylor series provides a relation between the step size and the accuracy of the numerical solution to the diff. eqn.

Page 30: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

30

Finite Difference Methods

K. Webb  MAE 4020/5020

The preceding example showed One method – forward difference – for numerically approximating a derivative

Transformation of a differential equation to a difference equation

How Taylor series can provide an understanding of the error associated with an approximation

Now we’ll take a closer look at three finite difference methods and how Taylor series can help us understand the error associated with each

Page 31: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

31

Forward Difference

K. Webb  MAE 4020/5020

Can also derive the forward difference approximation from the Taylor Series

Solving for 

We’ve already seen that

So, the error term is

The forward difference, including error, is

Error of the forward difference approximation is on the order of the step size

Page 32: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

32

Forward Difference

K. Webb  MAE 4020/5020

Value of the function, at  and forward one step at used to approximate the derivative at 

Chapra

Page 33: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

33

Backward Difference

K. Webb  MAE 4020/5020

Backward difference uses value of at  and one step backward at to approximate the derivative at 

This can also be developed by expanding the Taylor series backward

Then solving for 

Again the error is on the order of the step size

The backward difference expression, including error, becomes

Error of the backward difference approximation is on the order of the step size

Page 34: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

34

Backward Difference

K. Webb  MAE 4020/5020

Now use the value of at  and backward one step at  to approximate the derivative at 

Again, error isChapra

Page 35: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

35

Central Difference

K. Webb  MAE 4020/5020

Central difference uses value of one step backward at  and ones step ahead at  to approximate the derivative at 

2 This can also be developed by 

subtracting  the backward Taylor series from the forward series

Second‐order derivative terms cancel, leaving

2

Now, the remainder term is

The central difference expression, including error, becomes

2

Error of the central difference approximation is on the order of the step size squared

Central difference method is more accurate than forward or backward Uses more information

Page 36: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

36

Central Difference

K. Webb  MAE 4020/5020

Now use the value of backward one step at  and forward one step at  to approximate the derivative at 

Reduced error:Chapra

Page 37: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

Total Numerical Error37

K. Webb  MAE 4020/5020

Page 38: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

38

Total Numerical Error

K. Webb  MAE 4020/5020

Total numerical error is the sum of roundoff and truncation error Roundoff error is largely out of your control, and, with double precision arithmetic, it is not typically an issue

Truncation error can be a significant problem, but can be reduced by decreasing step size

Reducing step size reduces truncation error, but may also result in subtractive cancellation, thereby increasing roundoff error

Choose step size to minimize total error Or, more typically, to reduce truncation error to an acceptable level

Page 39: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

39

Total Numerical Error

K. Webb  MAE 4020/5020

Reducing step size reduces truncation error, but may also result in subtractive cancellation, thereby increasing roundoff error

Could choose step size to minimize total error

But, more typically, reduce step size just enough to reduce truncation error to an acceptable level

Chapra

Page 40: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

40

Central Difference Error Analysis

K. Webb  MAE 4020/5020

First derivative of a function in terms of the central difference approximation is

The last term on the right is the truncation error There is also roundoff error associated with each value

were  represents a rounded value, and  is the corresponding roundoff error

Page 41: Section 4 Roundoff and Truncation Errorweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 4 … · Taylor Series K. Webb MAE 4020/5020 Taylor’s Theorem –any smooth (i.e.,

41

Central Difference Error Analysis

K. Webb  MAE 4020/5020

Substituting the expressions for the rounded values into the expression for the true derivative yields

Giving a total error of 

Truncation error increases with step size Roundoff error decreases with step size

Truncation errorRoundoff error