Lecture 21 Integration Comp 208 Computers in Engineering
























- Slides: 24
Lecture 21 Integration Comp 208 Computers in Engineering Yi Lin Winter, 2007 2/20/2021 Comp 208 Computers in Engineering 1
Lecture 22 Learning goals l Integration l l 2/20/2021 Rectangular midpoint rule Trapezoidal rule Simpson’s rule Monte Carlo Comp 208 Computers in Engineering 2
Integration l Recall the definition of the integral l As with differentiation, let’s experiment with small but finite deltas 2/20/2021 Comp 208 Computers in Engineering 3
Meet our function for today 2/20/2021 Comp 208 Computers in Engineering 4
‘Dumb’ integrals, Left-point rule l Again, applying the definition directly l 2/20/2021 Obtain the left-point rectangular rule Comp 208 Computers in Engineering 5
Left-point rectangular rule l l Note how this scheme will always underestimate an increasing function and overestimate a decreasing function This maps well to the definition but we can very easily make it much better so we won’t even bother to write code for this. 2/20/2021 Comp 208 Computers in Engineering 6
Right-point rule l Using the right point as height, it will overestimate the ascending functions but underestimate the descending functions 2/20/2021 Comp 208 Computers in Engineering 7
Midpoint rule l l 2/20/2021 Each rectangular band which makes up the integral is evaluated at the midpoint Hopefully the overestimation and underestimation effects cancel out on each panel Comp 208 Computers in Engineering 8
Midpoint algorithm double midpoint_int( Df. D foo , double a , double b , int panels ) { int i; double sum = 0; double dx = ( b - a ) / panels; double x = a + dx / 2. 0; for( i = 0; i < panels; ++i ) { sum += foo( x ); x += dx; } return sum * dx; } 2/20/2021 Comp 208 Computers in Engineering 9
How good is the midpoint rule? l Absolutely accurate for constant and linear functions l l How accurate for parabolas and other non-linear functions? This is a pretty naïve way of doing things. 2/20/2021 Comp 208 Computers in Engineering 10
Trying other geometries l The rectangle isn’t the only quadrilateral that we can use to evaluate an integral l l We could try even more sophisticated shapes l l What about a trapeze? Fit a parabola through 3 points and integrate that How can we make integration rules that guarantee higher accuracy? 2/20/2021 Comp 208 Computers in Engineering 11
Trapezoidal rule Approximate curves as sequences of straight lines instead of sequences of constants 2/20/2021 Comp 208 Computers in Engineering 12
Trapezoidal integral l l The integral formula comes from the Taylor series The error term has a known order of magnitude which comes from the terms we truncate from the series to make the integration rule 2/20/2021 Comp 208 Computers in Engineering 13
Summing trapezes l Area of a trapeze l Remember that all the trapezes share an edge 2/20/2021 Comp 208 Computers in Engineering 14
Optimizing the sum l Because we are adding up all the inner edges twice but dividing all of our sums by two we can reduce the effort l l 2/20/2021 Start sum with the outer edges divided by two Then add all the inner edges undivided Comp 208 Computers in Engineering 15
Trapezoidal integration double trapezoidal_int( Df. D foo , double a , double b , int panels ) { int i; double sum = ( foo( a ) + foo( b ) ) / 2. 0; double dx = ( b - a ) / panels; double x = a + dx; for( i = 0; i < ( panels - 1); ++i ) { sum += foo( x ); x += dx; } return sum * dx; } 2/20/2021 Comp 208 Computers in Engineering 16
How good is this? l If deltas are small enough then the straight line approximation can be good l l How does it deal with errors? Is that a good way? How does this method’s accuracy relate to how many points it uses (relate polynomials) 2/20/2021 Comp 208 Computers in Engineering 17
Guarantees? l Midpoint and trapezoidal rules, amazingly enough, offer the same guarantees l l 100% accurate on constants and linear functions BUT, trapezoidal deals with errors better, on average. 2/20/2021 Comp 208 Computers in Engineering 18
Using a parabola l Simpson’s rule fits a parabola through 3 points and integrates it l l l 2/20/2021 Definition of what a panel is, is now somewhat up in the air… our old panels had 2 points but now we need three We can subdivide our current panels… …or we can use neighbour panels together. Comp 208 Computers in Engineering 19
Using a parabola l l As the trapezoidal rule for integration finds the area under the line connecting the endpoints of a panel, Simpson's rule finds the area under the parabola which passes through 3 points (the endpoints and the midpoint) on a curve. Area under parabola: Δx 2/20/2021 Comp 208 Computers in Engineering Δx 20
Simpson’s formula l Again, the formula comes from the taylor series: 2/20/2021 Comp 208 Computers in Engineering 21
Simpson’s algorithm double simpsons_int( Df. D foo , double a , double b , int panels ) { int i; double sum = 0; double dx = ( b - a ) / panels; double x = a; for( i = 0; i < panels; ++i ) { sum += foo( x ) + 4*foo( x + dx/2. 0 ) + foo( x + dx ); x += dx; } return sum * dx / 6. 0; } 2/20/2021 Comp 208 Computers in Engineering 22
How good is this? l Look at the error term… ∆x 5!! l l If ∆x is 0. 01, then the precision is already beyond most engineering requirements… Even more methods exist however… l 2/20/2021 Even methods to make methods of arbitrary precision Comp 208 Computers in Engineering 23
Monte Carlo Integration double monte_carlo_int(Df. D f, double x 0, double x 1, int n) { double sum = 0; int i; srand(time(NULL)); // Sum n random values of f(x) with x in [x 0, x 1]. for(i = 0; i < n; ++i) sum += f( (rand() / RAND_MAX) * (x 1 - x 0) + x 0 ); // Divide the sum by n to get the average value of f(x) with x in [x 0, x 1]. // Multiply by (x 1 - x 0) to get the area. return (sum / n) * (x 1 - x 0); } 2/20/2021 Comp 208 Computers in Engineering 24
























