Download presentation
Presentation is loading. Please wait.
Published byRachel Lynch Modified over 9 years ago
1
Approximate computations Jordi Cortadella Department of Computer Science
2
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; // 1.1 3.1 cout.precision(20); cout << x + y << endl; // 4.2000000000000001776 Introduction to Programming© Dept. CS, UPC2
3
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) < 0.000001) … // Ok ! Introduction to Programming© Dept. CS, UPC3
4
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
5
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
6
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
7
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
8
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
9
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
10
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.
11
The Newton-Raphson method Introduction to Programming© Dept. CS, UPC11
12
The Newton-Raphson method Introduction to Programming© Dept. CS, UPC12 source: http://en.wikipedia.org/wiki/Newton’s_method
13
Square root (using Newton-Raphson) Calculate Find the zero of the following function: where Recurrence: Introduction to Programming© Dept. CS, UPC13
14
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(xx – a) >= epsilon) x = 0.5(x + a/x); return x; } Introduction to Programming© Dept. CS, UPC14
15
Square root (using Newton-Raphson) Example: square_root(1024.0) Introduction to Programming© Dept. CS, UPC15 x 1.00000000000000000000 512.50000000000000000000 257.24902439024390332634 130.61480157022683101786 69.227324054488946103447 42.009585631008270922848 33.192487416854376647279 32.021420905000240964000 32.000007164815897908738 32.000000000000802913291
16
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
17
Approximating definite integrals The approximation is better if several intervals are used: Introduction to Programming© Dept. CS, UPC17 a b
18
Approximating definite integrals Introduction to Programming© Dept. CS, UPC18 h
19
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 + ih); return (f(a) + f(b) + 2s)h/2; } Introduction to Programming© Dept. CS, UPC19
20
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
21
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
22
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 (xx + yy < 1.0) nin = nin + 1; } return 4.0nin/n; } Introduction to Programming© Dept. CS, UPC22
23
Approximating n 103.200000 1003.120000 1,0003.132000 10,0003.171200 100,0003.141520 1,000,0003.141664 10,000,0003.141130 100,000,0003.141692 1,000,000,0003.141604 Introduction to Programming© Dept. CS, UPC23
24
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).
25
Monte Carlo applications: examples Introduction to Programming© Dept. CS, UPC25
26
Example: intersection of two bodies Introduction to Programming© Dept. CS, UPC26
27
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 = s50rand(); double y2 = s100rand(); double z2 = s50rand(); // Check whether the point is inside the intersection if (x2 + y2 + 2z2 <= 100 and 3x2 + y2 + z2 <= 150) ++nin; } return 4000.0nin/n; } Introduction to Programming© Dept. CS, UPC27
28
Example: intersection of two bodies Introduction to Programming© Dept. CS, UPC28 1032.66 number of samples
29
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.