Approximate computations

Similar documents
Product of polynomials. Introduction to Programming (in C++) Numerical algorithms. Product of polynomials. Product of polynomials

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

Representation of Numbers. Number Representation. Representation of Numbers. 32-bit Unsigned Integers 3/24/2014. Fixed point Integer Representation

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

Area & Volume. Chapter 6.1 & 6.2 September 25, y = 1! x 2. Back to Area:

Introduction to Integration

6.2 Volumes of Revolution: The Disk Method

Floating Point Numbers and Interval Arithmetic

Questions About Numbers. Number Systems and Arithmetic. Introduction to Binary Numbers. Negative Numbers?

10/9/2012. Operator is an operation performed over data at runtime. Arithmetic, Logical, Comparison, Assignment, Etc. Operators have precedence

What do all those bits mean now? Number Systems and Arithmetic. Introduction to Binary Numbers. Questions About Numbers

Graphing Conic Sections

Explicit Decoupled Group Iterative Method for the Triangle Element Solution of 2D Helmholtz Equations

If f(x, y) is a surface that lies above r(t), we can think about the area between the surface and the curve.

a < a+ x < a+2 x < < a+n x = b, n A i n f(x i ) x. i=1 i=1

Before We Begin. Introduction to Spatial Domain Filtering. Introduction to Digital Image Processing. Overview (1): Administrative Details (1):

Midterm 2 Sample solution

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Systems I. Logic Design I. Topics Digital logic Logic gates Simple combinational logic circuits

MSTH 236 ELAC SUMMER 2017 CP 1 SHORT ANSWER. Write the word or phrase that best completes each statement or answers the question.

Accelerating 3D convolution using streaming architectures on FPGAs

Computing offsets of freeform curves using quadratic trigonometric splines

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

6.3 Volumes. Just as area is always positive, so is volume and our attitudes towards finding it.

Orthogonal line segment intersection

Grade 7/8 Math Circles Geometric Arithmetic October 31, 2012

1. SEQUENCES INVOLVING EXPONENTIAL GROWTH (GEOMETRIC SEQUENCES)

Numerical integration methods

Math 142, Exam 1 Information.

ZZ - Advanced Math Review 2017

Summer Review Packet For Algebra 2 CP/Honors

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence

Section 5.3 : Finding Area Between Curves

Agilent Mass Hunter Software

Double Integrals. MATH 375 Numerical Analysis. J. Robert Buchanan. Fall Department of Mathematics. J. Robert Buchanan Double Integrals

6.3 Definite Integrals and Antiderivatives

Union-Find Problem. Using Arrays And Chains. A Set As A Tree. Result Of A Find Operation

9 Graph Cutting Procedures

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example:

Today. CS 188: Artificial Intelligence Fall Recap: Search. Example: Pancake Problem. Example: Pancake Problem. General Tree Search.

The Fundamental Theorem of Calculus

Lecture 7: Integration Techniques

Slides for Data Mining by I. H. Witten and E. Frank

f[a] x + f[a + x] x + f[a +2 x] x + + f[b x] x

PARALLEL AND DISTRIBUTED COMPUTING

Fault injection attacks on cryptographic devices and countermeasures Part 2

Very sad code. Abstraction, List, & Cons. CS61A Lecture 7. Happier Code. Goals. Constructors. Constructors 6/29/2011. Selectors.

Unit 5 Vocabulary. A function is a special relationship where each input has a single output.

1.5 Extrema and the Mean Value Theorem

Stained Glass Design. Teaching Goals:

Ranking of Hexagonal Fuzzy Numbers for Solving Multi Objective Fuzzy Linear Programming Problem

9.1 apply the distance and midpoint formulas

APPLICATIONS OF INTEGRATION

Lecture Overview. Knowledge-based systems in Bioinformatics, 1MB602. Procedural abstraction. The sum procedure. Integration as a procedure

INTRODUCTION TO SIMPLICIAL COMPLEXES

5/9/17. Lesson 51 - FTC PART 2. Review FTC, PART 1. statement as the Integral Evaluation Theorem as it tells us HOW to evaluate the definite integral

