Comp 208 Computers in Engineering Yi Lin Winter, 2007 Lecture 21 Integration Comp 208 Computers in Engineering Yi Lin Winter, 2007 2019/5/13 Comp208 Computers in Engineering
Lecture 22 Learning goals Integration Rectangular midpoint rule Trapezoidal rule Simpson’s rule Monte Carlo 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Integration Recall the definition of the integral As with differentiation, let’s experiment with small but finite deltas 2019/5/13 Comp208 Computers in Engineering
Meet our function for today 2019/5/13 Comp208 Computers in Engineering
‘Dumb’ integrals, Left-point rule Again, applying the definition directly Obtain the left-point rectangular rule 2019/5/13 Comp208 Computers in Engineering
Left-point rectangular rule 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. 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Right-point rule Using the right point as height, it will overestimate the ascending functions but underestimate the descending functions 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Midpoint rule Each rectangular band which makes up the integral is evaluated at the midpoint Hopefully the overestimation and underestimation effects cancel out on each panel 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Midpoint algorithm double midpoint_int( DfD 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; 2019/5/13 Comp208 Computers in Engineering
How good is the midpoint rule? Absolutely accurate for constant and linear functions How accurate for parabolas and other non-linear functions? This is a pretty naïve way of doing things. 2019/5/13 Comp208 Computers in Engineering
Trying other geometries The rectangle isn’t the only quadrilateral that we can use to evaluate an integral What about a trapeze? We could try even more sophisticated shapes Fit a parabola through 3 points and integrate that How can we make integration rules that guarantee higher accuracy? 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Trapezoidal rule Approximate curves as sequences of straight lines instead of sequences of constants 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Trapezoidal integral 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 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Summing trapezes Area of a trapeze Remember that all the trapezes share an edge 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Optimizing the sum Because we are adding up all the inner edges twice but dividing all of our sums by two we can reduce the effort Start sum with the outer edges divided by two Then add all the inner edges undivided 2019/5/13 Comp208 Computers in Engineering
Trapezoidal integration double trapezoidal_int( DfD 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; 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering How good is this? If deltas are small enough then the straight line approximation can be good 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) 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Guarantees? Midpoint and trapezoidal rules, amazingly enough, offer the same guarantees 100% accurate on constants and linear functions BUT, trapezoidal deals with errors better, on average. 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Using a parabola Simpson’s rule fits a parabola through 3 points and integrates it 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. 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Using a parabola 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 Δx 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Simpson’s formula Again, the formula comes from the taylor series: 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering Simpson’s algorithm double simpsons_int( DfD 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; 2019/5/13 Comp208 Computers in Engineering
Comp208 Computers in Engineering How good is this? Look at the error term… ∆x5!! If ∆x is 0.01, then the precision is already beyond most engineering requirements… Even more methods exist however… Even methods to make methods of arbitrary precision 2019/5/13 Comp208 Computers in Engineering
Monte Carlo Integration double monte_carlo_int(DfD f, double x0, double x1, int n) { double sum = 0; int i; srand(time(NULL)); // Sum n random values of f(x) with x in [x0, x1]. for(i = 0; i < n; ++i) sum += f( (rand() / RAND_MAX) * (x1 - x0) + x0 ); // Divide the sum by n to get the average value of f(x) with x in [x0, x1]. // Multiply by (x1 - x0) to get the area. return (sum / n) * (x1 - x0); } 2019/5/13 Comp208 Computers in Engineering