mathematical principles for scientific computing and

22
Mathematical Principles for Scientific Computing and Visualization Chapter 2: Computational Basics Gerald Farin & Dianne Hansford CRC Press, Taylor & Francis Group, An A K Peters Book www.farinhansford.com/books/scv c 2008 Farin & Hansford Scientific Computing and Visualization 1 / 22

Upload: others

Post on 02-Oct-2021

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mathematical Principles for Scientific Computing and

Mathematical Principles for

Scientific Computing and Visualization

Chapter 2: Computational Basics

Gerald Farin & Dianne Hansford

CRC Press, Taylor & Francis Group, An A K Peters Bookwww.farinhansford.com/books/scv

c©2008

Farin & Hansford Scientific Computing and Visualization 1 / 22

Page 2: Mathematical Principles for Scientific Computing and

Outline

1 Introduction to Computational Basics

2 Algorithms

3 Floating Point Numbers

4 Errors

5 Case Study: 1991 Scud Attack

Farin & Hansford Scientific Computing and Visualization 2 / 22

Page 3: Mathematical Principles for Scientific Computing and

Computational Basics

Scientific Computing / Computational Science

solves scientific problems by

• Constructing good mathematical models

• Using efficient computational tools

Key:

• Algorithms• Floating point numbers

Farin & Hansford Scientific Computing and Visualization 3 / 22

Page 4: Mathematical Principles for Scientific Computing and

Algorithms: Defintion

Algorithm = set of instructions to solve a problem

• unambiguous

• terminates

• complete

• provable

Ambiguous sentence fun: Link

Ambiguous “Dangling else”

if (a > b) then if (c > d) then print(“1”) else print(“2”)

Farin & Hansford Scientific Computing and Visualization 4 / 22

Page 5: Mathematical Principles for Scientific Computing and

Algorithms: Errors → dangerous consequences

WW III Almost:1980 NORAD software reported US under attackCause: Algorithm didn’t cover case of faulty circuit

Undetected Exposure:NASA software missed hole in ozone layer for possibly 5 yearsCause: Algorithm did not report outlier data

Crash and Burn:1999 Mars Climate Orbiter flew too close to Mars and disintegrated,costing $378 M and 1 year of travelCause: incorrect mix of metric and English units

Deadly Radiation Therapy:Therac-25 delivered up to 100x too much radiation, killing 3 peopleCause: Bug in user-interaction software(software testers did not use software as technicians)

More examples: Link

Farin & Hansford Scientific Computing and Visualization 5 / 22

Page 6: Mathematical Principles for Scientific Computing and

Algorithm Complexity: Big-O

Worst case execution time (or space)

Function of the amount of input data n

O(1) Access array index

O(logn) Binary search

O(n) Traverse array

O(nlogn) Merge sort

O(n2) Traverse a 2D arrray

O(n3) Solve n× n linear system

O(2n) Recursive Fibonacci (exponential)

See AlgorithmComplexity.nb for plots

Farin & Hansford Scientific Computing and Visualization 6 / 22

Page 7: Mathematical Principles for Scientific Computing and

Algorithm Complexity: O(logn)Binary Search: O(logn)

Where does the log come from?

Problem: Find the position of a target value in a sorted array of n values

Example: Find 12 in the array

1 3 4 6 7 9 10 12 15

Let x be the number of times the array is divided until down to 1 element

n

2x= 1

2x = n

log2(2x ) = log2(n)

x = log(n)

Farin & Hansford Scientific Computing and Visualization 7 / 22

Page 8: Mathematical Principles for Scientific Computing and

Algorithm Complexity: Hard Problems

Hard problems: efficient algorithms do not exist

Hard 6= Poorly designed algorithm

Not hard ≡ bounded by a polynomial

Not hard: Solve n linear equations with n unknownsGauss elimination with n3 computations

Hard: Solve nonlinear system of equationsNo bound on the number of computations

Hard: How many shapes are able to tile the plane?15th convex pentagon found in 2015 Link

Approximate solutions used to solve hard problems• Hueristics: greedy algorithms, e.g, Bees’ garden rounds• Linearization: piecewise linear, e.g., FEM

Farin & Hansford Scientific Computing and Visualization 8 / 22

Page 9: Mathematical Principles for Scientific Computing and

Algorithmic Advances

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

1617 18

19

20

Voronoi diagram of a point set

Innovative techniques andNew mathematical machinery

Example: Computer simulation of acluster of 1000 stars

For each time step:

Attraction of each pair⇒ 999,000 computations

Instead: neighboring stars only:

6 neighbors/star average

6 attractions × 1000 stars⇒ 6000 computations

Farin & Hansford Scientific Computing and Visualization 9 / 22

Page 10: Mathematical Principles for Scientific Computing and

Floating Point Numbers I

IEEE standard forsingle precision (“float”):

±0.x1x2x3 . . . x9 ± e1e2 range: 10±38

sign, significand/mantissa, exponent

double precision:

±0.x1x2x3 . . . x16 ± e1e2e3 range: 10±308

Advantages of FPN:

Omit leading/trailing zeros (scientific notation)

Extend range of numbers representable

Maximum precision

Farin & Hansford Scientific Computing and Visualization 10 / 22

Page 11: Mathematical Principles for Scientific Computing and

Floating Point Numbers II

FPN approximates a real number

• significand provides precision

• exponent provides range

Fixed point 32 bit number: 231 − 1 ≈ 109 max value