Lily Yen and Mogens Hansen

50 AMC LECTURES Lecture 2 Analytic Geometry Distance and Lines. can be calculated by the following formula:

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

Hyperbolas. Definition of Hyperbola

Thirty-fourth Annual Columbus State Invitational Mathematics Tournament. Instructions

ASTs, Regex, Parsing, and Pretty Printing

Math 464 Fall 2012 Notes on Marginal and Conditional Densities October 18, 2012

MA 124 (Calculus II) Lecture 2: January 24, 2019 Section A3. Professor Jennifer Balakrishnan,

Efficient K-NN Search in Polyphonic Music Databases Using a Lower Bounding Mechanism

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

Matlab s Numerical Integration Commands

Engineer To Engineer Note

Simplifying Algebra. Simplifying Algebra. Curriculum Ready.

Data-Types Optimization for Floating-Point Formats by Program Transformation

6/23/2011. Review: IEEE-754. CSE 2021: Computer Organization. Exercises. Examples. Shakil M. Khan (adapted from Profs. Roumani & Asif)

B. Definition: The volume of a solid of known integrable cross-section area A(x) from x = a

Lexical Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Math/CS 467/667 Programming Assignment 01. Adaptive Gauss Quadrature. q(x)p 4 (x) = 0

Qubit allocation for quantum circuit compilers

Basics of Logic Design Arithmetic Logic Unit (ALU)

MATH 25 CLASS 5 NOTES, SEP

Recursive Spoke Darts: Local Hyperplane Sampling for Delaunay and Voronoi Meshing in Arbitrary Dimensions

Solutions to Math 41 Final Exam December 12, 2011

Complete Coverage Path Planning of Mobile Robot Based on Dynamic Programming Algorithm Peng Zhou, Zhong-min Wang, Zhen-nan Li, Yang Li

Parallel Square and Cube Computations

Mathematics Interventi. and Focused Mathematics Booster Packs Alignment to Eureka Math and Common Core State Standards

Chapter 2 Sensitivity Analysis: Differential Calculus of Models

Performance enhancement of IEEE DCF using novel backoff algorithm

Dynamic Programming. Andreas Klappenecker. [partially based on slides by Prof. Welch] Monday, September 24, 2012

The notation y = f(x) gives a way to denote specific values of a function. The value of f at a can be written as f( a ), read f of a.

TOWARDS GRADIENT BASED AERODYNAMIC OPTIMIZATION OF WIND TURBINE BLADES USING OVERSET GRIDS

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications.

Solution of Linear Algebraic Equations using the Gauss-Jordan Method

ScienceDirect. Recursive Spoke Darts: Local Hyperplane Sampling for Delaunay and Voronoi Meshing in Arbitrary Dimensions

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers?

Final Exam Review F 06 M 236 Be sure to look over all of your tests, as well as over the activities you did in the activity book

8.2 Areas in the Plane

Topic: Software Model Checking via Counter-Example Guided Abstraction Refinement. Having a BLAST with SLAM. Combining Strengths. SLAM Overview SLAM

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. General Tree Search. Uniform Cost. Lecture 3: A* Search 9/4/2007

AI Adjacent Fields. This slide deck courtesy of Dan Klein at UC Berkeley

MATH 2530: WORKSHEET 7. x 2 y dz dy dx =

Lexical analysis, scanners. Construction of a scanner

Engineer To Engineer Note

Transcription:

Living with floting-point numers Stndrd normlized representtion (sign + frction + exponent): Approximte computtions Rnges of vlues: Representtions for:, +, +0, 0, NN (not numer) Jordi Cortdell Deprtment of Computer Science Be creful when operting with rel numers: doule x, y; cin >> x >> y; // 1.1 3.1 cout.precision(20); cout << x + y << endl; // 4.2000000000000001776 Single nd doule-precision FP numers flot: doule: 1 sign 1. 51 50 0 2 e 1023 or 1 sign 52 1 + 52 i 2 i 2 e 1023 i=1 Introduction to Progrmming Dept. CS, UPC 2 Compring floting-point numers Comprisons: = + c; if ( == c) Allow certin tolernce for equlity comprisons: if (expr1 == expr2) // Wrong! // my e flse if (s(expr1 expr2) < 0.000001) // Ok! Not every numer cn e represented. Exmple: 0.15 is stored s 0.149999999999999994448884876874 Introduction to Progrmming Dept. CS, UPC 3 Introduction to Progrmming Dept. CS, UPC 4

