Approximate computations Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Python Crash Course Accuracy 3 rd year Bachelors V1.0 dd Hour 7.
Advertisements

CSE 330: Numerical Methods
CS 351/ IT 351 Modeling and Simulation Technologies Errors In Models Dr. Jim Holten.
Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Graduate School of Information Sciences, Tohoku University
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 6 Roots of Equations Bracketing Methods.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Statistics.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Fin500J: Mathematical Foundations in Finance Topic 3: Numerical Methods for Solving Non-linear Equations Philip H. Dybvig Reference: Numerical Methods.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
I. Finding Roots II. Integrating Functions
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Simulation of Random Walk How do we investigate this numerically? Choose the step length to be a=1 Use a computer to generate random numbers r i uniformly.
4.6 Numerical Integration Trapezoid and Simpson’s Rules.
Solving Non-Linear Equations (Root Finding)
Population All members of a set which have a given characteristic. Population Data Data associated with a certain population. Population Parameter A measure.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
CISE301_Topic11 CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4:
Data types and their representation Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
A little bit of geometry Jordi Cortadella Department of Computer Science.
CSCI 125 & 161 Lecture 13 Martin van Bommel. Floating Point Data Floating point numbers are not exact Value 0.1 in binary is very close to 1/10, but not.
Recursion Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Additional Problems.
Sequences Jordi Cortadella Department of Computer Science.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Using Technology to Approximate Roots of Polynomial Equations.
Polynomials Jordi Cortadella Department of Computer Science.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
Monté Carlo Simulation  Understand the concept of Monté Carlo Simulation  Learn how to use Monté Carlo Simulation to make good decisions  Learn how.
Reusing computations Jordi Cortadella Department of Computer Science.
Lecture 4: Statistics Review II Date: 9/5/02  Hypothesis tests: power  Estimation: likelihood, moment estimation, least square  Statistical properties.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Statistics : Statistical Inference Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University 1.
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
Numerical Methods Solution of Equation.
4 Numerical Methods Root Finding Secant Method Modified Secant
Recursive Methods for Finding Roots of Functions Bisection & Newton’s Method.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Vectors Jordi Cortadella Department of Computer Science.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Functions Jordi Cortadella Department of Computer Science.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
CSE 330: Numerical Methods. What is true error? True error is the difference between the true value (also called the exact value) and the approximate.
Monte Carlo Methods Some example applications in C++
Approximate computations
Reasoning with invariants
Solution of Nonlinear Equations (Root finding Problems
Numerical Methods and Analysis
For loops, nested loops and scopes
Some problems for your consideration
Approximate computations
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
Search algorithms for vectors
Jordi Cortadella Department of Computer Science
A little bit of geometry
Solutions for Nonlinear Equations
Presentation transcript:

Approximate computations Jordi Cortadella Department of Computer Science

Living with floating-point numbers Standard normalized representation (sign + fraction + exponent): Ranges of values: Representations for: – , + , +0,  0, NaN (not a number) Be careful when operating with real numbers: double x, y; cin >> x >> y; // cout.precision(20); cout << x + y << endl; // Introduction to Programming© Dept. CS, UPC2

Comparing floating-point numbers Comparisons: a = b + c; if (a – b == c) … // may be false Allow certain tolerance for equality comparisons: if (expr1 == expr2) … // Wrong ! if (abs(expr1 – expr2) < ) … // Ok ! Introduction to Programming© Dept. CS, UPC3

Root of a continuous function Bolzano’s theorem: Let f be a real-valued continuous function. Let a and b be two values such that a < b and f(a)·f(b) < 0. Then, there is a value c  [a,b] such that f(c)=0. Introduction to Programming© Dept. CS, UPC4 a b c

Root of a continuous function Design a function that finds a root of a continuous function f in the interval [a, b] assuming the conditions of Bolzano’s theorem are fulfilled. Given a precision (  ), the function must return a value c such that the root of f is in the interval [c, c+  ]. Introduction to Programming© Dept. CS, UPC5 a b c

Root of a continuous function Strategy: narrow the interval [a, b] by half, checking whether the value of f in the middle of the interval is positive or negative. Iterate until the width of the interval is smaller . Introduction to Programming© Dept. CS, UPC6 a b

Root of a continuous function // Pre: f is continuous, a < b and f(a)*f(b) < 0. // Returns c[a,b] such that a root exists in the // interval [c,c+]. // Invariant: a root of f exists in the interval [a,b] Introduction to Programming© Dept. CS, UPC7 a b

Root of a continuous function double root(double a, double b, double epsilon) { while (b – a > epsilon) { double c = (a + b)/2; if (f(a)f(c) <= 0) b = c; else a = c; } return a; } Introduction to Programming© Dept. CS, UPC8

Root of a continuous function // A recursive version double root(double a, double b, double epsilon) { if (b – a <= epsilon) return a; double c = (a + b)/2; if (f(a)f(c) <= 0) return root(a,c,epsilon); else return root(c,b,epsilon); } Introduction to Programming© Dept. CS, UPC9

The Newton-Raphson method Introduction to Programming© Dept. CS, UPC10 A method for finding successively approximations to the roots of a real-valued function. The function must be differentiable.

The Newton-Raphson method Introduction to Programming© Dept. CS, UPC11

The Newton-Raphson method Introduction to Programming© Dept. CS, UPC12 source:

Square root (using Newton-Raphson) Calculate Find the zero of the following function: where Recurrence: Introduction to Programming© Dept. CS, UPC13

Square root (using Newton-Raphson) Pre: // Pre: a >= 0 // Returns x such that |x 2 -a| <  double square_root(double a) { double x = 1.0; // Makes an initial guess // Iterates using the Newton-Raphson recurrence while (abs(xx – a) >= epsilon) x = 0.5(x + a/x); return x; } Introduction to Programming© Dept. CS, UPC14

Square root (using Newton-Raphson) Example: square_root(1024.0) Introduction to Programming© Dept. CS, UPC15 x

Approximating definite integrals There are various methods to approximate a definite integral: The trapezoidal method approximates the area with a trapezoid: Introduction to Programming© Dept. CS, UPC16

Approximating definite integrals The approximation is better if several intervals are used: Introduction to Programming© Dept. CS, UPC17 a b

Approximating definite integrals Introduction to Programming© Dept. CS, UPC18 h

Approximating definite integrals Pre: // Pre: b >= a, n > 0 // Returns an approximation of the definite integral of f // between a and b using n intervals. double integral(double a, double b, int n) { double h = (b – a)/n; double s = 0; for (int i = 1; i < n; ++i) s = s + f(a + ih); return (f(a) + f(b) + 2s)h/2; } Introduction to Programming© Dept. CS, UPC19

Monte Carlo methods Algorithms that use repeated generation of random numbers to perform numerical computations. The methods often rely on the existence of an algorithm that generates random numbers uniformly distributed over an interval. In C++ we can use rand(), that generates numbers in the interval [0, RAND_MAX) Introduction to Programming© Dept. CS, UPC20

Approximating  Introduction to Programming© Dept. CS, UPC21 Let us pick a random point within the unit square. Q: What is the probability for the point to be inside the circle? A: The probability is  /4 Algorithm: Generate n random points in the unit square Count the number of points inside the circle (n in ) Approximate  /4  n in /n

Approximating  Pre: #include // Pre: n is the number of generated points // Returns an approximation of  using n random points double approx_pi(int n) { int nin = 0; double randmax = double(RAND_MAX); for (int i = 0; i < n; ++i) { double x = rand()/randmax; double y = rand()/randmax; if (xx + yy < 1.0) nin = nin + 1; } return 4.0nin/n; } Introduction to Programming© Dept. CS, UPC22

Approximating  n  , , , ,000, ,000, ,000, ,000,000, Introduction to Programming© Dept. CS, UPC23

Generating random numbers in an interval Introduction to Programming© Dept. CS, UPC24 DomainIntervalRandom number R R R Z Z Note: Be careful with integer divisions when delivering real numbers (enforce real division).

Monte Carlo applications: examples Introduction to Programming© Dept. CS, UPC25

Example: intersection of two bodies Introduction to Programming© Dept. CS, UPC26

Example: intersection of two bodies // Returns an estimation of the volume of the // intersection of two bodies with n random samples. double volume_intersection(int n) { int nin = 0; double s50 = 50.0/RAND_MAX; // scaling for numbers in [0,50) double s100 = 100.0/RAND_MAX; // scaling for numbers in [0,100) // Generate n random samples for (int i = 0; i < n; ++i) { // Generate a random point inside the cuboid double x2 = s50rand(); double y2 = s100rand(); double z2 = s50rand(); // Check whether the point is inside the intersection if (x2 + y2 + 2z2 <= 100 and 3x2 + y2 + z2 <= 150) ++nin; } return nin/n; } Introduction to Programming© Dept. CS, UPC27

Example: intersection of two bodies Introduction to Programming© Dept. CS, UPC number of samples

Summary Approximate computations is a resort when no exact solutions can be found numerically. Intervals of tolerance are often used to define the level of accuracy of the computation. Random sampling methods can be used to statistically estimate the result of some complex problems. Introduction to Programming© Dept. CS, UPC29