Floating point 32 bit number:

1 sign bit + 23 significand bits + 8 exponent bits

8 exponent bits: 28/2 → range [−128, 127]

2128 ≈ 1038 ⇒ max value ≈ 1038

Floating point 64 bit number: 1 sign + 52 sig + 11 exprange [−1022, 1023] ⇒ max value ≈ 10308

Farin & Hansford Scientific Computing and Visualization 11 / 22

Page 12: Mathematical Principles for Scientific Computing and

Floating Point Numbers III

Significand stored in powers of 2−i

2−1 = 0.5 2−2 = 0.25 2−3 = 0.125

Only a finite number of real numbers are representable!

−10308 −10−308 0 10−308 10308overflow usable underflow usable overflow

Zoom in on usable range: representable numbers not uniformly distributed|||| | | | | | | | | | |

“single precision”: 32 bit with 7 (ave) significant digits“double precision”: 64 bit with 16 (ave) significant digits

Resource: IEEE.org See number-rep.nb

Farin & Hansford Scientific Computing and Visualization 12 / 22

Page 13: Mathematical Principles for Scientific Computing and

Floating Point Numbers IV

Storage and manipulation of FPNs requires computing power

Special chip: Floating Point Unit (FPU) – “math coprocessor”

IEEE standards ensure program transportability and consistency of resultsfrom machine to machine

Supercomputers simulating complex physical phenomena– Billions of variables

FLOPS = Floating Point Operation Per Second

June 2016: fastest at 93 PFLOPS (Peta FLOPS = 1015 FLOPS)

Farin & Hansford Scientific Computing and Visualization 13 / 22

Page 14: Mathematical Principles for Scientific Computing and

Floating Point Errors

Finite number of digits

0.1E1 + 0.1E − 20 = 0.1E1

Non-associativity of addition:(x + y) + z 6= x + (y + z)

(See number-rep.nb)

Farin & Hansford Scientific Computing and Visualization 14 / 22

Page 15: Mathematical Principles for Scientific Computing and

Floating Point Errors

Never usex == y

Instead use a tolerance

abs(x − y) < 0.5E − 08

Farin & Hansford Scientific Computing and Visualization 15 / 22

Page 16: Mathematical Principles for Scientific Computing and

Truncation Error

-6 -4 -2 2 4 6

-1.0

-0.5

0.5

1.0

Approximation with five terms

sin x =

∞∑

i=1

(−1)i−1

(2i − 1)!x2i−1

See TruncationError.nb

Farin & Hansford Scientific Computing and Visualization 16 / 22

Page 17: Mathematical Principles for Scientific Computing and

Cancellation Error

-10 -5 5 10

0.1

0.2

0.3

0.4

0.5

-4.´10-8-2.´10-8 2.´10-8 4.´10-8

0.2

0.4

0.6

0.8

1.0

f (x) =1− cos x

x2

See Cancellation subtractive.nb

Farin & Hansford Scientific Computing and Visualization 17 / 22

Page 18: Mathematical Principles for Scientific Computing and

Floating Point Errors

Decimal ≃ Binary2 ≃ 103 ≃ 119 ≃ 10010.5 ≃ 0.1

0.1 ≃ 0× 1/2 + 0× 1/4 + 0× 1/8 + 1× 1/16 + 1× 1/32 + 0× 1/64 +0× 1/128 + 1× 1/256 + +1× 1/512 + . . .

= .000110011 . . .Non-terminating binary expansion

Conversion / truncation error: 0.1 × 10 6= 1

Result depends on precision

Farin & Hansford Scientific Computing and Visualization 18 / 22

Page 19: Mathematical Principles for Scientific Computing and

Case Study: 1991 Scud Attack

1st Gulf war 1991

Iraq attacking US troopsusing Scud Missiles

US defense: Patriot missiles

Farin & Hansford Scientific Computing and Visualization 19 / 22

Page 20: Mathematical Principles for Scientific Computing and

Patriot Clock

Start: 0 seconds

Tracking computer’s time counter: integer units indicating 1/10 second

Time converted to 24 bit fixed point binary

Since 1/10 has a non-terminating binary expansion

⇒ each time increment off by 9.5E-8 sec (times reduced by 0.0001%)

Farin & Hansford Scientific Computing and Visualization 20 / 22

Page 21: Mathematical Principles for Scientific Computing and

Patriot Tracking

Figure credit: University of Sydney

Object detection(position + velocity)

Position prediction after 1 second– Based on ballistic trajectory

Object at predicted position? Fire!

Original system built in early 1970s

System updated for Gulf War to handle ballistic technology

System updated to 48 bit floating point

Problem: Error in software update – not all clocks updated

Farin & Hansford Scientific Computing and Visualization 21 / 22

Page 22: Mathematical Principles for Scientific Computing and

Scud Disaster

Patriot system up for 100 hours

Timing off by 0.34 seconds

100 hr × 60 min/hr × 60 sec/min × 10 inc/sec = 3,600,000 inc

Error = 9.5E-8 sec × 3.6E6 = 0.34 sec

Scud traveling approx. 1600 meters/sec

⇒ Scud position missed by 500+ meters

Patriot not fired

28 fatalities and 100 injuries

Source: Robert Skeel, Roundoff Error and the Patriot Missile, SIAM News,Volume 25, No. 4, page 11, July 1992.

Farin & Hansford Scientific Computing and Visualization 22 / 22