Root of continuous function Root of continuous function Bolzno s theorem: Let f e rel-vlued continuous function. Let nd e two vlues such tht < nd f() f() < 0. Then, there is vlue c [,] such tht f(c)=0. Design function tht finds root of continuous function f in the intervl [, ] ssuming the conditions of Bolzno s theorem re fulfilled. Given precision ( ), the function must return vlue c such tht the root of f is in the intervl [c, c+ ]. c c Introduction to Progrmming Dept. CS, UPC 5 Root of continuous function Strtegy: nrrow the intervl [, ] y hlf, checking whether the vlue of f in the middle of the intervl is positive or negtive. Iterte until the width of the intervl is smller. Introduction to Progrmming Dept. CS, UPC 6 Root of continuous function // Pre: f is continuous, < nd f()*f() < 0. // Returns c [,] such tht root exists in the // intervl [c,c+ ]. // Invrint: root of f exists in the intervl [,] Introduction to Progrmming Dept. CS, UPC 7 Introduction to Progrmming Dept. CS, UPC 8

Root of continuous function Root of continuous function doule root(doule, doule, doule epsilon) { while ( > epsilon) { doule c = ( + )/2; if (f() f(c) <= 0) = c; else = c; return ; // A recursive version doule root(doule, doule, doule epsilon) { if ( <= epsilon) return ; doule c = ( + )/2; if (f() f(c) <= 0) return root(,c,epsilon); else return root(c,,epsilon); Introduction to Progrmming Dept. CS, UPC 9 The Newton-Rphson method Introduction to Progrmming Dept. CS, UPC 10 The Newton-Rphson method A method for finding successively pproximtions to the roots of rel-vlued function. The function must e differentile. Introduction to Progrmming Dept. CS, UPC 11 Introduction to Progrmming Dept. CS, UPC 12

The Newton-Rphson method Squre root (using Newton-Rphson) Clculte Find the zero of the following function: where Recurrence: source: http://en.wikipedi.org/wiki/newton s_method Introduction to Progrmming Dept. CS, UPC 13 Squre root (using Newton-Rphson) // Pre: >= 0 // Returns x such tht x 2 - < doule squre_root(doule ) { doule x = 1.0; // Mkes n initil guess // Itertes using the Newton-Rphson recurrence while (s(x x ) >= epsilon) x = 0.5 (x + /x); return x; Introduction to Progrmming Dept. CS, UPC 15 Introduction to Progrmming Dept. CS, UPC 14 Squre root (using Newton-Rphson) Exmple: squre_root(1024.0) x 1.00000000000000000000 512.50000000000000000000 257.24902439024390332634 130.61480157022683101786 69.227324054488946103447 42.009585631008270922848 33.192487416854376647279 32.021420905000240964000 32.000007164815897908738 32.000000000000802913291 Introduction to Progrmming Dept. CS, UPC 16

Approximting definite integrls Approximting definite integrls There re vrious methods to pproximte definite integrl: The pproximtion is etter if severl intervls re used: The trpezoidl method pproximtes the re with trpezoid: Introduction to Progrmming Dept. CS, UPC 17 Approximting definite integrls Introduction to Progrmming Dept. CS, UPC 18 Approximting definite integrls // Pre: >=, n > 0 // Returns n pproximtion of the definite integrl of f // etween nd using n intervls. doule integrl(doule, doule, int n) { doule h = ( )/n; h doule s = 0; for (int i = 1; i < n; ++i) s = s + f( + i h); return (f() + f() + 2 s) h/2; Introduction to Progrmming Dept. CS, UPC 19 Introduction to Progrmming Dept. CS, UPC 20

Monte Crlo methods Algorithms tht use repeted genertion of rndom numers to perform numericl computtions. The methods often rely on the existence of n lgorithm tht genertes rndom numers uniformly distriuted over n intervl. In C++ we cn use rnd(), tht genertes numers in the intervl [0, RAND_MAX) Introduction to Progrmming Dept. CS, UPC 21 Approximting Approximting Let us pick rndom point within the unit squre. Q: Wht is the proility for the point to e inside the circle? A: The proility is /4 Algorithm: Generte n rndom points in the unit squre Count the numer of points inside the circle (n in ) Approximte /4 n in /n Introduction to Progrmming Dept. CS, UPC 22 Approximting #include <cstdli> // Pre: n is the numer of generted points // Returns n pproximtion of using n rndom points doule pprox_pi(int n) { int nin = 0; doule rndmx = doule(rand_max); for (int i = 0; i < n; ++i) { doule x = rnd()/rndmx; doule y = rnd()/rndmx; if (x x + y y < 1.0) nin = nin + 1; return 4.0 nin/n; n 10 3.200000 100 3.120000 1,000 3.132000 10,000 3.171200 100,000 3.141520 1,000,000 3.141664 10,000,000 3.141130 100,000,000 3.141692 1,000,000,000 3.141604 Introduction to Progrmming Dept. CS, UPC 23 Introduction to Progrmming Dept. CS, UPC 24

Generting rndom numers in n intervl Monte Crlo pplictions: exmples Assume rnd() genertes rndom nturl numer r in the intervl [0, R). Domin Intervl Rndom numer R [0,1) Τ r R R [0, ) rτr R [, ) + rτr Z [0, ) r mod Z [, ) + r mod ( ) Note: Be creful with integer divisions when delivering rel numers (enforce rel division). Determine the pproximte numer of lttice points in sphere of rdius r centered in the origin. Determine the volume of 3D region R defined s follows: A point P = (x, y, z) is in R if nd only if x 2 + y 2 + 2z 2 100 nd 3x 2 + y 2 + z 2 150. And mny other ppliction domins: Mthemtics, Computtionl Physics, Finnces, Simultion, Artificil Intelligence, Gmes, Computer Grphics, etc. Introduction to Progrmming Dept. CS, UPC 25 Exmple: intersection of two odies Determine the volume of 3D region R defined s follows: A point P = (x, y, z) is in R if nd only if x 2 + y 2 + 2z 2 100 nd 3x 2 + y 2 + z 2 150. The intersection of the two odies is inside rectngulr cuoid C, with center in the origin, such tht: x 2 50 y 2 100 z 2 50 The volume of the cuoid is: Introduction to Progrmming Dept. CS, UPC 26 Exmple: intersection of two odies // Returns n estimtion of the volume of the // intersection of two odies with n rndom smples. doule volume_intersection(int n) { int nin = 0; doule s50 = sqrt(50)/rand_max; // scling for numers in [0,sqrt(50)) doule s10 = 10.0/RAND_MAX; // scling for numers in [0,10) // Generte n rndom smples for (int i = 0; i < n; ++i) { // Generte rndom point inside the cuoid doule x = s50 rnd(); doule y = s10 rnd(); doule z = s50 rnd(); // Check whether the point is inside the intersection if (x x + y y + 2 z z <= 100 nd 3 x x + y y + z z <= 150) ++nin; Volume C = 2 50 2 10 2 50 = 4000 return 4000.0 nin/n; Introduction to Progrmming Dept. CS, UPC 27 Introduction to Progrmming Dept. CS, UPC 28

Exmple: intersection of two odies Volume 2474.56 Summry Approximte computtions is resort when no exct solutions cn e found numericlly. Intervls of tolernce re often used to define the level of ccurcy of the computtion. Rndom smpling methods cn e used to sttisticlly estimte the result of some complex prolems. numer of smples Introduction to Progrmming Dept. CS, UPC 29 Introduction to Progrmming Dept. CS, UPC 30